[英]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.ToInt32
和SqlDbType.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
的值,因此应该将此参数转换为数字,而如果字段Phone
是NVarChar
,则参数@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.