0

I've been trying to create a login page that will check if you're an administrator or a customer in my SQL data source. I am not sure why it can't understand the MySQLCommands. I added MySql.Data in the references but this doesn't seem to work.

This is where for example: MySqlConnection and MySqlCommand have blue underlinement.

        Dim cmd As MySqlCommand = New MySqlCommand '(strSQL, con)
3
  • What error (if any) do you get? Commented Jun 12, 2016 at 18:15
  • And if you right click them, is there a Resolve option? Commented Jun 12, 2016 at 18:15
  • @Steve Type 'MySQLComman' is not defined Commented Jun 12, 2016 at 18:38

2 Answers 2

1

Password is a reserved word in MySql. If you want to use a field with that name then everytime you use it in your code you should remember to put it between backticks:

 `password` = ...

Said that your code has serious problems. You should never concatenate strings coming from the user input to form a sql text. This leads to syntax errors caused by parsing problem and to Sql Injection attacks. You shoul use a parameterized query like this

   strSQL = "SELECT name FROM employer WHERE (login=@login AND `password`=@pwd"
   Dim cmd As MySqlCommand = New MySqlCommand(strSQL, con)
   cmd.Parameters.Add("@login", MySqlDbType.VarChar).Value = strUser 
   cmd.Parameters.Add("@pwd",MySqlDbType.VarChar).Value = strPaswoord
   con.Open()
   If cmd.ExecuteScalar() = Nothing Then
      ....

Finally you should also change the way you get your data because you want to minimize the trips to access the database for performance reason. You should SELECT both the Name and the EMail with a single query and use an MySqlDataReader to get the data.

Other problems present in your code are the lack of appropriate using statement around the connection and the security problem caused by a possible clear text password stored in the database.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help. I'm new in all this and am trying to keep it simple so I didn't think of how to stop sqlinjections. THis is also why my code is probably not very clean. But for example: Dim con As MySqlConnection = New MySqlConnection() shows that MySqlConnection is not defined.
Did you add the reference to the MySql.Data.dll library and then did you add an Imports MySql.Data.MySqlClient ? These steps are basic if you want to use the library to access an MySql database
YES! I forgot MySql.Data.MySqlClient at the top! I jsut figured that out. Thank you
0

@GSerg asked me if I could right click and resolve. I tried that but that was not an option. After messing around with the error it appears that I had to write at top:

Imports MySql.Data.MySqlClient

I also had to add backticks when I used the word password for MySQL as @Steve reminded me.

Thank you for your help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.