-1

I have this code in textbox 2 which multiply the value of text box 1 and 2 adds the result to text box 3:

private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox3.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text));
        }

When I insert the data in the database via this

code private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Insert into Receipts(Qty,ItemName,Price,Amount) values('" + textBox2.Text + "','" + textBox4.Text + "','" + textBox1.Text + "','" + textBox3.Text + "')";
            cmd.ExecuteNonQuery();

            con.Close();
            this.receiptsTableAdapter.Fill(this.shopDataSet.Receipts);
            textBox4.Clear();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();


            //MessageBox.Show("Data Inserted !");
        }

I get error on textBox2.Clear(); ( exceptional handling but data inserts succcessfully ) program is working fine without this,

textBox4.Clear();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();

I want to clear text boxes, i am only getting error on textbox2 which have the code to multiply the value of text box 1 and 2 adds the result to text box 3.

thanks :)

Trevor
  • 7,777
  • 6
  • 31
  • 50
Shehrooz
  • 39
  • 4
  • 1
    **Always use parameterized sql and avoid string concatenation** to add values to sql statements. This mitigates SQL Injection vulnerabilities and ensures values are passed to the statement correctly. See [How can I add user-supplied input to an SQL statement?](https://stackoverflow.com/q/35163361/1260204), and [Exploits of a Mom](https://xkcd.com/327/). – Igor Mar 26 '20 at 12:28
  • 1
    The exception is likely originating from `textBox2_TextChanged` and is likely a FormatException because you are trying to convert an empty string to an int. Debug your code to better understand how to trouble shoot exceptions and unexpected results. – Igor Mar 26 '20 at 12:29
  • i am not expert with debugging etc. Can you share the solution for this ? – Shehrooz Mar 26 '20 at 12:42

2 Answers2

1

I get error on textBox2.Clear(); ( exceptional handling but data inserts succcessfully ) program is working fine without this,

 textBox4.Clear();
 textBox1.Clear(); // This is the first problem
 textBox2.Clear(); // This is the second problem
 textBox3.Clear();

The reason for this error is because of the event textBox2_TextChanged. Inside you are trying to use the Text property to perform a calculation, this fails because the Text property is empty and it fails to convert it.

 textBox3.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text));

To fix this, you can check first, if the Text property can be successfully parsed before using it using the Int.TryParse Method along with an out variable; only available targeting C# >= 7.0. For example:

 private void textBox2_TextChanged(object sender, EventArgs e)
 {
    if(int.TryParse(textBox1.Text, out int txt1) && int.TryParse(textBox2.Text, out int txt2))
    {
       textBox3.Text = (txt1 * txt2).ToString();
    }
 }

Another version you could perhaps use if targeting versions older than C# 7.0:

 private void textBox2_TextChanged(object sender, EventArgs e)
 {
    int txt1;
    int txt2;
    if(int.TryParse(textBox1.Text, out txt1) && int.TryParse(textBox2.Text, out txt2))
    {
       textBox3.Text = (txt1 * txt2).ToString();
    }
 }
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • this code showing red underline under txt , int1, int2 – Shehrooz Mar 26 '20 at 13:15
  • Under what code, there's no `int1` or `int2` in the code above. Do you mean `txt1` and or `txt2`? If so, you may be using an older version of `c#`, I'll update code. – Trevor Mar 26 '20 at 13:18
  • under **txt1** and **txt2** and **int** – Shehrooz Mar 26 '20 at 13:20
  • cannot implicitly convert type 'int' to 'string' – Shehrooz Mar 26 '20 at 13:25
  • Or rather than trying to do a `TryParse` you could just check `if(string.IsNullOrEmpty() )` and then try to parse your int. – 845614720 Mar 26 '20 at 13:25
  • Thanks :), can you please explain what i was doing wrong and what you did to solve the error again thank you very much, it was my first time here! – Shehrooz Mar 26 '20 at 13:30
  • @Frank `Or rather than trying to do a TryParse you could just check if(string.IsNullOrEmpty() ) and then try to parse your int` why check if its null and or empty and then try and parse it? There's no need to do this because `TryParse` does this for you. – Trevor Mar 26 '20 at 13:31
  • @Shehrooz Please see my second paragraph in my post. Basically calling `clear` on the textbox control clears the `Text` property and then fires that event off, when it does, the `Text` property is empty and it fails to convert that `Text` property to an int, so it fails. – Trevor Mar 26 '20 at 13:32
0

Like others have said, the issue you're having is related to textBox2_TextChanged this void is getting called everytime that you change anything in textBox2.

In my opinion you should be checking your textbox(s) to make sure that they are not empty, and then try to parse your ints.

Here's something to get you started.

https://dotnetfiddle.net/lyNnQY

845614720
  • 756
  • 1
  • 7
  • 22
  • I'm sorry, but the fiddle isn't correct. They are not adding values, checking if the `Text` properties are null and or empty isn't needed as the `TryParse` will take care of this; this step isn't needed. – Trevor Mar 26 '20 at 13:37
  • `TryParse` will return a `bool` back. The only reason I would be doing it this way is for better error handling. If you use `TryParse` and your textbox is empty, it will return false. If your textbox is not empty, and you have a invalid value; it will also return false. My approach is to check `NullOrEmpty` so the user then has a chance to validate the input and convert to an `int`, if it's not valid return a different error message. It's different, not wrong. – 845614720 Mar 26 '20 at 13:44
  • `so the user then has a chance to validate the input and convert to an int` of course and I would agree *if that's what the user is doing*, but they are not. What about the fiddle calculation? – Trevor Mar 26 '20 at 13:48