ispostback - C# Page Fields not populating as expected -


could please tell me doing wrong? trying call method automatically when page loads, however, not work.

protected void page_load(object sender, eventargs e) {     if (!page.ispostback)     {         customerinformation();      } }  protected void ddnames_selectedindexchanged(object sender, eventargs e) {     customerinformation(); }  private void customerinformation() {     string dbstring = configurationmanager.connectionstrings["techsupportdbconstring"].connectionstring;     string query = "select * customers name='" + ddnames.text + "'";      sqlconnection connection = new sqlconnection(dbstring);     connection.open();      sqlcommand com = new sqlcommand(query, connection);     sqldatareader reader = com.executereader();     if (reader.read())     {         lblname.text = reader["name"].tostring() + "'s personal information";         lbladdress.text = reader["address"].tostring() + "\n" + reader["city"].tostring() + " " + reader["state"].tostring() + " " + reader["zipcode"].tostring();         lblphone.text = reader["phone"].tostring();         lblemail.text = reader["email"].tostring();          reader.close();         connection.close();     } } 

ok looking through code can see connection of reader , database closed in loop should not done.

sqldatareader reader = command.executereader();     if (reader.hasrows) {      while (reader.read())      {                 //populate fields reader      } } else {      lbl.text = "no records found."; } reader.close(); connection.close(); 

next thing noticed dropdown selected text/value should either ddnames.selecteditem.text or in case of value passing ddnames.selectedvalue.

next thing make sure each reader name should match table column name.

last not least proper null check before applying tostring().if column has null value tostring() fail convert.

hope helps.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -