Click here to Skip to main content
Sign Up to vote bad
good
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:03

Comments
Wes Aday - 20 Sep '12 - 7:06
Yes and? The error is perfectly clear. dr is still open when you try and open it again.
neldesai - 20 Sep '12 - 8:01
thank you......

1 solution

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
Your Filters
Interested
Ignored
     
0 OriginalGriff 253
1 Rohan Leuva 220
2 Sergey Alexandrovich Kryukov 172
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,528
1 OriginalGriff 6,819
2 CPallini 3,603
3 Rohan Leuva 2,923
4 Maciej Los 2,268


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