Click here to Skip to main content
11,715,043 members (71,679 online)
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 1: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 Sergey Alexandrovich Kryukov 520
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2015
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