Error converting data type nvarchar to numeric
Greetings to all Forum members.
I have created a Stored Procedure form and named it “Component” on the Price Components table. When I fill the form and click the calculator button it calculates all records I am expecting into the appropriate textboxes. When I click the Save button, I want the Price Components table to be updated accordingly. But when I click the save button, this runtime error displays “Error converting data type nvarchar to numeric”
I wish to attach this message with the form, the error, and the code behind the save button. I will appreciate if someone would help me fix the problem. Thanks.
private void btnSave_Click(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
string query = "UPDATE PriceComponents SET Date=@Date, Item=@Item, ProductName=@ProductName, BatchNo=@BatchNo, Qty=@Qty, GrossQty=@GrossQty, UnitCost=@UnitCost, TotalCost=@TotalCost, GrossCost=@GrossCost, Profit=@Profit, Commission=@Commission, IncomeTax=@IncomeTax, ComponentsTotal=@ComponentsTotal, ProductionQty=@ProductionQty, SalesPrice=@SalesPrice, Comments=@Comments WHERE PriceID=@PriceID";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@PriceID", priceIDTextBox.Text);
cmd.Parameters.AddWithValue("@Date", dateDateTimePicker.Value);
cmd.Parameters.AddWithValue("@Item", itemTextBox.Text);
cmd.Parameters.AddWithValue("@ProductName", productNameTextBox.Text);
cmd.Parameters.AddWithValue("@BatchNo", batchNoComboBox.Text);
cmd.Parameters.AddWithValue("@Qty", qtyTextBox.Text);
cmd.Parameters.AddWithValue("@GrossQty", grossQtyTextBox.Text);
cmd.Parameters.AddWithValue("@UnitCost", unitCostTextBox.Text);
cmd.Parameters.AddWithValue("@TotalCost", totalCostTextBox.Text);
cmd.Parameters.AddWithValue("@GrossCost", grossCostTextBox.Text);
cmd.Parameters.AddWithValue("@Profit", profitTextBox.Text);
cmd.Parameters.AddWithValue("@Commission", commentsTextBox.Text);
cmd.Parameters.AddWithValue("@IncomeTax", incomeTaxTextBox.Text);
cmd.Parameters.AddWithValue("@ComponentsTotal", componentsTotalTextBox.Text);
cmd.Parameters.AddWithValue("@ProductionQty", productionQtyTextBox.Text);
cmd.Parameters.AddWithValue("@SalesPrice", salesPriceTextBox.Text);
cmd.Parameters.AddWithValue("@Comments", commentsTextBox.Text);
cmd.Connection = con;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
MessageBox.Show("Components record updated.");
}
else
{
MessageBox.Show("Components not Updated.");
}
con.Close();
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.