Question Invalid Attempt To Call Metadata When Reader Is Closed

satishkrshnn

New member
Joined
May 2, 2013
Messages
2
Location
India
Programming Experience
Beginner
I get an error " INVALID ATTEMPT TO READ METADATA WHEN READER IS CLOSED" when i run my program.
Plz help me solve this problem.

Below is my Code.
    Private Sub cmbstudentid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbstudentid.SelectedIndexChanged
        Try
            Call connection()
            sqlstr = "select _Name,_course_enro from Admission__details where _Eno like '%" & cmbstudentid.SelectedItem & "'"
            cmd = New SqlCommand(sqlstr, con)
            dr = cmd.ExecuteReader
            While dr.Read
                Me.txtname.Text = dr(0)
                Me.txtcourseenrolled.Text = dr(1)
                sqlstr2 = "select Cfees from Course_details where Cname like '%" & txtcourseenrolled.Text & "'"
                Dim cmd2 = New SqlCommand(sqlstr2, con)
                If con.State = ConnectionState.Closed Then
                    con.Open()
                End If
                dr.Close()
                dr1 = cmd2.ExecuteReader
                If dr1.HasRows = True Then
                    While dr1.Read
                        Me.txtcoursefees.Text = dr(0)
                    End While
                End If
                dr1.Close()
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    End Sub
 
Last edited by a moderator:
Well, the fact that you're calling Close on a data reader inside a While loop that's based on that data reader's Read method could be an issue. If you want to keep reading from the data then the data reader can't be closed. You need to call Close after the loop is finished. To avoid such issues, always use a Using block, e.g.
Using reader = command.ExecuteReader()
    '...
End Using
The 'reader' variable only exists within the using block and the data reader is implicitly closed at the End Using line so there can be no issues.
 
Solved....

jmcilhinney
Well, the fact that you're calling Close on a data reader inside a While loop that's based on that data reader's Read method could be an issue. If you want to keep reading from the data then the data reader can't be closed. You need to call Close after the loop is finished. To avoid such issues, always use a Using block, e.g.
1
2
3​
Using reader = command.ExecuteReader()
'...
End Using


The 'reader' variable only exists within the using block and the data reader is implicitly closed at the End Using line so there can be no issues.​

jmcilhinney i thank u first for suggesting me something new of which i had no idea about.
secondly ur assumption was wrong. I tried ur suggestion but it ran into an error " There is already an open connection associated With this reader which must be Closed First" . Upon finding this error, i got the answer for my error because (dr) was not closed before executing (dr1). So i closed it.
Finnaly The reason why i got this error " Invalid Attempt To Call Metadata When Reader Is Closed" is because of the line which is highlighted in bold and underlined.
Private Sub cmbstudentid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbstudentid.SelectedIndexChanged
Try
Call connection()
sqlstr = "select _Name,_course_enro from Admission__details where _Eno like '%" & cmbstudentid.SelectedItem & "'"
cmd = New SqlCommand(sqlstr, con)
dr = cmd.ExecuteReader
While dr.Read
Me.txtname.Text = dr(0)
Me.txtcourseenrolled.Text = dr(1)
sqlstr2 = "select Cfees from Course_details where Cname like '%" & txtcourseenrolled.Text & "'"
Dim cmd2 = New SqlCommand(sqlstr2, con)
If con.State = ConnectionState.Closed Then
con.Open()
End If
dr.Close()
dr1 = cmd2.ExecuteReader
If dr1.HasRows = True Then
While dr1.Read
Me.txtcoursefees.Text = dr(0)
End While
End If
dr1.Close()
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
I got the error because i should have used dr1(0) instead of dr(0).
Now This problem is solved.
 

Latest posts

Back
Top