简体   繁体   English

无法将数据类型nvarchar转换为数值

[英]Cannot convert data type nvarchar to numeric

get this error to the txtNum.Text i dont know what to do i already tried the 得到这个错误到txtNum.Text我不知道该怎么办我已经尝试了

Convert.ToInt32 and SqlDbType.Int but it doesnt work please help me Convert.ToInt32SqlDbType.Int但它不起作用,请帮助我

the total has a $ sign in it maybe that's the problem. 总数中有一个$符号,也许就是问题所在。 the Total's DataType is Decimal 总计的数据类型为十进制

com = new SqlCommand(@"INSERT INTO Orders(
                        OrderDate, 
                        Username, 
                        FirstName, 
                        LastName, 
                        Address, 
                        Phone, 
                        Total, 
                        HasBeenShipped
                    ) 
                    VALUES(
                        GetDate(),
                        @p5, 
                        @p1, 
                        @p2, 
                        @p3, 
                        @p4, 
                        @p6,
                        'false'
                    )", con2);
com.Parameters.AddWithValue("@p5", Label2.Text);
com.Parameters.AddWithValue("@p1", txtFname.Text);
com.Parameters.AddWithValue("@p2", TxtLname.Text);
com.Parameters.AddWithValue("@p3", TxtAdd.Text);
com.Parameters.AddWithValue("@p4", Convert.ToInt32(TxtNum.Text));
com.Parameters.AddWithValue("@p6", lblTotal.Text);
com.ExecuteNonQuery();

I suppose that the last field HasBeenShipped is a bit datatype in the database. 我想最后一个字段HasBeenShipped是数据库中的一点数据类型。
If this is the case then pass 0 (the numeric value) for it not 'false' (a string) 如果是这种情况,则为其传递0(数值),而不是“ false”(字符串)

Moreover it is the parameter @p6 that contains the value for the field Total and thus it is this parameter that should be converted to a number, while the parameter @p4 could be a string if the field Phone is a NVarChar 此外,参数@p6包含字段Total的值,因此应该将此参数转换为数字,而如果字段PhoneNVarChar ,则参数@p4可以是字符串

com = new SqlCommand(@"INSERT INTO Orders 
             (OrderDate, Username, FirstName, LastName, Address, Phone, Total, HasBeenShipped)
       VALUES(GetDate(), @p5,      @p1,       @p2,      @p3,     @p4,   @p6,   0)", con2);
com.Parameters.AddWithValue("@p5", Label2.Text);
com.Parameters.AddWithValue("@p1", txtFname.Text);
com.Parameters.AddWithValue("@p2", TxtLname.Text);
com.Parameters.AddWithValue("@p3", TxtAdd.Text);
com.Parameters.AddWithValue("@p4", TxtNum.Text);
com.Parameters.AddWithValue("@p6", Convert.ToInt32(lblTotal.Text));

EDIT Following your comment below, if the lblTotal.Text contains the currency symbol then you cannot convert directly to an integer with Convert.ToInt32, you should use decimal.TryParse 编辑在下面的注释之后,如果lblTotal.Text包含货币符号,那么您不能使用Convert.ToInt32直接转换为整数,则应使用decimal.TryParse

decimal totalValue
decimal.TryParse(lblTotal.Text, NumberStyles.Currency, 
                 CultureInfo.CurrentCulture, out totalValue);
com.Parameters.AddWithValue("@p6", totalValue);

If lblTotal is formatted as a currency then you can use: 如果lblTotal格式化为货币,则可以使用:

com.Parameters.AddWithValue("@p6", Decimal.Parse(lblTotal.Text,NumberStyles.Currency));

Obviously it will throw an exception if it's not a valid currency value - you'd need to decide what to do in that case. 显然,如果不是有效的货币值,它将引发异常-在这种情况下,您需要决定要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将数据类型nvarchar转换为数字 - Cannot convert datatype nvarchar to numeric 在查询中将数据类型nvarchar转换为数值时出错 - Error converting data type nvarchar to numeric in query “将数据类型 nvarchar 转换为数字时出错。” - “Error converting data type nvarchar to numeric.” 在MSSQL中将nvarchar转换为地理数据类型 - Convert nvarchar to geography data type in MSSQL CSharp ado.net将数据类型nvarchar转换为数值时出错 - CSharp ado.net Error converting data type nvarchar to numeric 获取错误:“在SQL中将数据类型nvarchar转换为数字时出错” - Getting Error: “Error converting data type nvarchar to numeric” in SQL 当我的表上没有任何nvarchar时,如何解决“将nvarchar数据类型转换为数值错误”的问题? - How can i solve “error converting data type nvarchar to numeric” when i don't have any nvarchar on my table? 错误:System.Data.SqlClient.SqlException(0x80131904):将数据类型nvarchar转换为数值时出错 - Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to numeric 错误System.Data.SqlClient.SqlException:'将数据类型nvarchar转换为数字时出错。 在c#中 - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c# 异常无法将nvarchar转换为int - Exception cannot convert nvarchar to int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM