none
Help me !!! Input string was not in a correct format.?? RRS feed

  • Question

  •  public Main()
            {
                InitializeComponent();

                connection.Open();

                string selectQuery = "SELECT * FROM user_info WHERE atm_id=" + int.Parse(textBox1.Text);
                command = new MySqlCommand(selectQuery, connection);
                mdr = command.ExecuteReader();

                if (mdr.Read())
                {
                    tbcurrentaccount.Text = mdr.GetInt32("cash").ToString();
                }
                else
                {
                    textBox1.Text = "";
                    tbcurrentaccount.Text = "";
                    connection.Close();
                }
                connection.Close();
            }
    Saturday, January 21, 2017 5:08 PM

All replies

  • Hi

    The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer.

    >>int.Parse(textBox1.Text);

    It's extremely unlikely that the text boxes will contain a valid integer immediately when the form is created - which is where you're getting the integer values. It would make much more sense to update textBox1.Text in the button click events (in the same way that you are in the constructor).

    Also, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.

      textBox1.Text = "20";
    
      int i;
    string selectQuery = "SELECT * FROM user_info WHERE atm_id=" + int.TryParse(textBox1.Text,out i);// 
    

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Monday, January 23, 2017 2:29 AM
    Monday, January 23, 2017 2:08 AM
  • Any updates?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 26, 2017 3:08 AM
    Moderator