get count fails

T

TJS

trying to find count of existing email addresses to prevent duplicates but
simple function fails - what am I missing ?



Function DuplicateFound(byVal vEmail as String) As boolean
Dim sqlQuery AS string
Dim cmdSelect AS sqlcommand
sqlQuery = "SELECT Count(*) FROM users WHERE lower(rtrim(ltrim(email))
=@em "
cmdSelect.Parameters.Add( "@em" , vEmail ) '<== error on this line
cmdSelect = New SQLCommand(sqlQuery,con)
con.Open()
if cmdSelect.ExecuteScalar().toString > 0 then :return true : else :
return false:end if
End Function

( con.Open works for update and insert functions )

error msg:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 248: Dim cmdSelect AS sqlcommand
Line 249: sqlQuery = "SELECT Count(*) FROM Portal_users WHERE
lower(rtrim(ltrim(email)) =@em "
Line 250: cmdSelect.Parameters.Add( "@em" , vEmail )
Line 251: cmdSelect = New SQLCommand(sqlQuery,con)
Line 252: con.Open()
 
Y

Yunus Emre ALPÖZEN

You should instantiate your object firstly. After this you can add a
parameter. Just take that line after next line.
 
T

TJS

I changed it to
Function DuplicateFound(byVal vEmail as String) As boolean
Dim sqlQuery AS string
Dim cmdSelect AS sqlcommand
sqlQuery = "SELECT Count(*) FROM users WHERE lower(rtrim(ltrim(email)) =
@ em "
cmdSelect = New SQLCommand(sqlQuery,con)
cmdSelect.Parameters.Add( "@em" , vEmail ) '<== error on this line
con.Open()
if cmdSelect.ExecuteScalar() > 0 then :return true : else : return
false:end if
End Function

but I now get error:

Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect
syntax near '='.
 
Y

Yunus Emre ALPÖZEN

I think email address is a string based column. you should use = operator
with "'"
Something like this..
sqlQuery = "SELECT Count(*) FROM users WHERE lower(rtrim(ltrim(email)) ='
@ em'
 
T

TJS

hmm ...
that doesn't seem to help either


Yunus Emre ALPÖZEN said:
I think email address is a string based column. you should use = operator
with "'"
Something like this..
sqlQuery = "SELECT Count(*) FROM users WHERE lower(rtrim(ltrim(email)) ='
@ em'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top