c# - Parameter.addwithvalue - ExecuteReader: CommandText property has not been initialized -


i error executereader: commandtext property has not been initialized @ adapter.fill(ds) line. weird thing if replace @user actual string (e.g. 'name') works fine, seems broken in part sets parameter.

i've tried set string both , without ''s (i.e. @user/'@user'). i've tried using both = , like. user.identity.name.tostring() has been tested return logged in user correctly setting textbox it.

sorry non-english database variables , if question has been answered somewhere. i've given after half dozen hours of searching, though (maybe suck @ it).

relevant code:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.configuration; using system.data; using system.data.sqlclient;   public partial class bruker : system.web.ui.page { protected void page_load(object sender, eventargs e) {      string conn = configurationmanager.connectionstrings["localsqlserver"].connectionstring;     sqlconnection sql = new sqlconnection(conn);     sql.open();      sqlcommand command = new sqlcommand(conn);     command.commandtext = "select klubb.klubbnavn klubber klubb inner join user_klubber_connection conn on klubb.klubb_id = conn.klubb_id inner join aspnet_users bruker on bruker.userid = conn.userid bruker.username = @user";     command.parameters.addwithvalue("@user", user.identity.name.tostring());     command.connection = sql;     setdropdownlist(command);     dropdownlist1.selectedindex = 0;     changegridview(getmembersofclub(), sql);      sql.close();  }  protected void setdropdownlist(sqlcommand command) {      sqldataadapter adapter = new sqldataadapter(command);     sqlcommandbuilder builder = new sqlcommandbuilder(adapter);      dataset ds = new dataset();     adapter.fill(ds);      dropdownlist1.datasource = ds;     dropdownlist1.datatextfield = "klubbnavn";      dropdownlist1.databind();  } } 

edit forget following on page_load

 response.write(string.format("user name {0}",  user.identity.name)); 

and see output

its running fine

  protected void page_load(object sender, eventargs e)     {           sqlconnection sql = new sqlconnection( configurationmanager.connectionstrings["yourconnectionname"].connectionstring);         sql.open();          sqlcommand command = new sqlcommand("select * userinfo uloginid=@user", sql);         command.parameters.addwithvalue("@user", user.identity.name.tostring());         setdropdownlist(command);         dropdownlist1.selectedindex = 0;          sql.close();      }      protected void setdropdownlist(sqlcommand command)     {          sqldataadapter adapter = new sqldataadapter(command);         sqlcommandbuilder builder = new sqlcommandbuilder(adapter);          dataset ds = new dataset();         adapter.fill(ds);          dropdownlist1.datasource = ds;         dropdownlist1.datatextfield = "ufirstname";          dropdownlist1.databind();      } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -