Invalid attempt to FieldCount when reader is closed

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
 
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

Not to worry, found a solution now.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top