I am currently working on a simple login system with C# and MySQL and I have everything working so far but I can't seem to get the login verification working, I can't work out the correct way of checking the records in the table as I have tried a few different ways that I found online but none were working.
public bool loginCorrect(String name, String password)
{
if (this.OpenConnection() == true) {
using (MySqlCommand cmd = new MySqlCommand ("SELECT * FROM logins WHERE name = @UserName AND password = @Password")) {
cmd.Parameters.AddWithValue ("@UserName", name);
cmd.Parameters.AddWithValue ("@Password", password);
//Now we are going to read the data imput
MySqlDataReader myLoginReader = cmd.ExecuteReader ();
//if the data matches the rows (username, password), then you enter to the page
bool isExist = myLoginReader.HasRows();
//Close Reader
myLoginReader.Close ();
}
//Close Connection
this.CloseConnection ();
}
return isExist;
}
This is the current code for the login verification which doesn't work. I want the boolean to return true when the passed parameters name and password match which the ones in the database table 'logins'.
Below is my working Insert method just in case you need to compare the structure.
public void Insert(String username, String password)
{
string query = "INSERT INTO logins (name, password) VALUES ('" + username + "','" + password + "')";
//open connection
if (this.OpenConnection () == true)
{
//Create command and assign query
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
EDIT: I have altered the loginCorrect method as suggested and I get these errors when compiling: bool isExist = myLoginReader.HasRows(); // Non-Invocable member 'System.Data.Common.DbDataReader.HasRows' cannot be used like a method.
And: return isExist; // The name 'isExist' does not exist in the current context.
EDIT 2 Current Code
public void Insert(String name, String password)
{
//open connection
if (this.OpenConnection())
{
string query = "INSERT INTO logins (name, password) VALUES (@UserName, @Password)";
//Create command and assign query
MySqlCommand cmd = new MySqlCommand(query, connection);
//Add parameters
cmd.Parameters.AddWithValue ("@UserName", name);
cmd.Parameters.AddWithValue ("@Password", password);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
public bool loginCorrect(String name, String password)
{
bool isExist = false;
if (this.OpenConnection()) {
using (MySqlCommand cmd = new MySqlCommand ("SELECT * FROM logins WHERE `name` = @UserName AND password = @Password")) {
cmd.Parameters.AddWithValue ("@UserName", name);
cmd.Parameters.AddWithValue ("@Password", password);
//Now we are going to read the data imput
MySqlDataReader myLoginReader = cmd.ExecuteReader ();
//if the data matches the rows (username, password), then you enter to the page
isExist = myLoginReader.HasRows;
//Close Reader
myLoginReader.Close ();
}
//Close Connection
this.CloseConnection ();
}
return isExist;
}
Which gives
System.InvalidOperationException: Connection must be valid and open.
SOLUTION
public void Insert(String name, String password)
{
//open connection
if (this.OpenConnection())
{
string query = "INSERT INTO logins (name, password) VALUES (@UserName, @Password)";
//Create command and assign query
MySqlCommand cmd = new MySqlCommand(query, connection);
//Add parameters
cmd.Parameters.AddWithValue ("@UserName", name);
cmd.Parameters.AddWithValue ("@Password", password);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
public bool loginCorrect(String name, String password)
{
bool isExist = false;
if (this.OpenConnection()) {
using (MySqlCommand cmd = new MySqlCommand ("SELECT * FROM logins WHERE `name` = @UserName AND password = @Password", connection)) {
cmd.Parameters.AddWithValue ("@UserName", name);
cmd.Parameters.AddWithValue ("@Password", password);
//Now we are going to read the data imput
MySqlDataReader myLoginReader = cmd.ExecuteReader ();
//if the data matches the rows (username, password), then you enter to the page
isExist = myLoginReader.HasRows;
//Close Reader
myLoginReader.Close ();
}
//Close Connection
this.CloseConnection ();
}
return isExist;
}
The problem was not passing the connection in the loginCorrect method