Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is already an open DataReader associated with this Command which must be closed first.

how to slove this like of error
Posted
Comments
Herman<T>.Instance 28-Aug-14 6:20am    
the exception tells it all.
You don't show code so we cannot help you where the error occurs. Please Improve your question and add the code that gets into error.

1.Cannot exist more then an open reader for a given command, so you must close the reader after its usage.

2.You should use DataReader by using try-finally block like in the next example:
C#
List<ErpCountry> resultList = new List<ErpCountry>();
DataReader reader = null;
//
try
{
    IDbCommand dbCommand = new SqlCommand();
    dbCommand.Connection = _session.DbConnection;
    dbCommand.CommandText = string.Format("SELECT ID, Name from Countries");
    //
    if (_session.DbConnection.State == ConnectionState.Closed)
        _session.DbConnection.Open();
    //
    reader = dbCommand.ExecuteReader();
    //
    while (reader.Read())
    {
        ErpCountry erpEntity = new ErpCountry();
        erpEntity.ID = (int)reader["ID"];
        erpEntity.Name = (string)reader["Name"];
        //
        resultList.Add(erpEntity);
    }
}
finally
{
    if (reader != null)
        reader.Close();
    //
    _session.DbConnection.Close();
}
 
Share this answer
 
v2
the exception tells it all.

datareader use like this.
C#
if (dr.HasRows)
{
while (dr.Read())
{
\\your code
}
}
dr.close();
 
Share this answer
 
close the datareader by using close() . And in your case the exception tells all. Else please show your code here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900