How to fix datareader problem?
-
SqlCommand cmd; SqlDataReader rdr1; int i; cmd = new SqlCommand("Select * from tblTest where [Check]='False'",con1); rdr1 = cmd.ExecuteReader(); if (rdr1.HasRows) { while (rdr1.Read()) { cmd = new SqlCommand("Insert into tblTest values('" + rdr1[0].ToString() + "','" + rdr1[1].ToString() +"','" + rdr1[2].ToString()+ "')", con2); i = cmd1.ExecuteNonQuery(); cmd = new SqlCommand("update tbltest set [check]='True' where ID='" + rdr1[0].ToString() + "'", con1); i = cmd2.ExecuteNonQuery(); } } cmd.Dispose(); rdr1.Close(); Here one datareader is already open with tblTest and inside of while loop I am trying to update records for table tblTest,Please let me know how to fix datareader problem. We can do with dataadapters but here how we can solve it?
-
SqlCommand cmd; SqlDataReader rdr1; int i; cmd = new SqlCommand("Select * from tblTest where [Check]='False'",con1); rdr1 = cmd.ExecuteReader(); if (rdr1.HasRows) { while (rdr1.Read()) { cmd = new SqlCommand("Insert into tblTest values('" + rdr1[0].ToString() + "','" + rdr1[1].ToString() +"','" + rdr1[2].ToString()+ "')", con2); i = cmd1.ExecuteNonQuery(); cmd = new SqlCommand("update tbltest set [check]='True' where ID='" + rdr1[0].ToString() + "'", con1); i = cmd2.ExecuteNonQuery(); } } cmd.Dispose(); rdr1.Close(); Here one datareader is already open with tblTest and inside of while loop I am trying to update records for table tblTest,Please let me know how to fix datareader problem. We can do with dataadapters but here how we can solve it?
-
What are you trying to do?? :omg: Selecting values from tblTest and inserting again into same table??
-
selecting values from one table like [tblOne] then matching records with other table like [tblTwo] then updating first table [tblOne].
To be honest, it is not easy to read your code. Could you clean that up a little? While cmd is the created command, cmd1 and cmd2 are executed. It's weird and does not invite someone to look into it in detail. Also, could you describe the problem and maybe some details of the goal you're trying to archive?
-
SqlCommand cmd; SqlDataReader rdr1; int i; cmd = new SqlCommand("Select * from tblTest where [Check]='False'",con1); rdr1 = cmd.ExecuteReader(); if (rdr1.HasRows) { while (rdr1.Read()) { cmd = new SqlCommand("Insert into tblTest values('" + rdr1[0].ToString() + "','" + rdr1[1].ToString() +"','" + rdr1[2].ToString()+ "')", con2); i = cmd1.ExecuteNonQuery(); cmd = new SqlCommand("update tbltest set [check]='True' where ID='" + rdr1[0].ToString() + "'", con1); i = cmd2.ExecuteNonQuery(); } } cmd.Dispose(); rdr1.Close(); Here one datareader is already open with tblTest and inside of while loop I am trying to update records for table tblTest,Please let me know how to fix datareader problem. We can do with dataadapters but here how we can solve it?
It is not the problem of DataReader. When the DataReader is opened, it will hold the connection exclusively. So you can't execute any sql commands when it opened. The solution is to use a separate connection or use a DataSet. P.S : You need to improve your coding standards.