Reputation: 71
I've got a Winform that update a SQL database by editing the two TextBox Product Name and Product cost, However it doesn't update the database, here is my sample code
private void simpleButton5_Click(object sender, EventArgs e)
{
string id = comboBox2.Items[comboBox2.SelectedIndex].ToString();
string name = txtProdName.Text;
string cost = txtProductCost.Text;
cn.Open();
string query = "UPDATE [Product1] SET [Product_Name]= @Product_Name,[Product_Cost]= @Product_Cost where [Product_ID]= @Product_ID";
SqlCommand cmd = new SqlCommand(query, cn);
cmd.Parameters.AddWithValue("@Product_ID", id);
cmd.Parameters.AddWithValue("@Product_Name", name);
cmd.Parameters.AddWithValue("@Product_Price", cost);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Update Succesfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
cn.Close();
}
The datatype of id is char
, Product_Name is nvarchar(50)
, Porduct_Cost is bigint
.
Any thoughts I would appreciated
Upvotes: 0
Views: 5791
Reputation: 5636
It seems that you have a error in conversion, If your cost field in database is bigint then convert
string cost = txtProductCost.Text
to
Int64 cost = Convert.Toint64(txtProductCost.Text);
Int64 maps directly to BigInt.
Or if it is Double then convert
string cost = txtProductCost.Text
to
Double cost = Convert.ToDouble(txtProductCost.Text);
Hope it works for you.
Upvotes: 1
Reputation: 10573
First, Convert the string value into int
string cost = txtProductCost.Text;
costval=int.Parse(cost)// costval is integer
Next, update the database
cmd.Parameters.AddWithValue("@Product_Price", costval);
Upvotes: 0