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. error "There is already an open DataReader associated with this Command which must be closed 1st."

error "There is already an open DataReader associated with this Command which must be closed 1st."

Scheduled Pinned Locked Moved ASP.NET
helpquestiondatabase
6 Posts 3 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.
  • E Offline
    E Offline
    Eunice VB junior
    wrote on last edited by
    #1

    Hi ladies and gentlmen, I got this error while doing the transactions insert command in a do loop select command. What I'm trying to do is, 1st to run select command to query filename and filesize (it consists of few results). Then with the retrieved results, rename the filename and insert into another table with the new filename. After that delete it from the original table. But i got this error "There is already an open DataReader associated with this Command which must be closed 1st". How can i overcome this?can anyone help me on this? I'm out of ideas. Thanks and appreciate... Here is my code:- cmd.Connection = cn cn.Open() tn = cn.BeginTransaction cmd.Transaction = tn strQuery2 = "SELECT FileName,FileSize FROM udtTempAttach where UniqueID = '" & Me.lblUniqueID.Text & "'" cmd.CommandText = strQuery2 dr2 = cmd.ExecuteReader Try Do While dr2.Read filename = Replace(dr2(0).ToString, ".", "_" & DistID & ".") filesize = dr2(1).ToString FileIO.FileSystem.CopyFile(ServerPath + "TempAttach\" + dr2(0).ToString, ServerPath + "Attachment\" + filename, True) If FileIO.FileSystem.FileExists(ServerPath + "Attachment\" + filename) Then FileIO.FileSystem.DeleteFile(ServerPath + "TempAttach\" + dr2(0).ToString) End If strQuery3 = "INSERT udtAttachment (DistID,FileName,FileSize,EnterDate) " strQuery3 = strQuery3 & " values('" & DistID.ToString & "','" & filename.ToString & "','" & filesize.ToString & "',getdate()) " cmd.CommandText = strQuery3 ra = cmd.ExecuteNonQuery() strQuery3 = "Delete from udtTempAttach where UniqueID='" & Me.lblUniqueID.Text & "' and filename = '" & dr2(0).ToString & "'" cmd.CommandText = strQuery3 ra = cmd.ExecuteNonQuery() Loop dr2.Close() Catch ex As Exception MsgBox(ex.Message) dr2.Close() tn.Rollback() Finally If cn.State = ConnectionState.Open Then cn.Close() End Try .......Thanks.......

    C N 2 Replies Last reply
    0
    • E Eunice VB junior

      Hi ladies and gentlmen, I got this error while doing the transactions insert command in a do loop select command. What I'm trying to do is, 1st to run select command to query filename and filesize (it consists of few results). Then with the retrieved results, rename the filename and insert into another table with the new filename. After that delete it from the original table. But i got this error "There is already an open DataReader associated with this Command which must be closed 1st". How can i overcome this?can anyone help me on this? I'm out of ideas. Thanks and appreciate... Here is my code:- cmd.Connection = cn cn.Open() tn = cn.BeginTransaction cmd.Transaction = tn strQuery2 = "SELECT FileName,FileSize FROM udtTempAttach where UniqueID = '" & Me.lblUniqueID.Text & "'" cmd.CommandText = strQuery2 dr2 = cmd.ExecuteReader Try Do While dr2.Read filename = Replace(dr2(0).ToString, ".", "_" & DistID & ".") filesize = dr2(1).ToString FileIO.FileSystem.CopyFile(ServerPath + "TempAttach\" + dr2(0).ToString, ServerPath + "Attachment\" + filename, True) If FileIO.FileSystem.FileExists(ServerPath + "Attachment\" + filename) Then FileIO.FileSystem.DeleteFile(ServerPath + "TempAttach\" + dr2(0).ToString) End If strQuery3 = "INSERT udtAttachment (DistID,FileName,FileSize,EnterDate) " strQuery3 = strQuery3 & " values('" & DistID.ToString & "','" & filename.ToString & "','" & filesize.ToString & "',getdate()) " cmd.CommandText = strQuery3 ra = cmd.ExecuteNonQuery() strQuery3 = "Delete from udtTempAttach where UniqueID='" & Me.lblUniqueID.Text & "' and filename = '" & dr2(0).ToString & "'" cmd.CommandText = strQuery3 ra = cmd.ExecuteNonQuery() Loop dr2.Close() Catch ex As Exception MsgBox(ex.Message) dr2.Close() tn.Rollback() Finally If cn.State = ConnectionState.Open Then cn.Close() End Try .......Thanks.......

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Eunice (VB junior) wrote:

      There is already an open DataReader associated with this Command which must be closed 1st".

      Use a different command object to do insertion and deletion. This error is coming because DataReader is still active and you can't use the command for other queries until you close reader. Your queries are open to SQL Injection attacks. Read about it and change to parametrized queries.

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • E Eunice VB junior

        Hi ladies and gentlmen, I got this error while doing the transactions insert command in a do loop select command. What I'm trying to do is, 1st to run select command to query filename and filesize (it consists of few results). Then with the retrieved results, rename the filename and insert into another table with the new filename. After that delete it from the original table. But i got this error "There is already an open DataReader associated with this Command which must be closed 1st". How can i overcome this?can anyone help me on this? I'm out of ideas. Thanks and appreciate... Here is my code:- cmd.Connection = cn cn.Open() tn = cn.BeginTransaction cmd.Transaction = tn strQuery2 = "SELECT FileName,FileSize FROM udtTempAttach where UniqueID = '" & Me.lblUniqueID.Text & "'" cmd.CommandText = strQuery2 dr2 = cmd.ExecuteReader Try Do While dr2.Read filename = Replace(dr2(0).ToString, ".", "_" & DistID & ".") filesize = dr2(1).ToString FileIO.FileSystem.CopyFile(ServerPath + "TempAttach\" + dr2(0).ToString, ServerPath + "Attachment\" + filename, True) If FileIO.FileSystem.FileExists(ServerPath + "Attachment\" + filename) Then FileIO.FileSystem.DeleteFile(ServerPath + "TempAttach\" + dr2(0).ToString) End If strQuery3 = "INSERT udtAttachment (DistID,FileName,FileSize,EnterDate) " strQuery3 = strQuery3 & " values('" & DistID.ToString & "','" & filename.ToString & "','" & filesize.ToString & "',getdate()) " cmd.CommandText = strQuery3 ra = cmd.ExecuteNonQuery() strQuery3 = "Delete from udtTempAttach where UniqueID='" & Me.lblUniqueID.Text & "' and filename = '" & dr2(0).ToString & "'" cmd.CommandText = strQuery3 ra = cmd.ExecuteNonQuery() Loop dr2.Close() Catch ex As Exception MsgBox(ex.Message) dr2.Close() tn.Rollback() Finally If cn.State = ConnectionState.Open Then cn.Close() End Try .......Thanks.......

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        The error message means what it says. Also, your code looks to be very insecure. Do you know what a SQL injection attack is ?

        Christian Graus Driven to the arms of OSX by Vista.

        E 1 Reply Last reply
        0
        • C Christian Graus

          The error message means what it says. Also, your code looks to be very insecure. Do you know what a SQL injection attack is ?

          Christian Graus Driven to the arms of OSX by Vista.

          E Offline
          E Offline
          Eunice VB junior
          wrote on last edited by
          #4

          Any hint to overcome the SQL injection attack? Thanks in advanced.....

          N 1 Reply Last reply
          0
          • E Eunice VB junior

            Any hint to overcome the SQL injection attack? Thanks in advanced.....

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Eunice (VB junior) wrote:

            Any hint to overcome the SQL injection attack

            First hint is to use Google. This[^] was my first hit when I searched. :)

            Navaneeth How to use google | Ask smart questions

            E 1 Reply Last reply
            0
            • N N a v a n e e t h

              Eunice (VB junior) wrote:

              Any hint to overcome the SQL injection attack

              First hint is to use Google. This[^] was my first hit when I searched. :)

              Navaneeth How to use google | Ask smart questions

              E Offline
              E Offline
              Eunice VB junior
              wrote on last edited by
              #6

              thanks..... I'm doing it now. :)

              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