Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store SQL select to gridview using ASP.net C#?

 SqlCommand cmd = new SqlCommand("SELECT * FROM [order] WHERE date>=@begin AND date<=@end", con);
 cmd.Parameters.AddWithValue("@begin",dt1);
 cmd.Parameters.AddWithValue("@end", dt2);

This is my select statement. I want to put the result to data gridview. How to use dataset to store in gridview in asp.net C#?

like image 847
Reynan Avatar asked Mar 21 '13 11:03

Reynan


2 Answers

Use a SqlDataAdapter.

SqlCommand cmd = new SqlCommand("SELECT * FROM [order] WHERE date>=@begin AND date<=@end", con);
cmd.Parameters.AddWithValue("@begin",dt1);
cmd.Parameters.AddWithValue("@end", dt2);
SqlDataAdapter sda = new SqlDataAdapater(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
yourGridView.DataSource = dt;
yourGridView.DataBind();
like image 106
LukeHennerley Avatar answered Sep 29 '22 06:09

LukeHennerley


You need a SqlDataReader object to execute your command and a DataTable to load the results into the GridView:

   SqlDataReader dr = cmd.ExecuteReader();
   DataTable dt = new DataTable();
   dt.Load(dr);

   gv.DataSource = dt;
   gv.DataBind();
like image 33
Darren Avatar answered Sep 29 '22 04:09

Darren