0

I am trying to get my data from the database and show in a messagebox but it's not working. I tried to put a dummy messagebox inside my while loop but it seems the code is not going into it also. Are there any ways to do this?

This is my code:

 ConnectToDatabase();

 MySqlCommand cmd = new MySqlCommand("Select Name, LPN From visitors Where password = '@password';", conn);
 cmd.Parameters.AddWithValue("@password", decodeStr);

 MySqlDataReader reader = cmd.ExecuteReader();

 while (reader.Read())
 {
     MessageBox.Show("he");

     name = reader["Name"].ToString();
     lpn = reader["LPN"].ToString();
 }
4
  • Not sure, but shouldn't cmd.Parameters.AddWithValue("@password", decodeStr); be without the @? Commented Aug 17, 2015 at 12:22
  • 2
    @Stefan - No that @ is valid Commented Aug 17, 2015 at 12:23
  • 1
    yes @Stefan previously i used that it worked Commented Aug 17, 2015 at 12:24
  • 4
    I think you should remove the single quote from '@password'. The correct SQL statement: Select Name, LPN From visitors Where password = @password; Commented Aug 17, 2015 at 12:24

1 Answer 1

6

You need to delete single quotes in your '@password' part. With single quotes, database manager thinks this is a string literal, not a parameter.

And that's why your reader doesn't have any data because your password column probably doesn't have any string as @password.

A few things more;

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.