VB.NET & SQL:ConnectionString 属性尚未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15528482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET & SQL : The ConnectionString property has not been initialized
提问by Fares K. A.
I'm trying to create a search, where the SQL query depends on a radio button and a textbox. The table, textbox and radio button are all on the same form. I'm using Microsoft Visual Studio 2012 for Web, and trying to change the data source for a GridView object on the form. However, I keep getting the error:
我正在尝试创建一个搜索,其中 SQL 查询取决于一个单选按钮和一个文本框。表格、文本框和单选按钮都在同一个表单上。我正在使用 Microsoft Visual Studio 2012 for Web,并尝试更改表单上 GridView 对象的数据源。但是,我不断收到错误消息:
The ConnectionString property has not been initialized.
ConnectionString 属性尚未初始化。
How can I fix this? Below is my full code.
我怎样才能解决这个问题?下面是我的完整代码。
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
Credits to @Steve, @MuhammadOmar, @Westie and @AmitApollo for help with the code in my previous thread.
感谢@Steve、@MuhammadOmar、@Westie 和@AmitApollo 对我之前线程中的代码的帮助。
Thank you!
谢谢!
采纳答案by SMHasnain
You should do it like
你应该这样做
make a global connection variable
创建一个全局连接变量
dim con as new SQLConnection
con.connectionsting = "Set your connection string"
con.open
after when you write what your code then write
之后当你写你的代码然后写
con.close
it will solve your problem
它会解决你的问题
help it helps.
帮助它有帮助。

