I am new to the C# and My method showing a error in try catch
connection is already open
code as follow, when I closed it from Class method then Form getting a error invalid connection
. here if put all code in FORM it is working. but here I get MysqlDataReader
as a return value. how can I solve this error.
CLASS
//select all categories
public MySqlDataReader SelectCategory() {
try
{
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM categories WHERE online = 1";
connection.Open();
MySqlDataReader categories = cmd.ExecuteReader();
return categories;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
return null;
}
}
FORM
public void show()
{
MySqlDataReader rd = db.SelectCategory();
try
{
while (rd.Read())
{
listBox1.Items.Add(rd.GetString(1));
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}