Reputation: 4997
I am using following code to connect to my database.
Dim conStr As String = "Data Source=.\SQLEXPRESS; ![enter image description here][1]
AttachDbFilename=|DataDirectory|\dbTest.mdf;
Integrated Security=True;
User Instance=True"
Dim sqlQry As String = "SELECT * FROM [tblTest] WHERE ([Name] = @Name)"
Dim dAdt As New SqlDataAdapter(sqlQry, conStr)
Dim dSet As New DataSet()
Then filling adapter with
dAdt.Fill(dSet, "tblTest")
and then I can use the data the way I want.
My question is: How to pass the value of parameter that user will give through a text box on my webform.
Dim sqlQry As String = "SELECT * FROM [tblTest] <b>WHERE ([Name] = @Name)
I mean how to pass the parameter value to my query?
Please modify my code to tell me how to do it.
Thanks a lot.
Upvotes: 2
Views: 5940
Reputation: 94645
You need to add parameter object and put your code in Button's Click handler or any other event you want to use. (read @cjk post).
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTest.mdf;Integrated Security=True;User Instance=True"
Dim sqlQry As String = "SELECT * FROM [tblTest] WHERE ([Name] = @Name)"
Dim Cn As New SqlConnection(conStr)
Dim SelectCmd As New SqlCommand(sqlQry, Cn)
SelectCmd.Parameters.AddWithValue("@Name", txtName.Text)
'Or
'SelectCmd.Parameters.Add("@Name",System.Data.SqlDbType.VarChar).Value=value_here
Dim dAdt As New SqlDataAdapter(SelectCmd)
Dim dSet As New DataSet
dAdt.Fill(dSet, "tblTest")
......
End Sub
Upvotes: 2
Reputation: 46425
You need to put you non-declarative (i.e. code that does stuff) into a method.
The lines at the top of your example, under the class declaration are just declarations of variables, and are set up when the class is instatiated.
For it to actually do something, there needs to be a method that is called.
Upvotes: 1