I'm being presented with the following error message every attempt to login using the correct credentials:
There is already an open DataReader associated with this Command which must be closed first.
Please could someone dissect this code so I can finally move onto the next phase, thanks people!
// Login Function for Manual Login
public void ent()
{
try
{
SqlConnection con = new SqlConnection(cc.connectDB());
con.Open();
SqlCommand cmd = new SqlCommand("delete from log", con);
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand("select * from Login where username='" + username.Text + "' and password='" + password.Text + "'", con);
cmd1.ExecuteNonQuery();
SqlDataReader c = cmd1.ExecuteReader();
if (c.Read() == true)
{
SqlCommand cmd2 = new SqlCommand("select typeid from Login where username='" + username.Text + "' and password='" + password.Text + "'", con);
Int32 count = (Int32)cmd2.ExecuteScalar();
if (count == 1)
{
SqlCommand cmd3 = new SqlCommand("insert into log values ('" + 1 + "')", con);
cmd3.ExecuteNonQuery();
}
else
if (count == 2)
{
SqlCommand cmd3 = new SqlCommand("insert into log values ('" + 2 + "')", con);
cmd3.ExecuteNonQuery();
}
Menu shw = new Menu();
shw.Show();
this.Hide();
}
else
{
MessageBox.Show("Login failed");
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
I'm connecting to an SQL Express server that is fully up and running but I just can't seem to find a way in which I'm able to close the reader without causing unnecessary errors.