0

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"

Error Message

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();
             }
kadeem675
  • 1
  • 1
  • FYI: That's an error with your code, not with Visual Studio. – ProgrammingLlama Mar 30 '22 at 05:44
  • 2
    Honestly, it's not clear why you don't just declare your SQL variables within the method that uses them, rather than as field variables in the class. – ProgrammingLlama Mar 30 '22 at 05:45
  • this is just the way I learned it from my teacher. I'm also trying to self-learn other ways of doing it. – kadeem675 Mar 30 '22 at 08:27
  • I see. Something like [this](https://pastebin.com/BWXpNQtL) is a more correct way of doing it. This example is with the MSSQL classes, but should work just the same with the MySQL ones. – ProgrammingLlama Mar 30 '22 at 08:30
  • i have attempted to try it a similar way and have gotten the same reader error message. https://imgur.com/a/S74s2DC – kadeem675 Mar 30 '22 at 08:42
  • You're calling `sqlData.Load(sqlDataReader);`, which will consume the reader to exhaustion. Then you're trying to further consume the reader. – ProgrammingLlama Mar 30 '22 at 08:50
  • Thank you for the help. I just have one more question. Do you know why it opens multiple instances of "new frmDashboard().Show(); Whenever the program is running? Or how to prevent that? – kadeem675 Mar 30 '22 at 09:08
  • You mean other than the two that your `btn_Login_Click` method opens? – ProgrammingLlama Mar 30 '22 at 09:23
  • No. In the while statement, it opens multiples instances of the next form. Even without the if statement. – kadeem675 Mar 30 '22 at 09:44
  • If you're using `while`, it will repeat for every row from the database. Your query doesn't currently filter by user, so it will return all the rows in your table. – ProgrammingLlama Mar 30 '22 at 09:46
  • ok, thank you. I will have to investigate how to filter my queries by user – kadeem675 Mar 30 '22 at 10:01

0 Answers0