我试图创建一个搜索,其中SQL查询取决于一个单选按钮和一个文本框。表格、文本框和单选按钮都在同一个表单上。我正在使用Microsoft Visual Studio 2012 for Web,并试图更改表单上GridView对象的数据源。然而,我一直得到错误:
ConnectionString属性未初始化
我该如何解决这个问题?下面是我的完整代码:
Protected Sub btnSearch_Click(sender As Object, e As ImageClickEventArgs) Handles btnSearch.Click
Dim sqlQuery As String = ""
If radClient.Checked = True Then
GridView1.Visible = False
sqlQuery = "SELECT * FROM Sales WHERE ClientID = '" & txtSearch.Text & "'"
ElseIf radItem.Checked = True Then
GridView1.Visible = False
sqlQuery = "SELECT * FROM Sales WHERE ItemID = '" & txtSearch.Text & "'"
ElseIf radUser.Checked = True Then
GridView1.Visible = False
sqlQuery = "SELECT * FROM Sales WHERE UserID = '" & txtSearch.Text & "'"
ElseIf radUser.Checked = False And radClient.Checked = False And radItem.Checked = False Then
ErrorLabel.Text = "Error: You have not chosen a search criteria."
Return
End If
SqlDataSource3.SelectCommand = sqlQuery
SqlDataSource3.DataBind()
End Sub
感谢@Steve, @MuhammadOmar, @Westie和@AmitApollo在我之前的线程中的代码帮助。
谢谢!
你应该这样做
创建一个全局连接变量
dim con as new SQLConnection
con.connectionsting = "Set your connection string"
con.open
当你写你的代码之后,然后写
con.close
它会解决你的问题