Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
Hi guys,
I'm having 2 db table(TableOne,TableTwo). I need to compare whether the Code in TableOne available in TableTwo. Below is how I created the command..guess it is wrong.
SqlCommand cmdOne = new SqlCommand("select Code from TableOne", conn);
rdrOne = cmdOne.ExecuteReader();

SqlCommand cmdTwo = new SqlCommand("select ID from TableTwo", conn);
rdrTwo = cmdTwo.ExecuteReader();

while (rdrOne.Read())
{
     string Code1 = (string)rdrOne["Code"];
     while (rdrTwo.Read())
     {
         string Code2 = (string)rdrTwo["ID"];
     }
}

I'm getting error at the bold line :
"There is already an open DataReader associated with this Command which must be closed first."

Could anyone help me out?

Skunkhead :)
Posted

you have first close your first reader then only you can use the second one ......

The reason behind this is,only one datareader per connection can be active at the same time.
So, you will have to create not only another reader but also another
connection to use two of them at the same time.

Instead of that what you are doing you can use DataSet and Retrieve both table into it....

And then simply compare both table values like,

ds.Table[0].rows[0]["Column_name"] == ds.Table[1].rows[0]["Column_name"]


I hope this will help you.
 
Share this answer
 
v3
hello friend as pratik said. better go for dataset or join is the better way to do these things... hope this one will help u
try dis
 
Share this answer
 
Well, it's because DataReaders are connected-mode, forward only thing. As long as a reader is open, connection is active and working on it. Thus if you try to use another reader then, you get the error.

Have a look at these:
MSDN blog describing the same in much more detail[^]
Workaround suggested with implication[^]
Similar discussion[^]

If needed, look here[^] for more.
 
Share this 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