How do I use a try and catch block in vb.net? [closed]
For the code shown below, how would I use a try/catch block to handle an error if it were to occur?
TOTKILO.Text = KILO.Text * TOUCH.Text * 0.01
TextBox10.Text = TextBox9.Text * TextBox8.Text * 0.01
K = Math.Round(Val(TOTKILO.Text) - Val(TextBox10.Text), 5)
TextBox11.Text = TextBox7.Text + K
cmd.CommandType = Data.CommandType.Text
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True"
con.Open()
If RadioButton1.Checked Then
GOLD = 1
ElseIf RadioButton2.Checked Then
SILVER = 1
End If
If RadioButton3.Checked Then
KGOLD = 1
Else开发者_JAVA百科If RadioButton4.Checked Then
KSILVER = 1
End If
cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " ) "
cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & TextBox11.Text & " WHERE CUSTOMERNAME = '" & ComboBox1.Text & "' "
cmd.Connection = con
cmd.ExecuteNonQuery()
con.Close()
END SUB
I used VB.Net controls and LINQ to SQL to perform all Updates and Inserts, so when an error occurs I want to show "Enter Data Correctly", how do I do that once I catch an exception?
I have added a simple Try...Catch below, but I think you will find you have a lot of issues:
- You appear to be doing multiplication on string objects, you should parse out the numbers first then cast back to string objects as necessary.
- You are also setting CommandType twice and in the second instance to an update command rather than an appropriate value.
- Your code is wide open to SQL injection, you should not be building your SQL statements through string concatenation, but rather using sqlparameters
- If you are going to do string concatenation at least use a string builder, but don't do string concatenation (see above)
- Disposable objects (like SQLCommand) need to be placed in Using blocks
- Don't post in all caps
- Only post your question once, you can edit a question once you've posted it if you'd like to add detail
Here is the modifed code:
Try
TOTKILO.Text = KILO.Text * TOUCH.Text * 0.01
TextBox10.Text = TextBox9.Text * TextBox8.Text * 0.01
K = Math.Round(Val(TOTKILO.Text) - Val(TextBox10.Text), 5)
TextBox11.Text = TextBox7.Text + K
cmd.CommandType = Data.CommandType.Text
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True"
con.Open()
If RadioButton1.Checked Then
GOLD = 1
ElseIf RadioButton2.Checked Then
SILVER = 1
End If
If RadioButton3.Checked Then
KGOLD = 1
ElseIf RadioButton4.Checked Then
KSILVER = 1
End If
cmd.CommandText = " INSERT INTO SALES VALUES('" & ComboBox1.Text & " ' , " & SILVER & " ," & GOLD & ",'" & ComboBox2.Text & "'," & KILO.Text & ", " & TOUCH.Text & " ," & TOTKILO.Text & "," & TextBox3.Text & "," & TextBox8.Text & "," & KGOLD & "," & KSILVER & "," & TextBox9.Text & " ," & TextBox10.Text & "," & TextBox11.Text & "," & TextBox12.Text & " , " & TextBox13.Text & " ) "
cmd.CommandType = " UPDATE BALANCE SET OBBALANCE = " & TextBox11.Text & " WHERE CUSTOMERNAME = '" & ComboBox1.Text & "' "
cmd.Connection = con
cmd.ExecuteNonQuery()
Catch (ex as Exception)
MsgBox.Show("Enter Data Correctly: " & ex.toString)
Finally
con.Close()
End Try
精彩评论