Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to fix datareader problem?

How to fix datareader problem?

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestionannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sr Frank
    wrote on last edited by
    #1

    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?

    M K 2 Replies Last reply
    0
    • S Sr Frank

      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?

      M Offline
      M Offline
      m dhu
      wrote on last edited by
      #2

      What are you trying to do?? :omg: Selecting values from tblTest and inserting again into same table??

      S 1 Reply Last reply
      0
      • M m dhu

        What are you trying to do?? :omg: Selecting values from tblTest and inserting again into same table??

        S Offline
        S Offline
        Sr Frank
        wrote on last edited by
        #3

        selecting values from one table like [tblOne] then matching records with other table like [tblTwo] then updating first table [tblOne].

        M 1 Reply Last reply
        0
        • S Sr Frank

          selecting values from one table like [tblOne] then matching records with other table like [tblTwo] then updating first table [tblOne].

          M Offline
          M Offline
          michaelschmitt
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Sr Frank

            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?

            K Offline
            K Offline
            Kannan Ar
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups