Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am self learning C#. When I try to update locations table as follows, only first field is updating.

<pre>if (dt.Rows.Count > 0)
            {
                
                MySqlCommand cmd = new MySqlCommand("UPDATE `locations` SET `lname`=@loca, `laddress`=@addr, `ltelephone`=@tele WHERE 'lcode'=@lcode", myDb.getConnection);

                cmd.Parameters.Add("@loca", MySqlDbType.VarChar).Value = txtName.Text;
                cmd.Parameters.Add("@addr", MySqlDbType.VarChar).Value = txtAddress.Text;
                cmd.Parameters.Add("@tele", MySqlDbType.VarChar).Value = txtTel.Text;
                cmd.Parameters.Add("@lcode", MySqlDbType.VarChar).Value = lCode;

                myDb.openConnection();

                cmd.ExecuteNonQuery();

           //     if (cmd.ExecuteNonQuery() == 1)
           //   {
                myDb.closeConnection();

              //  }
                //else
                //{
                //    MessageBox.Show("Update Error");
                //}
            }




lCode is a public variable.

What I have tried:

I tried in different ways. but only first field is updating. Can anyone help me?
Posted
Updated 9-Jul-21 22:26pm
Comments
Richard MacCutchan 10-Jul-21 4:15am    
What are the values before and after the update? And why are you not checking the return value of the ExecuteNonQuery command?

1 solution

It's very unlikely than that code will update only one field - people would have noticed long ago - so its more likely that that code isn't getting executed, nothing is getting changed, or the text values you send aren't what you think they should be.

So step one is to identify exactly what is happening.

Start with the debugger: put a breakpoint on the line which creates the MySqlCommand, and make sure your code hits it.
If it doesn't - that's your problem.
If it does, use your DB manager to look at the table content before that code runs: write a query to return all rows that match your code criteria.
Then look at exactly what is in the three TextBoxes and the lCode: if necessary use the debugger to change the text to something really noticeable!
Now step your code. Just before the ExecuteNonQuery, run your DB manager query again to be sure the rows are the right ones and you know exactly what they contain.
Execute that line.
Run your manager query again: what changed?

We can't do any of that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900