0

i am using vb asp.net

error: there is already an open DataReader associated with this Command which must be closed first.

working on this issue for couple weeks now and i have no idea how to fix this. any help will be helpful

i am using 2 reader on page load to display some information, but each of them i am closing it.

after that i am using 3rd reader on button click, which is giving me this error

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ...
            Dim reader As SqlDataReader = cmd1.ExecuteReader()
            While reader.Read()
                ...
            End While
            reader.Close()
            reader.Dispose()

            ...
            Dim reader2 As SqlDataReader = cmd1.ExecuteReader()
            dt.Load(reader2)
            reader2.Close()
            reader2.Dispose()

            BagRepeater.DataSource = dt
            BagRepeater.DataBind()
        End If
End Sub

Public Sub CHECKOUTLB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CHECKOUTLB.Click
 Dim Order_ID As Long
            Dim InsertOrderTBSelect As String = "Select @@Identity"
            Dim InsertOrderTB = "INSERT INTO [ORDER_TB] ... "
            Dim cmd1 = New SqlCommand(InsertOrderTB, sqlConn)
            cmd1.ExecuteNonQuery()
            cmd1.CommandText = InsertOrderTBSelect
            Order_ID = cmd1.ExecuteScalar()

            Dim reader3 As SqlDataReader = cmd3.ExecuteReader()
            While reader3.Read()
                Dim InsertOrderDetailTB = "INSERT INTO [ORDER_Detail_TB] ... "
                Dim cmd4 = New SqlCommand(InsertOrderDetailTB, sqlConn)
                cmd4.Parameters.AddWithValue("@Order_ID", Order_ID)
                cmd4.Parameters.AddWithValue("@Product_ID", reader3("Product_ID"))
                cmd4.Parameters.AddWithValue("@QTY", reader3("QTY"))
                cmd4.Parameters.AddWithValue("@Color", reader3("Color"))
                cmd4.ExecuteNonQuery()
            End While
            reader3.Close()

end sub

3
Contributors
2
Replies
25
Views
6 Months
Discussion Span
Last Post by Shark_1
0

This line

Dim reader3 As SqlDataReader = cmd3.ExecuteReader()

doesn't look right to me. Where is cmd3 coming from?

0

From my opinion your error should be occurred in between these following lines.
cmd1.ExecuteNonQuery()
cmd1.CommandText = InsertOrderTBSelect

First dispose/close the sqlcommand object then use it for another sql statement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.