c# - UpdateCommand not working? -
so have code stores update query in string, parameter bind update query , execute , update it, code:
string query = "update users set first_name = '@firstname' id = @id"; updateuserds.updateparameters.add("id", httpcontext.current.session["columnid"].tostring()); updateuserds.updateparameters.add("firstname", txt_firstname.text); updateuserds.updatecommand = query; updateuserds.update();
however when change string query to:
string query = "update users set first_name = 'name' id = 44";
it works , updates table, guessing how have binded query, realise have gone wrong?
btw: session["columnid"] being retrieved states 44 in stack trace
remove single quotes @firstname
:
string query = "update users set first_name = @firstname id = @id"; updateuserds.parameters.addwithvalue("@firstname", first_name); updateuserds.parameters.addwithvalue("@id", httpcontext.current.session["columnid"].tostring());
edit:
assuming using sql server database try this:
sqlcommand sqlcomm = new sqlcommand(); sqlcomm.commandtext = @"update users set first_name = @firstname id = @id"; sqlcomm.parameters.add("@firstname", sqldbtype.varchar); sqlcomm.parameters["@firstname"].value = txt_firstname.text; sqlcomm.parameters.add("@id", sqldbtype.varchar); sqlcomm.parameters["@id"].value = httpcontext.current.session["columnid"].tostring(); using (sqlconnection sqlconn = new sqlconnection(connection string here);) { sqlcomm.connection = sqlconn; sqlconn.open(); sqlcomm.executenonquery(); }
Comments
Post a Comment