我试图创建一个搜索页面,它会将结果显示在Gridview中,但是当我点击我的搜索按钮时仍然会收到以下错误:读取器关闭时无效尝试调用Read。
这是我的代码:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim connectionString As [String] = ConfigurationManager.ConnectionStrings("myDbConnectionString1").ConnectionString
Dim connection As New SqlConnection(connectionString)
connection.Open()
Dim mySQLQuery As String
Dim vID As String
vID = Request.QueryString("pgResults")
mySQLQuery = "SELECT name,address,results,url FROM myTb WHERE name LIKE '%" + TextBox1.Text + "%'"
Dim myCommand As New SqlCommand(mySQLQuery, connection)
Dim myReader1 As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While (myReader1.Read())
GridView1.DataSource = myReader1
GridView1.DataBind()
End While
panelBody.Visible = False
panelSearchResults.Visible = True
connection.Close()
End Sub
我能得到一些帮助吗?
答案 0 :(得分:2)
你不能调用Read()然后使用IDataReader
作为DataSource,让DataBinding处理它。试试这个:
Dim myReader1 As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
GridView1.DataSource = myReader1
GridView1.DataBind()