Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Problem during MySQL in C#. I want update data in my MySQL Database.

C#
String MyCon = "SERVER=********** +
            DATABASE=asterisk +
             UID=root +
             PASSWORD=**** + Convert Zero Datetime = True";

private void btnsubmit_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection(MyCon);
            con.Open();                          
            MySqlCommand cmd = new MySqlCommand(@"Update 
userid, password, role, first_name, last_name, user_group, user_level, active 
from aster_users 
Set password=@password, role=@role, first_name=@first_name, last_name=@last_name,
user_level=@user_level, user_group=@user_group, active=@active 
where userid = userid ", con);
            cmd.ExecuteNonQuery();
            cmd.Parameters.AddWithValue("@pasword",txtconformpassword.Text );
            cmd.Parameters.AddWithValue("@role",cmbrole.Text);
            cmd.Parameters.AddWithValue("@first_name",txtfirstname.Text);
            cmd.Parameters.AddWithValue("@last_name",txtlastname.Text);
            cmd.Parameters.AddWithValue("@user_group",cmbUser1.Text);
            cmd.Parameters.AddWithValue("@user_level",cmbuser.Text);
            cmd.Parameters.AddWithValue("@active",cmbstatus.Text);
            
            MessageBox.Show("updated......");
            con.Close();
        }


help me...
Posted
Updated 16-Nov-15 20:33pm
v4
Comments
Amit Jadli 17-Nov-15 1:29am    
command execution will come after the value initialization..
so put the cmd.ExecuteNonQuery(); after adding values..like this..

cmd.Parameters.AddWithValue("@pasword",txtconformpassword.Text );
cmd.Parameters.AddWithValue("@role",cmbrole.Text);
cmd.Parameters.AddWithValue("@first_name",txtfirstname.Text);
cmd.Parameters.AddWithValue("@last_name",txtlastname.Text);
cmd.Parameters.AddWithValue("@user_group",cmbUser1.Text);
cmd.Parameters.AddWithValue("@user_level",cmbuser.Text);
cmd.Parameters.AddWithValue("@active",cmbstatus.Text);
cmd.ExecuteNonQuery();
Member 12078840/divya 17-Nov-15 1:37am    
thank you for reply now also i got that error . i think mysql is incorrect. i did not know more about sql please check it.please

ExecuteNonQuery() should be the last command:
C#
cmd.Parameters.AddWithValue("@pasword",txtconformpassword.Text );
cmd.Parameters.AddWithValue("@role",cmbrole.Text);
cmd.Parameters.AddWithValue("@first_name",txtfirstname.Text);
cmd.Parameters.AddWithValue("@last_name",txtlastname.Text);
cmd.Parameters.AddWithValue("@user_group",cmbUser1.Text);
cmd.Parameters.AddWithValue("@user_level",cmbuser.Text);
cmd.Parameters.AddWithValue("@active",cmbstatus.Text);

cmd.ExecuteNonQuery();// <---- moved to after the parameters
 
Share this answer
 
Comments
Member 12078840/divya 17-Nov-15 1:49am    
thank you for reply now also i got that error . i think mysql is incorrect. i did not know more about sql please check it.please
cut
cmd.ExecuteNonQuery();

and paste it before
MessageBox.Show("updated......");
 
Share this answer
 
As you don't say what kind of error you get I assume that this part is causing you problems.
C#
where userid = userid "

You are inserting the literal string "userid", not the value of the variable userid.

C#
string sqlCommand = string.Format(@"UPDATE 
userid, password, role, first_name, last_name, user_group, user_level, active 
FROM aster_users 
SET password=@password, role=@role, first_name=@first_name, last_name=@last_name,
user_level=@user_level, user_group=@user_group, active=@active 
WHERE userid = '{0}';", userid);

// This line can be used to check if the SQL command looks OK.
// It also makes it easy to copy the command into the SQL query tool for testing
Debug.WriteLine(sqlCommand);

MySqlCommand cmd = new MySqlCommand(sqlCommand, con);

It is not dangerous to split a long code chunk into smaller parts. Doing so makes it easier to debug the code.

Personally I find it easier to read an SQL string if I use capital letters for the SQL pre-defined words. This is just my personal taste and there are many opinions about this.
 
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