0

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

5
  • 1
    do you have any error message receive? Commented Apr 25, 2013 at 11:43
  • 1
    what's the error msg u r getting and your cost in database is having double or int datatype. Commented Apr 25, 2013 at 11:50
  • No Error, it just doesn't work Commented Apr 25, 2013 at 11:55
  • put your senstive code inside Try, if u r not getting any error then there must be conversion error because your conversion part is outside the try,put it in inside try block for exect exception plz. Commented Apr 25, 2013 at 12:04
  • see my answer hope it works. Commented Apr 25, 2013 at 12:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

if u r using Bigint then use Convert.Toint64 instead of Convert.Toint32
0

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);

3 Comments

it gives me an Exception "Input string was not in a correct format."
I think cause it's bigInteger not int
In fact if you are using AddWithValue, you don't exclipitly convert that to an int value, because it will automatically be converted and inserted as well;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.