Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Guys, first ive got an error saying"There is already an open DataReader associated with this Command which must be closed first"
So I change my code I close the first reader soo here is my code now
VB
If (IsPostBack = False) Then
            Dim strSQL As String
            Dim strSQL2 As String
            Dim SQLreader As SqlClient.SqlDataReader
            Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
            strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]WHERE ([Category] = @Category)"

            connection.Open()
            Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
            myCommand.Parameters.AddWithValue("@Category", lblGEE.Text)
            SQLreader = myCommand.ExecuteReader()
            SQLreader = GridView1.DataSource
            GridView1.DataBind()
            SQLreader.Close()
            strSQL2 = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]WHERE ([Category] = @Category)"
            Dim myCommand2 As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL2, connection)
            myCommand2.Parameters.AddWithValue("@Category", lblCross.Text)
            TPCross.DataSource = myCommand2.ExecuteReader()
            TPCross.DataBind()
            connection.Close()
            connection.Close()

        End If


My problem is when i run it new error occured sayingObject reference not set to an instance of an object.
Help plss I dont know where ive got wrong.. Keep banging my head just to solved this problem.

Thanks in advance and more programming to come ^_^
Posted

found the error. The Microsoft doest support multiple readers so i close all the connections and create another. Tsk! Microsoft ^_^, makes my head crazy!!
 
Share this answer
 
Use DataTable instead of the SqlDataReader. DataTable supports your need.
 
Share this answer
 

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