Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using this code for displaying data in Gridview:
C#
SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
SqlCommand cmd = new SqlCommand("select * from product_order", con);
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();

But, I am unable to access data in grid.
Posted
Updated 13-Sep-10 23:22pm
v2
Comments
Sandeep Mewara 14-Sep-10 5:23am    
Edited for grammar and code formatting.

You need to read about ADO.NET. How DataReader works.
Instead of DataReader, you should use DataAdapter to fill dataset and then datagrid.

Have a look here:
Populating a DataSet from a DataAdapter (ADO.NET)[^]

A good read on ADO.NET with samples here:
ADO.NET Overview[^]
Accessing Data with ADO.NET[^]
 
Share this answer
 
Using DataTable (or DataSet if multiple tables are needed to select) and bind the datatable to the Grid
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
     SqlCommand cmd = new SqlCommand("select * from product_order", con);
     SqlDataAdapter sda = new SqlDataAdapter(cmd);
     DataTable dt = new DataTable();
     cmd.Connection = con;
     con.Open();

     sda.Fill(dt, "product_order");
     GridView1.DataSource = dt;
     GridView1.DataBind();
     con.Close();

Hope this will help you :)
 
Share this answer
 
Hi
In your code, try adding reader.read() after SqlDataReader reader = cmd.ExecuteReader() line.

SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
SqlCommand cmd = new SqlCommand("select * from product_order", con);
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

reader.read();

GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();


This might help you.

Regards,
Suresh
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900