2011-03-19 51 views
-4

對於下面顯示的代碼,如果發生錯誤,我將如何使用try/catch塊來處理錯誤?如何在vb.net中使用try和catch塊?

 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() 
     con.Close() 
END SUB 

我用VB.Net控制和LINQ to SQL執行所有更新和插入,所以當錯誤發生時我想顯示「請輸入正確的數據」,我該怎麼辦,一旦我抓住一個例外?

+0

請你不要罵的各位 – 2011-03-19 03:07:46

+0

可能重複[如何使用vb.net try和catch(http://stackoverflow.com/questions/ 5359610 /如何使用的嘗試,並趕上在VB網) – 2011-03-19 03:08:01

+0

並且不要張貼同樣的問題兩次! – 2011-03-19 03:09:04

回答

1

我添加了一個簡單的嘗試......下面接住,但我想你會發現你有很多的問題:

  • 你似乎是在做字符串對象乘法,你應該分析出數字,然後根據需要轉換回字符串對象。
  • 您還將CommandType設置爲兩次,而在第二個實例中設置爲更新命令而不是適當的值。你不應該通過字符串連接來建立你的SQL語句,而是使用sqlparameters
  • 如果你要做字符串連接,至少使用字符串生成器,但不要做字符串連接(見上文)
  • 一次性對象(如SqlCommand的)需要放置在使用塊
  • 全部大寫請勿張貼
  • 只有您的問題發佈一次,你可以編輯一個問題,一旦你」如果您想添加詳細信息,請發佈它

這裏是體改代碼:

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