2使用相同的查询获取数据时出现Gridview错误 [英] 2 Gridview error on getting data with the same querry
本文介绍了2使用相同的查询获取数据时出现Gridview错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里遇到了问题.我想设置两个gridview的数据,它们之间的唯一区别是参数.我的问题是我运行时出现错误.
这是我的代码
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.
并指出
TPCross.DataSource = myCommand2.ExecuteReader()
有什么错误?我有逻辑错误吗?
请帮助.在此先感谢
what is the error? do i have a logical error?
Plss help. thanks in advance
推荐答案
由于此列相同,因此可以在其中使用并集.
用这种方式实现
Since this the same column you can use union all in this.
make it this way
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) "
<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>
好吧,这是因为DataReader是连接模式,只能转发内容.只要阅读器处于打开状态,连接就处于活动状态并正在工作.因此,如果您尝试使用其他阅读器,则会出现错误.
看看这些:
^ ]
^ ]
类似的讨论 [此处 [
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.
这篇关于2使用相同的查询获取数据时出现Gridview错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文