1

I want to update 4 Columns in a particular row in MySQL using C#.I want to update the

value entered in text boxes into the database if a particular field already .I am using the

following query.

 string query = " update orderform set enrolmentexpected = " +
    textBox2.Text + " stockonhand=" + textBox3.Text + " numberrequired = "
    + textBox4.Text +  " where name = " + textBox1.Text + ";";

I am getting an exception that there is some error in mysql syntax but i am not able to

find such. Is my query right or there is some syntax mistake,and is there some way for

updating multiple columns

4 Answers 4

2

You have numerous problems in that line.

Use something like this

using(MySqlConnection cn = GetConnection())
{
    cn.Open();
    string queryText = "update orderform set enrolmentexpected = ?en, stockonhand=?st, numberrequired=?num where name = ?name;";     
    MySqlCommand cmd = new MySqlCommand(queryText, cn);
    cmd.Parameters.AddWithValue("?en", textBox2.Text);
    cmd.Parameters.AddWithValue("?st", textBox3.Text);
    cmd.Parameters.AddWithValue("?num", textBox4.Text); // ?? require conversion to Int ???
    cmd.Parameters.AddWithValue("?name", textBox1.Text);
    cmd.ExecuteNonQuery();
}
Sign up to request clarification or add additional context in comments.

Comments

1
string query = " update orderform set enrolmentexpected = " +
textBox2.Text + ", stockonhand=" + textBox3.Text + ", numberrequired = "
+ textBox4.Text +  " where name = '" + textBox1.Text + "';"

guess that name is a string and other two int

Comments

0

It seems you forgot the comma ',' after an expression :

string query = " update orderform set enrolmentexpected = " + textBox2.Text + ", stockonhand=" + textBox3.Text + ", numberrequired = " + textBox4.Text + " where name = " + textBox1.Text + ";";

and quotes too :

string query = " update orderform set enrolmentexpected = '" + textBox2.Text + "', stockonhand= '" + textBox3.Text + "' , numberrequired = '" + textBox4.Text + "' where name = " + textBox1.Text + ";";

3 Comments

@ dada686 When i am entering the previous name to update other columns in row it gives an exception that unknown column name, it takes the value entered into column name as name of the column
@jaggi This error is because you forgot to put the comma and quotes, so the interpreter is confused as to who is what.
@jaggi you should look at steve's answer, it is the best way to handle this situation.
0

It seems you are not using single quotes and commas.
Use this

string query = " update orderform set enrolmentexpected = '" +
textBox2.Text + "', stockonhand='" + textBox3.Text + "', numberrequired = '"
+ textBox4.Text +  "' where name = '" + textBox1.Text + "';";

This is valid in case all the datatypes are of type char or varchar.

Comments

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.