c# - listbox not showing any results -


i want listbox in visual studio list courses in course table (from database) application runs fine listbox remains empty code i'm using:

  protected void listbox1_selectedindexchanged(object sender, eventargs e)     {         string sqlcon = "data source = localhost; persist security info = true; user id = localhost; password = ****; unicode = true";          oracleconnection con = new oracleconnection(sqlcon);         con.open();         oraclecommand cmd = new oraclecommand();         cmd.connection = con;         cmd.commandtext = "select coursename course";         cmd.commandtype = commandtype.text;         oracledatareader dr = cmd.executereader();         dr.read();          while(dr.read())         {             listbox1.items.add(dr.getstring(1));         }     } 

there @ leat 3 errors in code , logic problem.

errors first:

  • you call read without doing record loaded read.
  • when reach loop code have loaded first record, read inside while try load second record. if there 1 record loop never runs , there no items in list
  • you call getstring(1) retrieve coursename table , give exception index out of range. arrays start @ index 0 coursename need getstring(0)

    // not needed, let loop run // dr.read(); while(dr.read()) {     // first column @ index 0      listbox1.items.add(dr.getstring(0)); } 

finally logic problem. seems use selectedindexchanged event fill items of same list triggers selectedindexchanged. seems illogic because have items in list , selecting 1 triggered event. code should run somewhere else (the load event?) trying here? anyway, if want keep code should @ least clearing items collection before loading items database otherwise, @ each selectedindexchanged add set of item identical loaded one.

  // before filling items clear previous ones   listbox1.items.clear(); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -