Question

I'm trying to develop a little user-user messaging system in C# (my first C# app) using a mysql database.

I know best practice would be to use something like EF for working with the database, but I'm writing this the "stupid" way just to get comfortable with the syntax.

I come from a vb background so I'm basically just converting what I normally do in vb and that's why I can't understand why, despite having a matching row in my database, the DataReader keeps coming back with no rows. See below:

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Page.Validate("Login");

        if (!IsValid)
        {
            return;
        }

        if (checkLogin())
        {
            Response.Redirect("Default.aspx");
        }
        else
        {
            lblErr.Text = "Invalid login attempt";
        }
    }

    protected Boolean checkLogin()
    {
        MySql db = new MySql();

        string sql = "select userid, firstname, lastname, ifnull(lastlogin, '') from users where email = @email and pass = @pass";

        List<MySqlParameter> args = new List<MySqlParameter>();
        args.Add(new MySqlParameter() { ParameterName = "@email", MySqlDbType = MySqlDbType.VarChar, Size = 100, Value = txtEmail.Text });
        args.Add(new MySqlParameter() { ParameterName = "@pass", MySqlDbType = MySqlDbType.VarChar, Size = 20, Value = txtPass.Text });

        MySqlDataReader dr = db.getReader(sql, args);

        if (dr.HasRows)
        {
            dr.Read();

            Session["userid"] = dr.GetInt32("userid");
            Session["firstname"] = dr.GetString("firstname");
            Session["lastname"] = dr.GetString("lastname");
            Session["lastlogin"] = dr.GetDateTime("lastlogin");

            dr.Close();

            return true;
        }
        else
        {
            return false;
        }
    }

The above code checks the user input against the database and returns a row if there are any rows in the table that match the user input...

Here's the code for the interaction with the database...

public class MySql
{
    static string connStr = "data source=localhost; initial catalog=mail; user id=root; password=Ly@12157114;";
    MySqlConnection conn = new MySqlConnection(connStr);

    public MySqlDataReader getReader(string sql, List<MySqlParameter> args)
    {
        if (args.Count == 0)
        {
            return getReader(sql);
        }
        else
        {
            return getReader(sql, args.ToArray());
        }
    }
    public MySqlDataReader getReader(string sql, MySqlParameter[] args = null)
    {
        MySqlCommand cmd = new MySqlCommand(sql, conn);

        if (args != null)
        {
            cmd.Parameters.AddRange(args);
        }

        conn.Open();

        MySqlDataReader dr = cmd.ExecuteReader();

        conn.Close();

        return dr;
    }
}

I get no errors or any indication that something has gone wrong here and I'm positive there is data that match my input into the login fields.

Can anyone help me to understand why the DataReader keeps coming back empty?

Was it helpful?

Solution

The connection of your data reader must be opened when you use it. The DataReader don't retrieve any data when you create it, it retrieves data when you use it. It can not retrieve your all database before you close the connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top