Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static DataTable SqlDataAdapter(string sqlStr)
    {
        DataTable dt = new DataTable();

        try
        {
           
            SqlDataAdapter da = new SqlDataAdapter(sqlStr, con);
            da.Fill(dt);
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        return dt;
    }

My above function gives following error .....

There is already an open DataReader associated with this Command which must be closed first????
Please help ???
Posted
Updated 4-Dec-13 20:59pm
v2

Error is clear enough. You can not execute any command on the connection which is in use by SqlDataReader,that is SqlDataReader is using your connection. It has to be closed explicitly as its not of disconnected architecture.

So,close the SqlDataReader from where you are calling this method.
 
Share this answer
 
Hai

U receive a error like "already open dataReader" for best coding use close and dispose .
After use connection string must be close,like con.close() and for dataadapter or datareader must dispose like da.dispose(),dt.dispose() method.


Regards
AravindB
 
Share this answer
 
v2

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