Failed to convert parameter value from string to decimal(when field is empty)(无法将参数值从字符串转换为十进制(当字段为空时))
问题描述
我遇到了一个已经完成一半的问题.
现在问题是我写的
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结束如果
注意:以上只是一个例子.
这篇关于无法将参数值从字符串转换为十进制(当字段为空时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法将参数值从字符串转换为十进制(当字段为空
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
