i am a newbie so bare with me.. can anybody help me solve this prob.

input string was not in a correct format

this is my code... i can figure out what wrong with this ..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace WindowsFormsApplication3pmp
{
    public partial class PayForm : Form
    {
        private ComboBox stype = new ComboBox();
        private int paynosp, spAdd, payfpair;
        public PayForm()
        {
            InitializeComponent();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void PaySrvcType_SelectedIndexChanged(object sender, EventArgs e)
        {
             stype.Items.Add("Baptismal");
             stype.Items.Add("Special Baptismal");
             stype.Items.Add("Confirmation");
             stype.Items.Add("Communion");

             SavePay.Click += SavePay_Click;
        }

        private void SavePay_Click(object sender, EventArgs e)
    {
            string noor = noOR.Text;
            string paydate = PayDate.Value.ToShortDateString();
            string payname = PayName.Text;
            string stype = PaySrvcType.Text;
            string stypeid = STypeID.Text;
            string payfp = PayFP.Text;
            string payNoSp = PayNoSp.Text;
            string paytotal = payTotal.Text;
            string addsp = Addsp.Text;
            string AssessBy = assessBy.Text;
            paynosp= Convert.ToInt32(payNoSp);
            spAdd = Convert.ToInt32(addsp);

            if (stype == "Baptismal")
            {
                stypeid += "1";
                payfp += "200";
            }
            else if (stype == "Special Baptismal")
            {
                stypeid += "2";
                payfp += "1000";
            }
            else if (stype == "Confirmation")
            {
                stypeid += "3";
                payfp += "100";
            }
            payfpair = Convert.ToInt32(payfp);

            paytotal += (payfpair +(paynosp * spAdd));

          DialogResult msgbox = new DialogResult();
          try
        {    
            if (PayName.Text == "" || assessBy.Text == "" )
            {
                MessageBox.Show("There are still unanswered fields", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            else
            {
                // string adduser = String.Format(@"UPDATE INTO users_info (userID, Fname, Mname, Sname, Bdate, Position, username, password) VALUES (''{0}'',''{1}'', ''{2}'', ''{3}'',''{4}, ''{5}'', ''{6}'', ''{7}'')", UserID, userfname, usermname, usersname, userbdate, userpos, TBusername, TBpass);
                const string connection = "Data source = abc; Database = PMP; Trusted_Connection = True;";
                using (SqlConnection cnx = new SqlConnection(connection))
                {
                    //SqlCommand command = new SqlCommand(adduser, cnx);
                    cnx.Open();

                    string addpay = (@"INSERT INTO table_OR ( ORdate, PayName, PayStypeId, PayStype, ORtotal, ORAsses) 
                                       VALUES ('" + paydate + "', '" +payname+ "', '" + stypeid + "', '" + stype + "', '" + paytotal + "','"+AssessBy+"')");

                    using (SqlCommand add_pay = new SqlCommand(addpay, cnx))
                    {
                        add_pay.ExecuteNonQuery();
                    }

                    MessageBox.Show("Payment successfully made!", "Successful!", MessageBoxButtons.OK);
                    // msgbox = MessageBox.Show("Edit another User?", "Edit Team", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (msgbox == System.Windows.Forms.DialogResult.Yes)
                    {
                        //text_reset();
                        PayName.Focus();
                        return;


                    }
                    else
                        this.Close();
                    cnx.Close();
                }
            }
        }
         
        catch (Exception err)
        {
              MessageBox.Show("Connection to database failed!" + err.Message);
              //text_reset();
              PayName.Focus();

              return;
        }
    }

    
    }
}

Recommended Answers

All 4 Replies

use code tags as your code is impossible to read this way. You also don't indicate where the problem is

I guess soem of your converting to integer from stirng throuwn an exception.
Please double check if your textBoxes which MUST have integers only, do really have them. My guess is that is one of them textboxes there is not only a number inside (a number without any decimal places -thats an integer).

What you can do, to make sure this kind of exception will not happen again, is to use some int checking (I will only show you two examples, for the rest do it by your own):

string payNoSp = PayNoSp.Text;
string addsp = Addsp.Text;
if(int.TryParse(payNoSp, out paynosp) && int.TryParse(addsp, out spAdd))
{
     if (stype == "Baptismal")
     {
         stypeid += "1";
         payfp += "200";
     }
     //and rest of the code here bellow...
}
else
     MessageBox.Show("Please insert only numbers for appropaite fields.");
i really appreciate ur help guys.. thanks..Mitja Bonca

You are welcome mate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.