Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here i am getting this error


System.invalidOperationException: There is already an open Reader that must be closed first

The code is:
VB
Dim temp As Integer = 0
str = "select CaseId  from New_Case_Id_Master where CaseId >" & temp & ""
cmd = New SqlCommand(str, cn.getConnection())
dr = cmd.ExecuteReader()
If dr.Read = False Then
    Dim s_temp As String
    s_temp = temp_no + "000001"
    s_no = Val(s_temp)

Else



    str = "select max(CaseId) from New_Case_Id_Master"
    cmd = New SqlCommand(str, cn.getConnection())
    dr = cmd.ExecuteReader()'showing this line number in error
    If dr.Read = True Then
        Dim s_no_temp, s_no_temp1 As String
        Dim no_temp As Integer
        no_temp = dr.Item(0)
        s_no_temp = no_temp.ToString

        s_no_temp1 = s_no_temp.Substring(0, 4)
        s_no = Val(s_no_temp1)

        If temp_no > s_no Then
            s_no = Val(temp_no.ToString + "000001")
        Else
            s_no = no_temp + 1
        End If
    End If

End If
Posted
Comments
[no name] 20-Sep-12 7:06am    
Yes and? The error is perfectly clear. dr is still open when you try and open it again.
neldesai 20-Sep-12 8:01am    
thank you......

1 solution

Check the underlined solution.

VB
Dim temp As Integer = 0
str = "select CaseId  from New_Case_Id_Master where CaseId >" & temp & ""
cmd = New SqlCommand(str, cn.getConnection())
dr = cmd.ExecuteReader()
If dr.Read = False Then
    Dim s_temp As String
    s_temp = temp_no + "000001"
    s_no = Val(s_temp)

Else
    str = "select max(CaseId) from New_Case_Id_Master"
    cmd = New SqlCommand(str, cn.getConnection())
    dr.Close();
    dr = cmd.ExecuteReader()'showing this line number in error
    If dr.Read = True Then
        Dim s_no_temp, s_no_temp1 As String
        Dim no_temp As Integer
        no_temp = dr.Item(0)
        s_no_temp = no_temp.ToString

        s_no_temp1 = s_no_temp.Substring(0, 4)
        s_no = Val(s_no_temp1)

        If temp_no > s_no Then
            s_no = Val(temp_no.ToString + "000001")
        Else
            s_no = no_temp + 1
        End If
    End If

End If



Hope you are using the dr outside this function. If not, please close that data reader (dr) before exiting the function (as it seems member/global variable).
 
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