Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is there anyway i can pass in the username, password, and id as parameters
into a query ?

I created a method to do that:

and tried to call it

this.Update("jen", "779", "55") ;

but i am getting :


C#
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

Additional information: Unknown column 'user' in 'where clause'




at the line


C#
cmd.ExecuteNonQuery();




C#
public void Update(string user, string passw, string ID)
     {


         string query = "UPDATE users SET username= user, password= passw, id= ID  WHERE username= user";

       //string query = "UPDATE users SET username='Joe', password='22', id='33'  WHERE username='Joe'";

 //Open connection
 if (OpenConnection() == true)
 {
     //create mysql command
     MySqlCommand cmd = new MySqlCommand();
     //Assign the query using CommandText
     cmd.CommandText = query;
     //Assign the connection using Connection
     cmd.Connection = connection;

     //Execute query
     cmd.ExecuteNonQuery();

     //close connection
     this.CloseConnection();
 }



     }


What I have tried:

*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Posted
Updated 6-Nov-16 2:24am

There are 2 things amiss here.

  • Normally (depending on which database engine you're using), parameter names are prefixed in the query string with an "@". It looks like you're MySQL there, and this is the case for that database engine. This means your query becomes
    SQL
    UPDATE users SET username= @user, password= @passw, id= @ID WHERE username= @user
  • You need to add the parameters to the DbCommand object
    C#
    cmd.Parameters.AddWithValue("@user", user);
    cmd.Parameters.AddWithValue("@passw", passw);
    cmd.Parameters.AddWithValue("@ID", ID);
    ...
    cmd.ExecuteNonQuery();
 
Share this answer
 
Comments
Midi_Mick 6-Nov-16 6:17am    
Just a quick observation: Why Update username when it hasn't changed - if it had, the WHERE clause would fail?
Your query string is incorrect. Why?
Because
username= user
is sent to the server exactly as you enter it. 'user' is not replaced by its value and is consider, in fact, a column name in many version of sql.


You need to build the string so that the values are substituted in the string. For example,
username='{0}'
in your string, where user is it's value. For the rest of how to use this, see See C# string functions.

Remember to include single quotes around strings. on the format side of your string as you need them passed to the sql execution.
 
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