"Error converting data type nvarchar to numeric" even if my data types are correct
My code experienced a weird problem. Last night, I ran my program. Everything was fine. Today, I ran the program, and got an error
Error converting data type nvarchar to numeric
even if I didn't change any settings or configurations in my database nor my code.
Code:
private void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand cmd;
SqlConnection con;
con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Kim\Documents\SMEMCO.mdf;Integrated Security=True");
con.Open();
cmd = new SqlCommand("INSERT INTO LoanRecord (EmployeeName, EmployeeNumber, Department, LoanAmount, YearsToPay, MonthlyPayment, TotalPayment) VALUES (@EmployeeName, @EmployeeNumber, @Department, @LoanAmount, @YearsToPay, @MonthlyPayment, @TotalPayment)", con);
cmd.Parameters.AddWithValue("@EmployeeName", txtName.Text);
cmd.Parameters.AddWithValue("@EmployeeNumber", txtEmpno.Text);
cmd.Parameters.AddWithValue("@Department", txtDept.Text);
cmd.Parameters.AddWithValue("@LoanAmount", amount);
cmd.Parameters.AddWithValue("@YearsToPay", txtYears.Text);
cmd.Parameters.AddWithValue("@MonthlyPayment", monthly);
cmd.Parameters.AddWithValue("@TotalPayment", total);
cmd.ExecuteNonQuery();
MessageBox.Show("Data successfully added to database.");
}
The exception was called in this form. I'm sure that my data types are appropriate.
I tried doing this but it threw an error
Input string was not in correct format
Code:
decimal amount = decimal.Parse(txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", amount);
Yesterday's code didn't have that code above and yet, it worked fine. What happened and what did I do wrong?
from Recent Questions - Stack Overflow https://ift.tt/3mxX4JL
https://ift.tt/2J7zzZn
Comments
Post a Comment