Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
removed from here .....................

What I have tried:

VB
Dim strSql As String = GetCaseSQL("12345")
Posted
Updated 9-Nov-16 3:36am
v3
Comments
Member 11403304 9-Nov-16 8:40am    
I am so lost here. So what I am trying to do before I move forward is test if my sql works.
When for example I test like this Dim strSql As String = GetCaseSQL("5678")
the results is SET @PartyID = 16482594
I would like the result to be SET @PartyID = 5678.
So the result should always be whatever is in the Dim strSql As String = GetCaseDQL("Whatever is in here should be returned as @PartyID")

1 solution

Remove the first two lines:
VB.NET
"DECLARE @PartyID INT  " + vbCrLf + _
"SET @PartyID = 16482594 " + vbCrLf + _

Then add your PartyID as a parameter to the SqlCommand when you actually use it:
VB
Using con As New SqlConnection(strConnect)
    Dim myValue As Integer = 12345
	con.Open()
	Using cmd As New SqlCommand(strSQL, con)
        cmd.Parameters.AddWithValue("@PartyID", myValue)
		Using reader As SqlDataReader = cmd.ExecuteReader()
			While reader.Read()
				...
			End While
		End Using
	End Using
End Using
Don't pass the string value to your method and try to concatenate it - that way is far too dangerous: read up on SQL Injection and you'll see what I mean.
 
Share this answer
 
Comments
Member 11403304 9-Nov-16 8:40am    
I am so lost here. So what I am trying to do before I move forward is test if my sql works.
When for example I test like this Dim strSql As String = GetCaseSQL("5678")
the results is SET @PartyID = 16482594
I would like the result to be SET @PartyID = 5678.
So the result should always be whatever is in the Dim strSql As String = GetCaseDQL("Whatever is in here should be returned as @PartyID")
OriginalGriff 9-Nov-16 9:16am    
The trouble is that the way you are trying to do this is dangerous - it involves concatenating strings, and that means that anyone who can enter the string can enter commands directly into your SQL server - and if it's a website that means from anywhere in the world - this is called SQL Injection and is totally to be avoided: google "bobby tables" and have a read.
The code I showed you shows how to do it safely - you provide a parameter which the SQL command calls @PartyID and which you set a value in a safe way, without string concatenation.

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