将nvarchar转换为数字时出错 [英] Error converting nvarchar to numeric
问题描述
当我尝试插入时,它不起作用
我尝试过:
尝试
{
string str = 插入LoanMaster(custId,principal,LoanTerm,LoanInterest,LoanMaturedAmt,LoanRate,Loanloantype,Loanmonthlyp) 跨度>;
str + = 值(@custId,@ principal,@ LoanInterest,@ LoanMaturedAmt,@ LoanRate,@ Loanloantype,@ Loanmonthlyp,@ LoanTerm)跨度>;
SqlCommand insert = new SqlCommand(str,myConn);
insert.Parameters.AddWithValue( @ custId ,LblCustId.Text);
insert.Parameters.AddWithValue( @ principal,双跨度> .Parse(TbPrincipal.Text));
insert.Parameters.AddWithValue( @ LoanTerm, INT 跨度> .Parse(DdlTerm.SelectedItem.Text));
insert.Parameters.AddWithValue( @ LoanMaturedAmt,双跨度> .Parse(LblMaturedAmt.Text));
insert.Parameters.AddWithValue( @ LoanRate,双跨度> .Parse(LblIntRte.Text));
insert.Parameters.AddWithValue( @ LoanInterest,双跨度> .Parse(LblInterest.Text));
insert.Parameters.AddWithValue( @ Loanloantype,DDlLoanType.SelectedItem.Text);
insert.Parameters.AddWithValue( @ Loanmonthlyp,双跨度> .Parse(LblMthlyP.Text));
myConn.Open();
int insertcount = insert.ExecuteNonQuery();
Response.Write( < script> alert('Successfully applied');< / script> 跨度>);
错误非常简单。它来自SQL,而不是来自C#。这意味着您尝试将非数字的内容存储到数字字段中。这对您来说非常非常容易,我们无法帮助您,因为我们无法查看您的数据库或运行您的代码。
只需使用调试器并验证要发送给SQL的值。然后,您还要检查SQL以确保将正确的参数放入正确的字段中。
非常简单。
引用:string str =插入LoanMaster(custId,principal,
LoanTerm
,LoanInterest,LoanMaturedAmt,LoanRate,Loanloantype,Loanmonthlyp);
str + =值(@custId,@ principal,@ LoanInterest,@ LoanMaturedAmt,@ LoanRate,@ Loanloantype,@ Loanmonthlyp,@LoanTerm
);
int insert语句,您将@Loadloantype
值传递给LoanRate
专栏
其中@Loadloantype
持有字符串
值和LoadRate
的类型为十进制
使用这个
string str = 插入LoanMaster(custId,principal,LoanTerm,LoanInterest,LoanMaturedAmt,LoanRate,Loanloantype,Loan)每月一次);
str + = 值(@custId,@ principal,@ LoanTerm,@ LoanInterest,@ LoanMaturedAmt,@ LoanRate,@ Loanloantype,@ Loanmonthlyp);
嗨Karthik
您可以执行一件事,尝试在插入之前进行验证...(SqlDbType.Decimal)
insert.Parameters.AddWithValue( @ Loanloantype,Decimal.Parse(DDlLoanType.SelectedItem.Text));
注意: 如果不起作用请与我分享详细信息。
when i try insert , it does not work
What I have tried:
try
{
string str = "insert into LoanMaster( custId, principal,LoanTerm,LoanInterest,LoanMaturedAmt,LoanRate, Loanloantype, Loanmonthlyp) ";
str += " values (@custId, @principal,@LoanInterest,@LoanMaturedAmt,@LoanRate,@Loanloantype,@Loanmonthlyp,@LoanTerm)";
SqlCommand insert = new SqlCommand(str, myConn);
insert.Parameters.AddWithValue("@custId", LblCustId.Text);
insert.Parameters.AddWithValue("@principal", double.Parse(TbPrincipal.Text));
insert.Parameters.AddWithValue("@LoanTerm", int.Parse(DdlTerm.SelectedItem.Text));
insert.Parameters.AddWithValue("@LoanMaturedAmt", double.Parse(LblMaturedAmt.Text));
insert.Parameters.AddWithValue("@LoanRate", double.Parse(LblIntRte.Text));
insert.Parameters.AddWithValue("@LoanInterest", double.Parse(LblInterest.Text));
insert.Parameters.AddWithValue("@Loanloantype", DDlLoanType.SelectedItem.Text);
insert.Parameters.AddWithValue("@Loanmonthlyp", double.Parse(LblMthlyP.Text));
myConn.Open();
int insertcount = insert.ExecuteNonQuery();
Response.Write("<script>alert('Successfully applied');</script>");
The error is pretty simple. It is coming from SQL, not from C#. This means you are trying to store something that is not a number into a numeric field. This is very, very easy for you to fix on your own and we cannot help you all the way because we cannot see your database nor run your code.
Just use the debugger and verify the values you are sending to SQL. Then you also check your SQL to make sure you are putting the correct parameters into the correct fields.
Very simple.
Quote:string str = "insert into LoanMaster( custId, principal,
LoanTerm
,LoanInterest,LoanMaturedAmt,LoanRate, Loanloantype, Loanmonthlyp) ";
str += " values (@custId, @principal,@LoanInterest,@LoanMaturedAmt,@LoanRate,@Loanloantype,@Loanmonthlyp,@LoanTerm
)";
int the insert statement, you are passing@Loadloantype
value toLoanRate
column
where@Loadloantype
holds thestring
value andLoadRate
is of typedecimal
use this
string str = "insert into LoanMaster( custId, principal,LoanTerm,LoanInterest,LoanMaturedAmt,LoanRate, Loanloantype, Loanmonthlyp) "; str += " values (@custId, @principal,@LoanTerm,@LoanInterest,@LoanMaturedAmt,@LoanRate,@Loanloantype,@Loanmonthlyp)";
Hi Karthik You can do one things,Try to put validation before insertion ...(SqlDbType.Decimal) insert.Parameters.AddWithValue("@Loanloantype", Decimal.Parse(DDlLoanType.SelectedItem.Text)); Note : if it not work please share details with me .
这篇关于将nvarchar转换为数字时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!