Click here to Skip to main content
15,923,852 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
I get this error when i run my vb.net program. I am unable to understand what causes this error.
The error points to dr1= cmd2.executereader in line no 113.

This is my coding.

VB
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()
' [error "Invalid attempt to call metadata when reader is closed" in next line -- SA]:
                dr1 = cmd2.ExecuteReader
                If dr1.HasRows = True Then
                    While dr1.Read
                       <big>Me.txtcoursefees.Text = dr(0)</big>
                    End While
                End If
                dr1.Close()
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    End Sub


Please Help.. I want this to be solved as this is a coding from my final year project
Posted
Updated 2-May-13 10:53am
v2
Comments
Sergey Alexandrovich Kryukov 2-May-13 16:51pm    
How can we know which line is #113?! Comment it in code.
—SA
ZurdoDev 2-May-13 16:51pm    
It may be that since you closed dr it closed the connection? Move dr.Close() to the end with dr1.Close().

1 solution

You're getting yourself in trouble by trying to reuse a connection. Don't. What you did was try to run 2 readers on the same connection, but one after the other. When you closes the first reader, it also closed the connection, preventing you from running the second reader on it.

You're checking to see if the connection is Closed. The problem with that is there are more states other than Open and Closed, such as Connecting, Fetching, Executing, ... The only time you can execute a reader is if the state is Open.

But, don't even bother checking the conenction state.

Create a new connection object every time you make a query to the database. Do not attempt to reuse a "shared" connection. You'll just end up in the position that you are in now and another nasty little problem to debug.
 
Share this answer
 
Comments
Dave Kreskowiak 2-May-13 18:15pm    
@ Dave Kreskowiak....
I hv created a module for connection string in which i have created a sub named connection. i call this sub wherever i need to call the connectionstring and con.open() and i directly proceed to write sqlquery. So are u suggesting me to explicitly define a connection string by using the following methods
dim con as sqlconnection
con = new sqlconnection
con.connectionstring = " ConnectionString Here"
con.open().

r u suggesting This one..
Dave Kreskowiak 2-May-13 18:17pm    
Don't post your responses as another solution.

You can have a method create and return a connection object. You don't have to specify the conneciton string every time you want to use a connection. It's in one place.

Public Function GetConnection() As SqlConnection
Dim newConn As New SqlConnection(GetConnectionString())
return newConn
End Function

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