We help IT Professionals succeed at work.

dr = cmd.ExecuteReader()

net_susan
net_susan asked
on
Medium Priority
9,167 Views
Last Modified: 2012-06-21
I have an error on this line:

 dr = cmd.ExecuteReader()

Incorrect syntax near '='.

What's wrong with my code?

Private Sub verifycus()
  Dim objConn As New SqlConnection("Server=myhost; Initial Catalog=mydb; User ID=myid; Password=mypw")
        objConn.Open()
        try
        dim VEmail, VGPassword
        Dim cmd As New SqlCommand("Select * FROM CustomerInfo WHERE Email= '" &VEmail &"' AND VGPassword = "&VPassword, objconn)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()
        While dr.Read
            Response.Write(dr.Item("Email"))
        End While
         dr.Close
               finally
               objConn.Close()
               objConn.Dispose()
               end try
 End Sub
Comment
Watch Question

Top Expert 2006
Commented:
Hi net_susan,

Try this:
Dim cmd As New SqlCommand("SELECT * FROM CustomerInfo WHERE Email= @email AND VGPassword = @password", objconn)
cmd.Parameters.Add(New SqlParameter("@email", VEmail))
cmd.Parameters.Add(New SqlParameter("@password", VPassword))

-- Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Perhaps a typo?

Dim cmd As New SqlCommand("Select * FROM CustomerInfo WHERE Email= '" &VEmail &"' AND VGPassword =  ' " & VPassword & " ' ", objconn)

Author

Commented:
Scolja,

BC30451: Name 'Email' is not declared.
Top Expert 2006

Commented:
net_susan,

I don't have "Email" anywhere, just hanging out... maybe you forgot the V in front of it on this line?

cmd.Parameters.Add(New SqlParameter("@email", VEmail))

-- Jason

Commented:
you probably just need to initialize dr with 'New'.  

Dim dr As New SqlDataReader

Commented:
Dim cmd As New SqlCommand("Select * FROM CustomerInfo WHERE Email= '" &VEmail &"' AND VGPassword = "&VPassword, objconn)

on this line, why do you have double quotes before &VEmail? should just be

WHERE Email = " & VEmail & " AND VGPassword = " & VPassword, objConn)
Top Expert 2006

Commented:
craskin,

Actually, you can't instantiate a SqlDataReader, it has to be assigned.

-- Jason

Author

Commented:
Jason,

I must have messed up somewhere?

Private Sub verifycus()
Dim objConn As New SqlConnection("Server=myhost; Initial Catalog=mydb; User ID=myid; Password=mypw")
        objConn.Open()
               try
        dim VEemail, VPassword
Dim cmd As New SqlCommand("SELECT * FROM CustomerInfo WHERE Email= @VEemail AND VGPassword = @Vpassword", objconn)
cmd.Parameters.Add(New SqlParameter("@email", VEemail))
cmd.Parameters.Add(New SqlParameter("@VGpassword", VPassword))
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()
        While dr.Read
            Response.Write(dr.Item("Email"))
        End While
         dr.Close
               finally
               objConn.Close()
               objConn.Dispose()
               end try
 End Sub
Top Expert 2006

Commented:
net_susan,

Well, from looking at this code... I'm not sure what you're trying to do... you declare your two variables VEemail and VPassword immediately before the New SqlCommand() line.  But then you pass those two variables into the SQL, but they're going to be blank, right?  You haven't assigned a value to them.....

So I'm not sure how you expect to receive any results.  Maybe you can clear this up for me....

-- Jason

Author

Commented:
Sorry. I'm passing them from the previous page (or at least trying to).

Protected Sub signinSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Redirect("next.aspx?VEemail='"+syEmail.Text +"'&VPassword='"+syPassword.Text+"'")
End Sub

That page works.
Top Expert 2006

Commented:
Then you need to retrieve them on your new page:

Dim VEemail As String = Request.QueryString("VEemail")
Dim VPassword As String = Request.QueryString("VPassword")

Those 2 lines should replace this one:

dim VEemail, VPassword

-- Jason

Author

Commented:
Thanks, new q coming soon.  :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.