问题描述
我遇到了一个已经完成一半的问题.
现在问题是我写的
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结束如果
注意:以上只是一个例子.
这篇关于无法将参数值从字符串转换为十进制(当字段为空时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!