Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am using following code to connect to my database.
<pre lang="vb"> 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 dAdt As New SqlDataAdapter(sqlQry, conStr)
    Dim dSet As New DataSet()


Then filling adapter with
<pre lang="vb">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.
C#
Dim sqlQry As String = "SELECT * FROM [tblTest] 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.
Posted
Updated 9-Sep-11 0:58am
v2

There are two ways to do so :

Solution 1 :
Dim sqlQry As String = "SELECT * FROM [tblTest] WHERE [Name] = " + textBox1.text


Solution 2 :
Dim sqlQry As String = "SELECT * FROM [tblTest] WHERE [Name] = @Name"

Dim command As New SqlCommand(sqlQry, connection)

' Add Name parameter for WHERE clause.
command.Parameters.Add("@Name", SqlDbType.Varchar, 20) -- 20 Refers to the size
command.Parameters("@Name").Value = textBox1.Text


Hope this helps.
All the best.
 
Share this answer
 
Comments
Furqan Sehgal 9-Sep-11 7:23am    
First method you suggested says
Object reference not set to an instance of an object.

Now I try second
Furqan Sehgal 9-Sep-11 7:25am    
Second method gives error on command (Declaration Expected). I wonder why this happens despite we have declared it on line above.
But when I moved it to button1_click, declaration error removed but it did not work in that case. It gave scaler error on dataadapter filling statement
When you use parameters (and that's exactly what you should do) you should define a SqlCommand object separately and use that command for your data adapter. So the whole code could look something like:
VB
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 dAdt As New System.Data.SqlClient.SqlDataAdapter()
Dim dSet As New DataSet()
Dim queryCommand As New System.Data.SqlClient.SqlCommand()
Dim queryConnection As New System.Data.SqlClient.SqlConnection()

queryConnection.ConnectionString = conStr
queryConnection.Open()
queryCommand.Connection = queryConnection
queryCommand.CommandText = sqlQry
queryCommand.Parameters.AddWithValue("@Name", textBox1.Text)
dAdt.SelectCommand = queryCommand
dAdt.Fill(dSet)
queryConnection.Close()

Remember to replace textBox1.Text with your actual text box object.

Also opening the connection, executing the fill etc should be enclosed to proper try..catch block
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900