c# - Must declare a scalar variable -
i getting exception "must declare scalar variable"@straccountid"
string @straccountid = string.empty; ssql = "select gub.btn, gup.cust_username, gup.email gbs_user_btn gub, gbs_user_profile gup gub.cust_uid = gup.cust_uid , gub.btn = '@straccountid' order create_date desc" @straccountid = straccountid.substring(0, 10);
code running query against db
try { ocn = new sqlconnection(configurationsettings.appsettings["gbregistrationconnstr"].tostring()); ocn.open(); ocmd = new sqlcommand(); ocmd.parameters.addwithvalue("@straccountid", straccountid); ocmd.commandtext = ssql; ocmd.connection = ocn; ocmd.commandtype = commandtype.text; odr = ocmd.executereader(commandbehavior.closeconnection);
i declared variable. there flaw in query?
first off bat rid of these 2 lines:
string @straccountid = string.empty; @straccountid = straccountid.substring(0, 10);
and try code:
string straccountid = "a1234"; //create variable , assign value string acctid = straccountid.substring(0, 10); ocn = new sqlconnection(configurationsettings.appsettings["gbregistrationconnstr"].tostring()); ocn.open(); ocmd = new sqlcommand(); ocmd.commandtext = ssql; ocmd.connection = ocn; ocmd.commandtype = commandtype.text; ocmd.parameters.add("straccountid", acctid); //<-- forgot add in parameter odr = ocmd.executereader(commandbehavior.closeconnection);
here link on how create parametized query: http://www.dotnetperls.com/sqlparameter
Comments
Post a Comment