Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlConnection CON=new SqlConnection(@"Data Source=SATEESH\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True");
CON.Open();
SqlCommand cmd=new SqlCommand("select * from register",CON);
SqlDataReader rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
while(rdr.Read())
{
  dt.Load(rdr);
}
dataGridView1.DataSource=dt;

the table contains data and I am getting the above exception.

I googled but unable to figure it out.
Posted
Updated 21-Apr-12 22:26pm
v2

Why are you using a loop at all?
The MSDN example for DataTable.Load[^] doesn't, and I can see no reason why you would want to...

C#
SqlCommand cmd=new SqlCommand("select * from register",CON);
SqlDataReader rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
dataGridView1.DataSource=dt;
 
Share this answer
 
Comments
satishmachineni 22-Apr-12 4:37am    
yes thanks ,i made changes and got desired result
OriginalGriff 22-Apr-12 5:35am    
You're welcome!
Don't see where exactly you are closing your Datareader in above code snippet but the error clearly sounds like you are trying to read data even after closing the reader.

Have a look here on how to retrieve data using DataReader here: Data Using a DataReader[^]
 
Share this answer
 
Comments
satishmachineni 22-Apr-12 4:38am    
the above loop solution solved my problem any way thanks for the answer

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