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
Post a Comment