Click here to Skip to main content
15,887,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys, ive got a problem here. I want to set the data of two gridview where the only difference between them is parameters. my problem is ive got error when i run it.
here is my code
<pre lang="vb">If (IsPostBack = False) Then
    Dim strSQL As String
    Dim strSQL2 As String
    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)
    GridView1.DataSource = myCommand.ExecuteReader()
    GridView1.DataBind()
    myCommand.Dispose()
    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()

Ive got an error saying like this one
<br />
<br />
There is already an open DataReader associated with this Command which must be closed first.

and it points out to
VB
TPCross.DataSource = myCommand2.ExecuteReader()

what is the error? do i have a logical error?
Plss help. thanks in advance
Posted

Since this the same column you can use union all in this.

make it this way
SQL
strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]WHERE ([Category] = @Category) union all SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]WHERE ([Category] = @Category2) "

XML
<pre lang="vb">connection.Open()
Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
myCommand.Parameters.AddWithValue("@Category",lblGEE.Text)
myCommand.Parameters.AddWithValue("@Category2", lblCross.Text)
GridView1.DataSource = myCommand.ExecuteReader()
GridView1.DataBind()
myCommand.Dispose()</pre>
 
Share this answer
 
Comments
janwel 11-May-11 4:38am    
sir it says here my problem is executereader?
Well, it's because DataReaders are connected-mode, forward only thing. As long as a reader is open, connection is active and working on it. Thus if you try to use another reader then, you get the error.

Have a look at these:
MSDN blog describing the same in much more detail[^]
Workaround suggested with implication[^]
Similar discussion[^]

If needed, look here[^] for more.
 
Share this answer
 
Comments
janwel 11-May-11 4:41am    
Sir does this means ive got a syntax error? but the articles you shown has no solution(on my understanding) plss help me ill try to renew my program
Sandeep Mewara 11-May-11 4:43am    
Yes, What you did is wrong and surely you did not got the meaning form the article.

You have executed two readers at one time for the same connection which is not allowed by default. One can pass on using a attribute mentioned in the workaround link.
janwel 11-May-11 4:52am    
ok sir ill try to read it more fully(excuse my basic undstanding in english, im not used in speaking it)
Sandeep Mewara 11-May-11 5:10am    
Thats ok.

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