I am facing the DataReader open connection issue in ASP.NET c# While Reading the records from MySQL database. I do not know how at first time of execution of datareader it showing me open connection.
The error as below at DAL
MySqlDataReader reader = myCommand.ExecuteReader();
There is already an open DataReader associated with this Connection which must be closed first
Sample Code
// BAL function call the DAL function
MainFunction()
{
...
...
using (MySqlDataReader reader = clsDAL.GetALL(SqlQuery, Parms))
{
while (reader.Read())
{
clsVitalsDisplay obj = new clsVitalsDisplay();
obj.BySessionId = Convert.ToInt32(reader["BySessionId"].ToString());
obj.ByDevSessionId_LifeTouchHeartRate = Convert.ToInt32(reader["ByDevSessionId_LifeTouchHeartRate"].ToString());
obj.HumanReadableDeviceId_LifeTouch = reader["HumanReadableDeviceId_LifeTouch"].ToString();
listLifetouchHeartRate.Add(obj);
}
}
}
// Getting the Error while Read the details from database in DAL (Data Access Layer) class at MySqlDataReader reader = myCommand.ExecuteReader();
public static MySqlDataReader GetALL(String _query, MySqlParameter[] sqlParameter)
{
MySqlCommand myCommand = new MySqlCommand();
try
{
myCommand.Connection = OpenConnection();
myCommand.CommandText = _query;
myCommand.Parameters.AddRange(sqlParameter);
myAdapter.SelectCommand = myCommand;
//Face the error at below line
MySqlDataReader reader = myCommand.ExecuteReader();
myCommand.Dispose();
return reader;
}
catch (MySqlException ex)
{
ErrorLog.ErrorLog.Log_Err("", ex, Global.gUserId.ToString());
return null;
}
finally
{
}
}
I have fixed this issue by closing the already opening adatabase connection if open as below. Afterthat its working fine.