c# - Need help taking the results of a SQL select statment and outting them into a gridview -
try { sqlcommand cmd = new sqlcommand(select [name], count([name]) count [delegationtracker] [date] = @date group [name], connection);conn); sqlcommand cmd = new sqlcommand(select @user_tested whmis_user_tests;, connct); cmd.parameters.clear(); cmd.parameters.add(@name, sqldbtype.varchar).value = httpcontext.current.session[name].tostring(); cmd.parameters.add(@date, sqldbtype.varchar).value = date;mmddyyyy cmd.parameters.add(@grade, sqldbtype.varchar).value = null; connection.open(); using (sqldatareader reader = cmd.executereader()) { while (reader.read()) { test = reader.getvalue(0).tostring(); test2 = reader.getvalue(1).tostring(); } } sqldatareader dr = cmd.executereader(); sqldataadapter adapter = new sqldataadapter(cmd); datatable dt = new datatable(); dt.load(dr); adapter.fill(dt); dataset ds = new dataset(); adapter.fill(ds); maingridview.datasource = dt; maingridview.datasource = ds; maingridview.databind(); connection.close(); return true; }
the gridview not display im assuming because has no data , datatable returns 1 record in sql table not display (within gridview) dataset far can tell returns nothing
the reason have many adapters , dataset , datatable testing see worked none have produced results...
i'm not sure whats going on in sample code. looks took 2 different methods , shuffled them together. here's simple method or loading datagrid using 1 of 2 sql queries provided.
private void loaddatagrid(datagrid datagrid, datetime date, string connectionstring) { using (var conn = new sqlconnection(connectionstring)) { using (sqlcommand cmd = conn.createcommand()) { cmd.parameters.add(new sqlparameter { parametername = "@date", sqldbtype = sqldbtype.datetime, value = date }); cmd.commandtext = "select [name], " + " count([name]) count " + "from [delegationtracker] " + "where [date] = @date " + "group [name]"; conn.open(); try { using (sqldatareader reader = cmd.executereader()) { datagrid.datasource = reader; datagrid.databind(); } } { conn.close(); } } } }
Comments
Post a Comment