c# - querying column from database and bind it -


con.open();  sqlcommand com,com2; string str5 = "select count(name) count [committee].[dbo].[supervisor] ;";  com2 = new sqlcommand(str5, con);  sqldatareader reader4 = com2.executereader(); int count= 0;  if (reader4.read()) {     string s = reader4["count"].tostring();     count = int.parse(s.trim());     reader4.close();     con.close();  }   con.open();   string str = "select name [committee].[dbo].[supervisor] ;";  com = new sqlcommand(str, con);  sqldatareader reader = com.executereader();   string  [] list;  list = new string [count] ;   if (reader.read())  {      (int = 0; < count; i++)      {          list[i] = reader["name"].tostring(); // here got problem      }       reader.close();      con.close();  }   dropdownlist1.datasource = list;  dropdownlist1.databind(); 

hello guys, have code i'm retrieving column "name" database , want bind in dropdownlist1. works but, i'm getting first row column. question how save in array row row , bind correctly?

every time sqldatareader.read called advances record next row, you're calling once means each time access reader["name"] you're getting first row. instead of doing if/for should calling in while:

var list = new list<string>(); while(reader.read()) {     list.add(reader["name"].tostring()); } 

it's easier if use list<string> instead of array shown above.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -