There Is Already An Open Datareader Associated With This Connection Which Must Be Closed First

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 bé = new connection(); nhỏ.getconnectionstring(); string imageid = context.Request.QueryString<"ImID">; SqlComm& comm& = new SqlCommand("select Images from Upload_images where Image_ID=" + imageid, connection.con); SqlDataReader dr = comm&.ExecuteReader(); dr.Read(); context.Response.BinaryWrite((Byte<>)dr<0>); context.Response.End(); dr.Close(); connection.bé.Close(); public bool IsReusable get return false;




*

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()) //vì something reader.Close(); // reader.Dispose(); // connection.Close(); // Instead, wrap them in using statements:
using(SqlConnection connection = new SqlConnection("connection string")) connection.Open(); using(SqlComm& cmd = new SqlCommand("SELECT * FROM SomeTable", connection)) using (SqlDataReader reader = cmd.ExecuteReader()) if (reader != null) while (reader.Read()) //vị something // reader closed and disposed up here // commvà disposed here //connection closed và disposed hereThe using statement will ensure correct disposal of the object và freeing of resources.If you forget then you are leaving the cleaning up khổng lồ the garbage collector, which could take a while
Maybe the following links could help youhttp://stackoverflow.com/questions/15875837/when-putting-multiple-sql-commands-showing-error-there-is-already-an-open-datar<^>
Close the DataReader & then SqlConnection, if there is an exception is not getting closed.

Bạn đang xem: There is already an open datareader associated with this connection which must be closed first

best practices khổng lồ write this code in try catch finally bloông chồng.in finally block cthua thảm the objects.Something lượt thích this.

Xem thêm: Cấp Nguồn Cho Máy Lạnh 2 Cục, Sơ Đồ Đấu Dây Máy Lạnh Inverter Đơn Giản Tại Nhà


SqlConnection conn = null;SqlComm& cmd = null;try conn = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString) cmd = new SqlCommand(reportDataSource, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("
endDate", SqlDbType.DateTime).Value = end; conn.Open(); //opens connection DataSet dphối = new DataSet(); new SqlDataAdapter(cmd).Fill(dset); this.gridDataSource.DataSource = dphối.Tables<0>;catch(Exception ex) Logger.Log(ex); throw;finally if(conn != null) conn.Dispose(); if(cmd != null) cmd.Dispose();
I have read và agree to lớn the Terms of Service and Privacy Policy Please subscribe me khổng lồ the cqaugusta.com newsletters
When answering a question please: Read the question carefully.Understvà that English isn"t everyone"s first language so be lenient of badspelling và grammar.If a question is poorly phrased then either ask for clarification, ignore it, oredit the question
and fix the problem. Insults are not welcome. Don"t tell someone to lớn read the manual. Chances are they have & don"t get it. Provide an answer or move on to lớn the next question. Let"s work lớn help developers, not make them feel stupid.

This nội dung, along with any associated source code & files, is licensed under The Code Project mở cửa License (CPOL)


There is already an open datareader associated with this commvà which must be closed first VB.NET Squốc lộ SERVER
'There is already an open datareader associated with this command which must be closed first.'
System.invalidoperationexception: 'there is already an open datareader associated with this commvà which must be closed first.'
InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.