問題描述
我遇到了一個已經完成一半的問題.
現在問題是我寫的
I got a problem which is half done.
Now the problem is I wrote
使用 vb.net 和 mssql 2014 作為后端
Using vb.net and mssql 2014 ent as backend
這是我寫的查詢
cmd = New SqlCommand(("insert into FAMPAR(Open_Bal, Curr_Bal, Disc_Perc, Stop_Days, Fix_Days) values(@Open_Bal , @Curr_Bal , @Disc_Perc , @Stop_Days ,@Fix_Days)"), con1)
cmd.Parameters.Add("Open_Bal", SqlDbType.Decimal).Value = txtOpeningBal.Text
cmd.Parameters.Add("Curr_Bal", SqlDbType.Decimal).Value = txtCurrBal.Text
cmd.Parameters.Add("Disc_Perc", SqlDbType.Decimal).Value = txtDisc.Text
cmd.Parameters.Add("Stop_Days", SqlDbType.Decimal).Value = txtStopDays.Text
cmd.Parameters.Add("Fix_Days", SqlDbType.Decimal).Value = txtBillOutstdg.Text
ExecuteQuery1()
現在,當我在 vbform 的每個字段中寫入時一切正常,但是當我將一個字段留空時,它會創建異常.
Now everything works fine when I write in every field in vbform but when I left one field empty then it creates exception.
SystemformatException: 無法從字符串轉換參數值到小數點...
SystemformatException: Failed to convert parameter value from a string to a decimal...
現在顯示systemformatException
那么是系統端出錯還是sql端出錯...???
Now it shows systemformatException
so is it error in system side or is it error sql side...???
而且我有時必須將框保持為空有時會填充(填充文本框時沒問題,查詢成功執行),那么我該怎么辦???
And I have to keep boxes empty sometimes and sometimes filled(no problem when text boxes are filled, query executes successfully), so what can I do with this???
推薦答案
你應該在你的代碼中放置一個驗證來檢查值是否為空.您可以使用 Decimal.TryParse 方法,請參閱鏈接一>.如果值為空,則在文本中分配0"值.來自鏈接中的示例:
You should put a validation in your code to check if the value is empty or not. You can use the Decimal.TryParse Method See link. If the value is empty, assign '0' value in the text. From the example in the link:
If Decimal.TryParse(txtOpeningBal.Text, number) Thencmd.Parameters.Add("Open_Bal", SqlDbType.Decimal).Value = number別的cmd.Parameters.Add("Open_Bal", SqlDbType.Decimal).Value = DBNull.value結束如果
注意:以上只是一個例子.
這篇關于無法將參數值從字符串轉換為十進制(當字段為空時)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!