Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i try insert , it does not work

What I have tried:

C#
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>"); 
Posted
Updated 28-Jul-16 3:40am
v2
Comments
Karthik_Mahalingam 28-Jul-16 9:27am    
what are the column Types in table ?

the column name and values are not in sync,
tring str = "insert into LoanMaster( custId, principal,LoanTerm,LoanInterest,LoanMaturedAmt,LoanRate, Loanloantype, Loanmonthlyp) ";
str += " values (@custId, @principal,@LoanInterest,@LoanMaturedAmt,@LoanRate,@Loanloantype,@Loanmonthlyp,@LoanTerm)";
Kornfeld Eliyahu Peter 28-Jul-16 9:30am    
Not everything NVARCHAR can hold can be converted to numeric! Use your debugger to see what the actual value is!!!
Member 12652110 28-Jul-16 9:35am    
thank you
Member 12652110 28-Jul-16 9:35am    
it works perfectly

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.
 
Share this answer
 
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

C#
string str = "insert into LoanMaster( custId, principal,LoanTerm,LoanInterest,LoanMaturedAmt,LoanRate, Loanloantype, Loanmonthlyp) ";
                             str += " values (@custId, @principal,@LoanTerm,@LoanInterest,@LoanMaturedAmt,@LoanRate,@Loanloantype,@Loanmonthlyp)";
 
Share this answer
 
C#
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 .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900