将nvarchar转换为数字时出错 [英] Error converting nvarchar to numeric

查看:75
本文介绍了将nvarchar转换为数字时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试插入时,它不起作用



我尝试过:



 尝试 
{
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 to LoanRate column
where @Loadloantype holds the string value and LoadRate is of type decimal

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆