Invalid attempt to FieldCount when reader is closed

  • Thread starter Thread starter mark.norgate
  • Start date Start date
M

mark.norgate

Hi
I have a repeater control nested within another repeater control, and
both the repeaters have a connection to the database.
I'm having problems because the application complains that "Invalid
attempt to FieldCount when reader is closed", even though I'm opening
and closing connections in the right order. Can anyone shed some light
on this? Code here:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim connection As New
SqlConnection(ConfigurationSettings.AppSettings("strDBConnect"))
Dim command As New SqlCommand("usp_JobTypes_Get", connection)
command.CommandType = CommandType.StoredProcedure

connection.Open()

Dim jobTypeDataReader As SqlDataReader = command.ExecuteReader
JobGroups.DataSource = jobTypeDataReader
JobGroups.DataBind()

connection.Close()
End Sub
Sub JobGroups_ItemCreated(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs) Handles JobGroups.ItemCreated
Dim JobsRepeater As Repeater = e.Item.FindControl("Jobs")

Dim connection As New
SqlConnection(ConfigurationSettings.AppSettings("strDBConnect"))
Dim command As New SqlCommand("usp_Jobs_Get", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@JobType", 20)

connection.Open()

Dim jobDataReader As SqlDataReader = command.ExecuteReader
JobsRepeater.DataSource = jobDataReader
JobsRepeater.DataBind()

connection.Close()
End Sub

Thanks, Mark
 
Hi
I have a repeater control nested within another repeater control, and
both the repeaters have a connection to the database.
I'm having problems because the application complains that "Invalid
attempt to FieldCount when reader is closed", even though I'm opening
and closing connections in the right order. Can anyone shed some light
on this? Code here:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim connection As New
SqlConnection(ConfigurationSettings.AppSettings("strDBConnect"))
        Dim command As New SqlCommand("usp_JobTypes_Get", connection)
        command.CommandType = CommandType.StoredProcedure

        connection.Open()

        Dim jobTypeDataReader As SqlDataReader = command.ExecuteReader
        JobGroups.DataSource = jobTypeDataReader
        JobGroups.DataBind()

        connection.Close()
    End Sub
    Sub JobGroups_ItemCreated(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs) Handles JobGroups.ItemCreated
        Dim JobsRepeater As Repeater = e.Item.FindControl("Jobs")

        Dim connection As New
SqlConnection(ConfigurationSettings.AppSettings("strDBConnect"))
        Dim command As New SqlCommand("usp_Jobs_Get", connection)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.Add("@JobType", 20)

        connection.Open()

        Dim jobDataReader As SqlDataReader = command.ExecuteReader
        JobsRepeater.DataSource = jobDataReader
        JobsRepeater.DataBind()

        connection.Close()
    End Sub

Thanks, Mark

Not to worry, found a solution now.

Mark
 
Back
Top