How to save sql select gridview 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 in a gridview table, how to use a dataset to store in gridview in asp.net c # help ASAP
Use the 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 this
cmd = new OleDbCommand(query, con); con.Open(); DataSet ds = new DataSet(); OleDbDataAdapter oda = new OleDbDataAdapter(cmd); oda.Fill(ds); assign datasource gridview here
GridView1.DataSource = ds.Tables[0]; and do not forget to write this piece of code
GridView1.DataBind(); con.Close(); You can use SqlDataAdaper
Represents a set of data commands and a database connection that is used to populate a DataSet and update a SQL Server database.
with DataTable
Represents a single data table in memory.
with the Gridview.DataBind() method
Binds a data source to a GridView control.
a
SqlDataAdapter da = new SqlDataAdapater(cmd, conn); DataTable dt = new DataTable(); da.Fill(dt); Gridview1.DataSource = dt; Gridview1.DataBind();