Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试将字符串写入数据库时发生FormatException_C#_Sql_Asp.net_Barcode - Fatal编程技术网

C# 尝试将字符串写入数据库时发生FormatException

C# 尝试将字符串写入数据库时发生FormatException,c#,sql,asp.net,barcode,C#,Sql,Asp.net,Barcode,我正在编写一个小型web应用程序,我想使用web表单向我的数据库添加一些详细信息。我正在使用asp.net和c#创建此web应用程序,并使用visual studio 2013作为开发工具。我正在使用条形码扫描仪扫描产品的条形码。因为我不能从我的用户界面中包括一个屏幕截图,我有3个标签,3个文本框(条形码,描述,价格),一个添加按钮,我创建了一个数据库来保存我的详细信息。我在数据库中的3列是Barcode,Description,Price,它们的数据类型是varchar(50),varchar

我正在编写一个小型web应用程序,我想使用web表单向我的数据库添加一些详细信息。我正在使用asp.net和c#创建此web应用程序,并使用visual studio 2013作为开发工具。我正在使用条形码扫描仪扫描产品的条形码。因为我不能从我的用户界面中包括一个屏幕截图,我有3个标签,3个文本框(条形码,描述,价格),一个添加按钮,我创建了一个数据库来保存我的详细信息。我在数据库中的3列是
Barcode
Description
Price
,它们的数据类型是
varchar(50)
varchar(50)
decimal(10,2)
。当我运行页面并扫描条形码时,我得到一个异常:

System.FormatException:输入字符串的格式不正确

我在没有条形码扫描器的情况下试过,用键盘输入条形码,效果很好

以下是例外情况的更多详细信息:

“/”应用程序中出现服务器错误。 输入字符串的格式不正确。描述:一个未处理的 执行当前web请求期间发生异常。 请检查堆栈

跟踪以获取有关错误及其来源的更多信息 密码

异常详细信息:System.FormatException:输入字符串不在 格式正确

源错误:

第31行:con.Open();第32行:第33行:
da.InsertCommand.ExecuteNonQuery();第34行:第35行:
con.Close()

源文件:path.aspx.cs行:33

堆栈跟踪:

[格式异常:输入字符串的格式不正确。]
System.Number.stringtonNumber(字符串str,NumberStyles选项, NumberBuffer&number,NumberFormatInfo信息,布尔值

parseDecimal)+11177559 System.Number.parseDecimal(字符串值, NumberStyles选项,NumberFormatInfo numfmt)+172
System.Convert.ToDecimal(字符串值,IFormatProvider)+67
System.String.System.IConvertible.ToDecimal(IFormatProvider) +10 System.Convert.ChangeType(对象值、类型转换类型、IFormatProvider提供程序)+11043185
System.Data.SqlClient.SqlParameter.胁迫值(对象值,元类型 destinationType、Boolean和强制ToDataFeed

布尔值和类型更改,布尔值允许流)+5332823

[FormatException:无法将参数值从字符串转换为字符串 Decimal.]System.Data.SqlClient.SqlParameter.ImpresseValue(对象 值、元类型destinationType、布尔值和强制ToDataFeed、

布尔值和类型更改,布尔值允许流)+5331815
System.Data.SqlClient.SqlParameter.GetImprovedValue()+185
System.Data.SqlClient.SqlParameter.Validate(Int32索引,布尔值 isCommandProc)+102
System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser解析器, SqlParameterCollection参数)+201
System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior 行为、字符串命令文本、SqlParameterCollection

参数,_SqlRPC&rpc)+237
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,运行行为,布尔值

returnStream,布尔异步,Int32超时,任务和任务,布尔 异步写入,SqlDataReader ds)+1421
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,运行行为,布尔值

returnStream,String方法,TaskCompletionSource
1完成,Int32
超时,任务和任务,布尔异步写入)+177
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1 完成,字符串方法名,布尔值

sendToPipe、Int32超时、布尔异步写入)+208
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()+163
PosSystem.Stock.addStock.btnAdd_在 path\add.aspx.cs:33
System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9615682
System.Web.UI.WebControl.Button.RaisePostBackEvent(字符串 事件参数)+103
System.Web.UI.WebControl.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)+10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔IncludeStagesSafterAsyncPoint)

+1724年

任何人都知道为什么会这样,我怎样才能解决它

以下是单击按钮时的代码:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int y;
        da.InsertCommand = new SqlCommand("INSERT INTO Products VALUES(@Barcode,@Description,@Price)", con);
        da.InsertCommand.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = txtBarcode.Text;
        da.InsertCommand.Parameters.Add("@Description", SqlDbType.VarChar).Value = txtDescription.Text;
        da.InsertCommand.Parameters.Add("@Price", SqlDbType.Decimal).Value = txtPPrice.Text;
        con.Open();

        da.InsertCommand.ExecuteNonQuery();

        con.Close();
    }
这条线

da.InsertCommand.Parameters.Add("@Price", SqlDbType.Decimal).Value = txtPPrice.Text;
这似乎是个问题。您正在尝试将文本保存到十进制字段。如果字符串的值可以由sql驱动程序转换为十进制,那么它可以工作,但在这种情况下,它失败了。可能是因为价格字符串包含货币格式(例如,$)


首先尝试将其解析为十进制

我意识到这是一个老问题,但是--我一直遇到这样的代码,问题是允许.NET和Sql强制值

我认为您仍然没有关注真正的问题,即您依赖于未经验证的数据的强制。但是,您当然可以删减任何空白——如果您自己进行转换,您将覆盖更多的领域,而不是试图通过编写更多防御代码来消除输入问题

我通常会在应用程序之外准备一个“沙盒”来测试类似的东西。看看这段代码吧……然而,它还远远不够完美——它演示了一些可以传递给TryParse的标志,这些标志将为您节省大量的麻烦和挫折

    Dim theProblemWithTheData As String = String.Format("$-123.45{0}", vbCrLf)
Dim dontDoThis As New System.Data.SqlClient.SqlParameter("fakeParameter",
                                                         SqlDbType.Decimal)
Dim doThis As New System.Data.SqlClient.SqlParameter("betterFakeParameter",
                                                     SqlDbType.Decimal)
Dim doThisValue As Decimal

Try
  dontDoThis.Value = theProblemWithTheData
  Dim success As Boolean = Decimal.TryParse(theProblemWithTheData,
                                            System.Globalization.NumberStyles.Any,
                                            System.Globalization.CultureInfo.CurrentCulture,
                                            doThisValue)

  If success Then
    doThis.Value = doThisValue
  End If

Catch ex As Exception
  Dim exInfo As String = ex.ToString
End Try

如果使用
.Value=decimal.Parse(txtprice.Text),会发生什么情况?显示在
文本框中输入的值。你可以