c# - no update on sql server database. No errors just no results -
//the @question column name needs change according checkbox. example checkbox1 - question1 sqlconnection con = new sqlconnection(...); string sql = "update inquiry2 set @question = @str email = @email , base = @base;"; sqlcommand cmd = new sqlcommand(sql, con); con.open(); //checkbox2 - question 2 //if (checkbox3.checked == true) //{ // str = str + checkbox3 + 'x'; //} datatable thedatatable = null; // verify dt in session before trying if(session["dt"] != null) { thedatatable = session["dt"] datatable; } //verify data table not null if(thedatatable != null) { email = thedatatable.rows[0]["email"].tostring(); base1 = thedatatable.rows[0]["base"].tostring(); } //checkbox1 - question 1 if (checkbox9.checked == true) { str = str + checkbox9.text + 'x'; strquestone = thedatatable.columns["question1"].tostring(); } cmd.parameters.addwithvalue("@email", email); cmd.parameters.addwithvalue("@str", str); cmd.parameters.addwithvalue("@question", strquestone); cmd.parameters.addwithvalue("@base", base1); cmd.executenonquery(); con.close();
you using parameter column name. database objects (columns names, tables, stored procedures or other objects) cannot passed parameters. actual values columns or variables can parameters. need build sql statement dynamically in case:
string sql ="update inquiry2 set " + strquestone + "= @str email = ...
but should carefull because code @ risk of sql injection attack.
Comments
Post a Comment