How to pass value to sql command in c# -
i have sql command:
string keyprocesses = "select distinct stuff((select ', '+ cn.name wmccmcategories cn inner join categorysets uc on uc.categoryid = cn.categoryid inner join keyprocesses u on u.categorysetid = uc.setid inner join companies c on c.companyid = u.companyid c.companyname = @companyname order cn.name xml path('')), 1, 1, '') liststr wmccmcategories cnn group cnn.name";
and have:
public sqlcommand sqlcmd = new sqlcommand(); sqlcmd.commandtext = commandtext; //where commandtext sql statement sqlcmd.parameters.clear();
then execute connection:
string connectionstring = configurationsettings.appsettings["connectionstring"]; using (sqlconnection connection = new sqlconnection(connectionstring)) { sqlcommand command = new sqlcommand(commandtext, connection); try { connection.open(); sqldataadapter sdr = new sqldataadapter(sqlcmd.commandtext, connection); datatable dt=new datatable(); sdr.fill(dt)
after want move next sql command , add parameter command:
sqlcmd.commandtext = keyprocesses; sqlcmd.parameters.addwithvalue("@companyname", compnyname); sqldataadapter sdr1 = new sqldataadapter(sqlcmd.commandtext, connection); datatable dtb1 = new datatable(); sdr1.fill(dtb1);
but fail execute sqlcommand
. make sure sql key process can run fix parameter pre-added in. compnname
not empty. suspect here how add value sql parameter. don't know wrong in way of using ?
edit: update whole part of code easy of investigating issue:
string connectionstring = configurationsettings.appsettings["connectionstring"]; using (sqlconnection connection = new sqlconnection(connectionstring)) { sqlcommand command = new sqlcommand(commandtext, connection); try { connection.open(); sqldataadapter sdr = new sqldataadapter(sqlcmd.commandtext, connection); datatable dt=new datatable(); sdr.fill(dt); //to store values of company names: list<companymodel> companies = new list<companymodel>(); for(int = 0; < dt.rows.count; i++) { companies.add(new companymodel { compnsn = + 1, compnname = dt.rows[i]["companyname"].tostring(), compnaddress = dt.rows[i]["address"].tostring() }); } companyrepeater.datasource = companies; companyrepeater.databind(); string comname = dt.rows[0]["companyname"].tostring(); var names = companies.select(c => c.compnname); string[] arr = names.toarray(); foreach (string compnyname in arr) { //to write names file debugging purpose using (streamwriter _testdata = new streamwriter(server.mappath("~/file.txt"), true)) { _testdata.writeline(compnyname); // write file. } //get keyprocesses sqlcmd.commandtext = keyprocesses; //sqlcmd.parameters.addwithvalue("@companyname", compnyname); sqlcmd.parameters.add("@companyname", sqldbtype.nvarchar); sqlcmd.parameters["@companyname"].value = compnyname; // sqldataadapter sdr1 = new sqldataadapter(sqlcmd.commandtext, connection); datatable dtb1 = new datatable(); //sdr1.fill(dtb1); using (var reader = sqlcmd.executereader()) { dtb1.load(reader); } companies.add(new companymodel { compnkeyprocesses = dtb1.rows[0][0].tostring() });
frankly, don't need adapter here @ all. adds confusion. remove it:
sqlcmd.commandtext = keyprocesses; sqlcmd.parameters.addwithvalue("@companyname", compnyname); using(var reader = sqlcmd.executereader()) { dtb1.load(reader); }
Comments
Post a Comment