Error converting data type nvarchar to numericGreetings 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();
}
}

Rajanikant HawaldarPosted Oct 15, 2026, 1:10 PM EDT
Specify type for required fields
Prasad RaveendranPosted Oct 25, 2026, 10:21 PM EDT
The
.Textproperty returns a string, so ADO.NET may send these parameters asnvarchar. SQL Server then attempts to convert them to the column's numeric data type.If a textbox contains an empty string, invalid text, or a number formatted differently from what SQL Server expects, the conversion may fail.
Recommended fix: Validate numeric inputs and explicitly specify the SQL parameter data types.
There is a likely copy-and-paste error
You are passing
commentsTextBox.Textto the@Commissionparameter. If the Commission column is numeric, this is a likely cause of the error.For numeric columns, use
decimal.TryParse()and explicitly define the parameter type. Here is an example for the Commission field:if (!decimal.TryParse(commissionTextBox.Text, out decimal commission))
{
MessageBox.Show("Please enter a valid Commission amount.");
return;
}
cmd.Parameters.Add("@Commission", SqlDbType.Decimal).Value = commission;
You can use the same approach for
Qty,UnitCost,TotalCost,GrossCost,Profit,IncomeTax,ComponentsTotal,ProductionQty, andSalesPrice, depending on their actual SQL Server data types.cmd.Parameters.Add("@Qty", SqlDbType.Decimal).Value = qty;
cmd.Parameters.Add("@UnitCost", SqlDbType.Decimal).Value = unitCost;
Here,
qtyandunitCostshould be decimal variables obtained after validating their respective textbox values. ConfigurePrecisionandScaleon decimal parameters to match the corresponding SQL Server columns.Check the SQL Server column definitions - Run this query in SQL Server Management Studio
This will show which columns are numeric and which are text columns.
My recommendation: Start by correcting the
@Commissionparameter. Then check all the numeric columns and make sure each receives a validated numeric value rather than an arbitrary string.One more improvement: use
usingfor your SQL connection and command so they are disposed of properly, even if an exception occurs.