I am trying to create a login form that will authenticate the user's credentials before letting them proceed to another form. I am getting an error message where it says "invalid attempt to read when reader is closed"
Code:
private void btn_Login_Click(object sender, EventArgs e)
{
sqlConnection.ConnectionString = "server=" + server + ";" + "username=" + username + ";" + "password=" + password + ";" + "database=" + database;
sqlConnection.Open();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "Select tunapunaboysrc.addregister.Username, tunapunaboysrc.addregister.Password "
+ "from tunapunaboysrc.addregister";
sqlDataReader = sqlCommand.ExecuteReader();
sqlData.Load(sqlDataReader);
dg_Login.DataSource = sqlData;
if (sqlDataReader.Read() == true)
{
new frmDashboard().Show();
this.Hide();
}
else
{
MessageBox.Show("Invalid Username or Password, Please Try Again", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtbx_username.Text = "";
txtbx_password.Text = "";
txtbx_username.Focus();
}
sqlDataReader.Close();
sqlConnection.Close();
new frmDashboard().Show();
this.Hide();
}
I also use this at the beginning also, incase if its needed.
public partial class frmLogin : Form
{
MySqlConnection sqlConnection = new MySqlConnection();
MySqlCommand sqlCommand = new MySqlCommand();
DataTable sqlData = new DataTable();
MySqlDataAdapter SqlAdapter = new MySqlDataAdapter();
DataSet sqlSet = new DataSet();
MySqlDataReader sqlDataReader;
String server = "localhost";
String username = "root";
String password = "cybers";
String database = "tunapunaboysrc";
Can someone help me figure out why my reader is closing, and how i can solve it?
i tried pasting the authentication code
if (sqlDataReader.Read() == true)
{
new frmDashboard().Show();
this.Hide();
}
else
{
MessageBox.Show("Invalid Username or Password, Please Try Again", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtbx_username.Text = "";
txtbx_password.Text = "";
txtbx_username.Focus();
}