=@begin AND date< =@end ", con); cmd....">

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

+4
source share
5 answers

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(); 
+6
source

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(); 
+3
source

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(); 
+2
source

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(); 
+2
source

You can use SQLDataAdapter for this.

 SqlDataAdapter adp=new SqlDataAdapter(cmd); Datatable dt=new Datatable(); adp.fill(dt); gridview.Datasource=dt; gridview.Databind(); 
+2
source

All Articles