Must declare the scalar variable "@CheckUser"

T

Tony M

vs 2005 - vb .net - web forms - xp pro

Can't figure out why I keep getting the error Must declare the scalar
variable "@CheckUser".
The error happens on SqlDA.Fill(SqlDS). Just trying to make sure EMail
address doesn't exist in database before I insert data.
Connection string and everything else is correct. If I use Dim SQL = "Select
* from Clients where EMail = " & chr(39) & "(e-mail address removed)" &
chr(39) it works fine.


Dim parmIfExist As New SqlParameter("@CheckUser", txtEmail.Text)
SQL = "Select * from Clients where EMail = @CheckUser"
SqlCmd.CommandText = SQL
SqlCmd.Connection() = SQLcon
SqlCmd.Connection.Open()
SqlCmd.Parameters.Clear()
SqlCmd.Parameters.Add(parmIfExist)
Dim SqlDA As New SqlDataAdapter(SQL, SQLcon)
Dim SqlDS As New Data.DataSet()
Dim DR As Data.DataRow

SqlDA.Fill(SqlDS) << Error on this line Must declare the scalar variable
"@CheckUser".

If SqlDS.Tables(0).Rows.Count = 0 Then
Do stuff
end if


Thanks
 
T

Teemu Keiski

Hi,

you don't use the SqlCmd object which has the SqlParameter associated,
instead you pass the SQL directly to the data adapter when it wouldn't have
knowledge of the parameter.

"Dim SqlDA As New SqlDataAdapter(SQL, SQLcon)"

Solution would be:

Construct the SqlDataAdapter with the SqlCmd

OR

Omit SqlCmd and set parameter directly with
SqlDa.SelectCommand.Parameters.Add(...)
 

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

Top