Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to use the following query
VB
Dim sqlQry As String = "SELECT * FROM tblTest where Name=@NM and Rank=@RN"
    
Then I fill my dataadapter by
Dim dAdt As New SqlDataAdapter(sqlQry, conStr)

But donot know where to put the parameters that I have set after where clause.

Thanks
Posted
Updated 6-Sep-11 5:41am
v2

Instead of passing the SQL string to the adapter create a SqlCommand[^], define the SqlParameters[^] and use that command for your adapter. When doing this you also have to create the SqlConnection.

So something like:
VB
Dim databaseConnection As New SqlConnection
Dim queryCommand As New SqlCommand

databaseConnection.ConnectionString = conStr
databaseConnection.Open
queryCommand.CommandText = "SELECT * FROM tblTest where Name=@NM and Rank=@RN"
queryCommand.Parameters.AddWithValue("@NM", someNMvariable)
queryCommand.Parameters.AddWithValue("@RN", someRNvariable)
queryCommand.Connection = databaseConnection
Dim dAdt As New SqlDataAdapter(queryCommand)
...
databaseConnection.Close
 
Share this answer
 
Comments
Sander Rossel 6-Sep-11 13:49pm    
My 5 for the correct and clear answer, except of course select * is quite a coding horror... :)
Wendelius 6-Sep-11 14:44pm    
Thanks. Yep, that's a real horror :)
 
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