Everything freezes when closing or disposing a reader

96 Views Asked by At

I'm having an issue with either the reader being open when I try to use the same connection to add data to the database OR closing or disposing the reader and having the program freeze. If you don't understand what i'm trying to explain, please ask questions. I'm a bit confused by the whole thing so I might be making 0 sense.

Code:

private void btnRegister_Click(object sender, EventArgs e)
{
    int numerror = 0;
    if (RUsernameTextBox.Text == "")
    {
        numerror++;
    }

    if (RPasswordTextBox.Text == "")
    {
        numerror++;
    }

    if (REmailTextBox.Text == "")
    {
        numerror++;
    }

    if (numerror > 0)
    {
        ErrorLabel.Text = "*" + numerror + " required field" + (numerror != 1 ? "s are" : " is") + " blank.";
    }
    else
    {
        var constring = "datasource=localhost;port=3306;username=Admin;password=**********;";
        using (var con = new MySqlConnection(constring))
        {
            con.Open();

            var cmd0 = new MySqlCommand("select username from userinfo.users where username=@username");
            cmd0.CommandType = CommandType.Text;
            cmd0.Connection = con;
            cmd0.Parameters.AddWithValue("@username", RUsernameTextBox.Text);
            using (var reader = cmd0.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    CMessageBox("Error", "Username is already in use.");
                    reader.Close();
                }
                else
                {
                    reader.Close();
                    var HashedPassword = EncodePassword(RPasswordTextBox.Text);
                    var cmd = new MySqlCommand("INSERT INTO userinfo.users (username,password,email,premium,picture) VALUES (@username, @hashedpassword, @email , @premium , @picture);");
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Username", RUsernameTextBox.Text);
                    cmd.Parameters.AddWithValue("@hashedpassword", HashedPassword);
                    cmd.Parameters.AddWithValue("@email", REmailTextBox.Text);
                    cmd.Parameters.AddWithValue("@premium", "0");
                    cmd.Parameters.AddWithValue("@picture", "ftp://***.***.*.**/Profile Pictures/" + RUsernameTextBox.Text + "/" +  Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");

                    try
                    {
                        cmd.ExecuteNonQuery();
                        MakeLoginVisible();

                        var TempFolder = Path.GetTempPath();
                        RProfilePicture.Image.Save("C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", System.Drawing.Imaging.ImageFormat.Png);
                        var ftpclient = new ftp("ftp://***.***.*.**/", "Admin", "**********");

                        ftpclient.createDirectory("Profile Pictures/" + RUsernameTextBox.Text);
                        ftpclient.upload("Profile Pictures/" + RUsernameTextBox.Text + "/" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", "C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");
                        MakeLoginVisible();
                        CMessageBox("Success!", "AirSpace Account '" + RUsernameTextBox.Text + "' Created.");
                        ftpclient = null;
                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        CMessageBox("Error", ex.Message.ToString());
                    }
                }
            }
        }
    }
}
2

There are 2 best solutions below

9
On

You don't really need a reader in this case. You can just get the count in your query and use ExecuteScalar()

var cmd0 = new MySqlCommand("select count(*) from userinfo.users where username=@username");
cmd0.CommandType = CommandType.Text;
cmd0.Connection = con;
cmd0.Parameters.AddWithValue("@username", RUsernameTextBox.Text);
if (((int)cmd0.ExecuteScalar()) > 0)
    CMessageBox("Error", "Username is already in use.");
else
   ... the rest of the code
0
On
using (var con = new MySqlConnection(constring))
using(var cmd0 = new MySqlCommand("Select count(*) from userinfo.users where username=@username", con))
{
    con.Open();
    cmd0.Parameters.AddWithValue("@username", RUsernameTextBox.Text);
    if((int)cmd0.ExecuteScalar() > 0)
    {
        CMessageBox("Error", "Username is already in use.");
        return;
    }
}

using (var con = new MySqlConnection(constring))
using(var cmd= new MySqlCommand("INSERT INTO userinfo.users (username,password,email,premium,picture) VALUES (@username, @hashedpassword, @email , @premium , @picture)", con))
{
    con.Open();
    var HashedPassword = EncodePassword(RPasswordTextBox.Text);
    cmd.Parameters.AddWithValue("@Username", RUsernameTextBox.Text);
    cmd.Parameters.AddWithValue("@hashedpassword", HashedPassword);
    cmd.Parameters.AddWithValue("@email", REmailTextBox.Text);
    cmd.Parameters.AddWithValue("@premium", "0");
    cmd.Parameters.AddWithValue("@picture", "ftp://***.***.*.**/Profile Pictures/" + RUsernameTextBox.Text + "/" +  Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");

    try
    {
        cmd.ExecuteNonQuery();
        MakeLoginVisible();

        var TempFolder = Path.GetTempPath();
        RProfilePicture.Image.Save("C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", System.Drawing.Imaging.ImageFormat.Png);
        var ftpclient = new ftp("ftp://***.***.*.**/", "Admin", "**********");

        ftpclient.createDirectory("Profile Pictures/" + RUsernameTextBox.Text);
        ftpclient.upload("Profile Pictures/" + RUsernameTextBox.Text + "/" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png", "C:\\temp\\" + Path.GetFileNameWithoutExtension(RProfilePicture.ImageLocation) + ".png");
        MakeLoginVisible();
        CMessageBox("Success!", "AirSpace Account '" + RUsernameTextBox.Text + "' Created.");
        ftpclient = null;
    }
    catch (Exception ex)
    {
        CMessageBox("Error", ex.Message.ToString());
    }
}