I am using a method which get me a list of items from database. I already used the connection to insert data to database and works fine, but when displaying data in gridview an exception pops up. Below you can find the method used, the code for binding data to gridview and the asp for the gridview. Hope you can help. Thanks
Method to retreive data
public List<Bet> getBets()
{
MySqlCommand cmd = Connection.CreateCommand();
cmd = new MySqlCommand("SELECT * FROM bets ORDER BY date");
try
{
if (this.Connection.State == ConnectionState.Closed)
this.Connection.Open();
MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); --> getting exception here
List<Bet> bets = new List<Bet>();
while (dr.Read())
{
Bet myBet = new Bet();
myBet = FillBetfromRow(dr);
bets.Add(myBet);
}
return bets;
}
catch (MySqlException ex)
{
throw ex;
}
finally
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
}
Binding data to gridview
gvBets.DataSource = new BetManagement().getBets();
gvBets.DataBind();
Try changing with Using Keyword. There may be an issue with the data reader close