Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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. enter image description here

share|improve this question
1  
what did you try and what was your results ? –  Steve B Sep 9 '11 at 9:33
 
I could not try anything...... No hint where to begin –  Furqan Sehgal Sep 9 '11 at 9:47
 
My friend it is easy to say for you. But where I live there are no courses or books available. I only have internet as source of learning –  Furqan Sehgal Sep 9 '11 at 11:20
 
@Furqan I have given you many links to websites in previous questions which have the answers in them. Have you not read them? –  Tim B James Sep 9 '11 at 13:07
add comment

2 Answers

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
share|improve this answer
 
It says declaration expected on SelectCmd, despite it has been declraed on the upper line SelectCmd.Parameters.AddWithValue("@Name",value_here) –  Furqan Sehgal Sep 9 '11 at 10:48
 
I have added my question showing the pic. with error. Please check that. Thanks a lot for your help. –  Furqan Sehgal Sep 9 '11 at 11:41
 
Which ones? can you suggest how? –  Furqan Sehgal Sep 9 '11 at 12:01
 
let us continue this discussion in chat –  Furqan Sehgal Sep 9 '11 at 16:12
add comment

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.

share|improve this answer
 
Any examples sir ? –  Furqan Sehgal Sep 9 '11 at 12:01
 
No, you need to learn how basic program structures work first. –  cjk Sep 9 '11 at 12:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.