MySqlCommand command = new MySqlCommand("select * from tbl_user where username = @uname", conn);
command.Parameters.AddWithValue("@uname", txtuname.Text);
conn.Open();
MySqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
MessageBox.Show("Username already used!");
}
}
else
{
string sql = "Insert into tbl_user(Username,Password,FirstName,LastName) values(@uname,@pword,@fname,@lname)";
MySqlCommand sda = new MySqlCommand(sql, conn);
sda.Parameters.AddWithValue("@uname", txtuname.Text);
sda.Parameters.AddWithValue("@pword", txtpword.Text);
sda.Parameters.AddWithValue("@fname", txtfname.Text);
sda.Parameters.AddWithValue("@lname", txtlname.Text);
sda.ExecuteNonQuery();
MessageBox.Show("Successfully Signed Up!\n You can login now!");
Form1 fm = new Form1();
this.Hide();
fm.ShowDialog();
}
reader.Close();
Asked
Active
Viewed 59 times
0

Gilad Green
- 36,708
- 7
- 61
- 95

maklot
- 13
- 6
-
The error means what it says - as long as there is a DataReader open on it, a connection cant be used for other things – Ňɏssa Pøngjǣrdenlarp Oct 04 '16 at 14:48
-
1You need to close the `reader` before your call to `ExecuteNonQuery`. It's best to put all the ADO.Net objects into `using` statement to make sure they are closed. – juharr Oct 04 '16 at 14:48
-
add `if (!reader.IsClosed) reader.Close();` in else block before your code – Jaydip Jadhav Oct 04 '16 at 14:58
-
1@JaydipJ, thanks a lot! It worked! – maklot Oct 04 '16 at 15:04