already an open datareader?

dualshock03

Well-known member
Joined
Jan 28, 2007
Messages
105
Programming Experience
1-3
how to fix this code??

Im always getting an error: " there's already an open datareader associated with this connection which must be close first!"


VB.NET:
Private Sub btn_VCR_generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_VCR_generate.Click
        con1.Open()
        Try

            comDate.CommandText = " SELECT * from tblpersonaldata where dateofbirth > '" & dfromvalue.Value & "' AND dateofbirth < '" & dtovalue.Value & "'"
            comDate.Connection = con1
            SearchAdap.SelectCommand = comDate
            readate = comDate.ExecuteReader
            While readate.Read
                txtsusId.Text = (readate.GetValue(readate.GetOrdinal("accussed_No")))
                txtfulname.Text = (readate.GetValue(readate.GetOrdinal("accusedname")))

            End While

            dtaSet.Clear()
            dgrid_view.AutoGenerateColumns = False
            dteAdap = New MySql.Data.MySqlClient.MySqlDataAdapter("select * from tblpersonaldata", con1)
            dteAdap.Fill(dtaSet, "tblpersonaldata")

            DataRecord.Table = dtaSet.Tables("tblpersonaldata")
            totalcnt = DataRecord.Count

            dgrid_view.DataSource = dtaSet.Tables("tblpersonaldata")
            lbltotal.Text = totalcnt

        Catch SQL_err As MySql.Data.MySqlClient.MySqlException
            MsgBox("Error searching ha database" & SQL_err.Message)
        End Try
        con1.Close()
    End Sub
 
Ei can you give a code sample for the second option that you've mention, what will be the structure for it? how to Load the DataReader into a DataTable first?, and then looping through the rows of the Datatable...
 
Ei can you give a code sample for the second option that you've mention, what will be the structure for it? how to Load the DataReader into a DataTable first?, and then looping through the rows of the Datatable...
VB.NET:
Dim table As New DataTable

Using reader As SqlDataReader = command.ExecuteReader()
    table.Load(reader)
End Using

For Each row As DataRow In table.Rows
    'Use row here.
Next
The DataReader is closed implicitly at the End Using line so it is not tying up the connection when you loop through the data.
 

Latest posts

Back
Top