none
Input String was not in correct format RRS feed

  • Question

  • Hey guys!Ive following piece of code ,Ive 2 similar projects 1st project has no such exception but 2nd one has exception "Input string was not in correct format"...kindly help me and tell me where im doing mistake...

    private void btnInsert_Click(object sender, EventArgs e)
     {

    if (txtCode.Equals("") || txtName.Equals("") || txtCity.Equals("") )
                    {
    MessageBox.Show("Generate Code And Enter Employee Further Details.");
                    }

    else
                   {


    try
                  {
    int code = Int32.Parse(txtCode.Text);
    string name = txtName.Text;
    string city=txtCity.Text;

    stringconString = "server=.; database=MyDb; uid=sa;  pwd=123;";
    string query = "insert into record values(" + code + " , '" + name + "' , '"+city+"')";


    con = new SqlConnection(conString);
                        SqlCommand sqlcmd = new SqlCommand(query, con);

    con.Open();
    sqlcmd.ExecuteNonQuery();

    MessageBox.Show("Record Saved");



                }



    catch(Exception ex)
                {
    MessageBox.Show(ex.Message);
                }


    finally
                {
    if (con.State == ConnectionState.Open)
                  {
    con.Close();
                  }

     }
            }
        }

                                                                                                                                                                                                                                                                                                                                                                                                                         


    • Edited by Khaqan Raza Thursday, November 1, 2012 5:42 AM
    Thursday, November 1, 2012 5:40 AM

Answers

  • That exception is likely coming from Int32.Parse. It means that the txtCode textbox doesn't contain a number.

    This line is wrong:

    if (txtCode.Equals("") || txtName.Equals("") || txtCity.Equals("") )

    You're comparing the textbox itself with a string, that will always return false and as a result the else branch will be executed even if you have no text in those textboxes. Use the Text property:

    if (txtCode.Text.Equals("") || txtName.Text.Equals("") || txtCity.Text.Equals("") )

    • Proposed as answer by RohitArora Thursday, November 1, 2012 11:07 AM
    • Marked as answer by Khaqan Raza Friday, November 2, 2012 10:09 AM
    Thursday, November 1, 2012 6:24 AM
    Moderator

All replies

  • That exception is likely coming from Int32.Parse. It means that the txtCode textbox doesn't contain a number.

    This line is wrong:

    if (txtCode.Equals("") || txtName.Equals("") || txtCity.Equals("") )

    You're comparing the textbox itself with a string, that will always return false and as a result the else branch will be executed even if you have no text in those textboxes. Use the Text property:

    if (txtCode.Text.Equals("") || txtName.Text.Equals("") || txtCity.Text.Equals("") )

    • Proposed as answer by RohitArora Thursday, November 1, 2012 11:07 AM
    • Marked as answer by Khaqan Raza Friday, November 2, 2012 10:09 AM
    Thursday, November 1, 2012 6:24 AM
    Moderator
  • Thanks alot Mike

    ATTARI

    Friday, November 2, 2012 10:09 AM
  • Hello Mike!
    Can we make a separate database connection class in our webapplication,through which we can access connection objects like conn or comm ...Mike if its possible plz share it with me.im working on a university project in which ive to build a website for an Arts College...Also
    Ive following piece of code...Ive a gridview in which 1st column contains checkbox control and rest of columns bound to database..ive an error of object reference not set to an instance of an object...
    Help me Mike!!

    try
                {
                    for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox c = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("cbSelect");
                    if (c.Checked==true)
                    {
                        lblResult.Text += GridView1.Rows[i].Cells[1].Text + " , ";
                    }
                }
                }
                catch (Exception ex)
                {

                    lblResult.Text=ex.Message;
                }

            }

    will be wait for your reply


    ATTARI

    Saturday, March 9, 2013 10:48 AM