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


C#
<%@ WebHandler Language="C#" Class="Handler" %>


C#
using System;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;

 using connectionclass;

    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            connection con = new connection();
            con.getconnectionstring();       
            string imageid = context.Request.QueryString["ImID"];
            SqlCommand command = new SqlCommand("select Images from Upload_images where Image_ID=" + imageid, connection.con);
            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])dr[0]);
            context.Response.End();
            dr.Close();
            connection.con.Close();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
Posted
Updated 22-Sep-19 19:29pm
v3

Try to avoid using readers like this:
C#
SqlConnection connection = new SqlConnection("connection string");
SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection);
SqlDataReader reader = cmd.ExecuteReader();
connection.Open();
if (reader != null)
{
      while (reader.Read())
      {
              //do something
      }
}
reader.Close(); // <- too easy to forget
reader.Dispose(); // <- too easy to forget
connection.Close(); // <- too easy to forget

Instead, wrap them in using statements:
C#
using(SqlConnection connection = new SqlConnection("connection string"))
{

    connection.Open();

    using(SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection))
    {
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            if (reader != null)
            {
                while (reader.Read())
                {
                    //do something
                }
            }
        } // reader closed and disposed up here

    } // command disposed here

} //connection closed and disposed here


The using statement will ensure correct disposal of the object and freeing of resources.

If you forget then you are leaving the cleaning up to the garbage collector, which could take a while
 
Share this answer
 
v2
Close the DataReader & then SqlConnection, if there is an exception is not getting closed.

best practices to write this code in try catch finally block.

in finally block close the objects.

Something like this.


C#
SqlConnection conn = null;
SqlCommand cmd = null;

try
{
    conn = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString)
    cmd = new SqlCommand(reportDataSource, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
    cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
    cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;

        conn.Open(); //opens connection

    DataSet dset = new DataSet();
    new SqlDataAdapter(cmd).Fill(dset);
    this.gridDataSource.DataSource = dset.Tables[0];
}
catch(Exception ex)
{
    Logger.Log(ex);
    throw;
}
finally
{
    if(conn != null)
        conn.Dispose();

        if(cmd != null)
        cmd.Dispose();
} 
 
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