Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
here i am getting this error
 

System.invalidOperationException: There is already an open Reader that must be closed first
The code is:
 
            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 20-Sep-12 2:03am
Comments
Wes Aday at 20-Sep-12 7:06am
   
Yes and? The error is perfectly clear. dr is still open when you try and open it again.
neldesai at 20-Sep-12 8:01am
   
thank you......

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check the underlined solution.
 
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).
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100