Click here to Skip to main content
15,900,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys :D

I'm Trying to teach myself WEBSERVICE and i think its doing pritty good so far, Unfortunetly i got an Error with my WEBSERVICE Database when i try to extract a value out of my DB it gives me a run time error named: "Invalid column name 'sddd'."

heres a part of my code: (it heppens in all my function for some reason)

VB
Function IDExist(ByVal iDNum As String) As Boolean
    sqlConn.ConnectionString = connectString
    sqlConn.Open()
    Dim strSQL As String = "SELECT IDNum FROM ProgDB WHERE (IDNum = " & iDNum & ")"
    sqlCMD.Connection = sqlConn
    sqlCMD.CommandText = strSQL
    Dim i As String = sqlCMD.ExecuteScalar
    sqlConn.Close()
    Dim exist As Boolean = False
    If i <> Nothing Then
        exist = True
    End If
    Return exist
End Function


Thank You in Advance, Tsahi.
Posted

In your code:
Dim strSQL As String = "SELECT IDNum FROM ProgDB WHERE (IDNum = " & iDNum & ")"

iDNum is defined as as string, so you should probabaly quote the value in your sql statement.
Without quoting this, the sql server will look for a column named the same as your iDNum value.

Like this: (Note the single quote added before and after teh IDNum parameter)
Dim strSQL As String = "SELECT IDNum FROM ProgDB WHERE (IDNum = '" & iDNum & "')"
 
Share this answer
 
Comments
tsahi-al 19-Oct-11 14:02pm    
WORKED! Thank you!!
Mehdi Gholam 19-Oct-11 14:46pm    
Nice catch, 5!
Sander Rossel 19-Oct-11 15:09pm    
Yeah, that does it... But what you REALLY should have said was SQL Injection! ;)
fjdiewornncalwe 19-Oct-11 15:42pm    
Ah yes. Of course. I've only been functioning because of Advil for the last two days, so I was quite proud of myself for catching the initial syntax error... :)
SQL Injection ALERT! *Red lights flashing!*
VB
Dim strSQL As String = "SELECT IDNum FROM ProgDB WHERE IDNum = @myParam"
sqlCMD.CommandText = strSQL
cmd.Parameters.AddWithValue("@myParam", iDNum)

This replaces @myParam with the value of iDNum. SQL takes care of the rest. So if @myParam is a varchar and your user would have typed "D'Artagnan" your query would have failed in your example (try pasting that exact string with D'Artagnan in SQL, won't work because the ' breaks your command). However, by using parameters everything goes well. As a bonus your queries are cached an can be re-used boosting performance! And your code looks cleaner since there is no weird string concatenation.
It's a win win win win situation...
Sony was down for days because of SQL Injection, it cost them millions, don't make that same mistake!
Learn from little Bobby Tables[^] :)

P.S. Coming to think of it, in this example I am not quite sure if you should say '@myParam' with or without the quotes, but using parameters is definitly the way to go!
 
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