Yazılım.
CevapSitesi.com Beta!
Çözüm Noktası
Facebook, Twitter, Google+ veya e-posta ile paylaşın.
| Sorular | Makaleler | Üyeler | Etiketler  | İletişim
Soru sormak ya da cevap vermek için;
giriş yapın veya üye olun.

Sosyal medya hesaplarınızla da giriş yapabilirsiniz.
0

"There is already an open DataReader associated with this Command which must be closed first" hatası

Sayfayı tekrar hızlı açtığımda yada bir kaç sayfa aynı anda açtığımda alıyorum. Bağlantı kapanma hatası sanırım ama bulamadım. sqlhelper class kullanıyorum

...Server Error in '/' Application.
There is already an open DataReader associated with this Command which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

Source Error:


Line 99:         {
Line 100:            DataTable table = new DataTable();
Line 101:            adapter.Fill(table);
Line 102:            CloseConnection();
Line 103:            adapter.Dispose();

Source File: c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\App_Code\SqlHelper.cs    Line: 101

Stack Trace:


[InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.]
   System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) +1731690
   System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +314
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +156
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +84
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +207
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +214
   System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +465
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +147
   SqlHelper.ToDataTable() in c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\App_Code\SqlHelper.cs:101
   SqlHelper.ToValue(Int32 rowIndex, Int32 cellIndex) in c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\App_Code\SqlHelper.cs:116
   Kategorisi.Page_Load(Object sender, EventArgs e) in c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\Kategorisi.aspx.cs:21
   System.Web.UI.Control.OnLoad(EventArgs e) +108
   System.Web.UI.Control.LoadRecursive() +90
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1533

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4261.0

SqlHelper class kodum
...using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public interface ISqlCommand
{
    ISqlCommand CommandText(string commandText);
    ISqlCommand Connection(SqlConnection connection);
    ISqlCommand Transaction(SqlTransaction transaction);
    ISqlCommand CommandTimeout(int timeout);
    ISqlCommand CommandType(CommandType commandType);
    ISqlCommand Parameters(Func<ISqlCommandParameter, object> parameter);
    DataSet ToDataSet();
    DataTable ToDataTable();
    DataRow ToDataRow(int rowIndex = 0);
    TResult ToValue<TResult>(int rowIndex = 0, int cellIndex = 0);
    SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default);
    int ExecuteNonQuery();
    object ExecuteScalar();
    TResult ExecuteScalar<TResult>();
}

public interface ISqlCommandParameter
{
    ISqlCommandParameter Add(SqlParameter parameter);
    ISqlCommandParameter Add(string parameterName, object value);
    ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType);
    ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size);
    ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size, string sourceColumn);
    ISqlCommandParameter AddWithValue(string parameterName, object value);
}

public class SqlHelper : ISqlCommand, IDisposable
{
    #region Filds
    public static readonly SqlConnection DefaultConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString);
    private readonly SqlCommand _command = null;
    private SqlConnection _connection = null;
    private SqlTransaction _transaction = null;
    #endregion

    static SqlHelper()
    {

    }
    public SqlHelper()
    {
        _connection = DefaultConnection;
        _command = new SqlCommand { Connection = _connection };
    }

    #region ISqlCommand
    public ISqlCommand CommandText(string commandText)
    {
        _command.CommandText = commandText;
        return this;
    }
    public ISqlCommand Connection(SqlConnection connection)
    {
        _connection = connection;
        return this;
    }
    public ISqlCommand Transaction(SqlTransaction transaction)
    {
        _transaction = transaction;
        _command.Transaction = _transaction;
        return this;
    }
    public ISqlCommand CommandTimeout(int timeout)
    {
        _command.CommandTimeout = timeout;
        return this;
    }
    public ISqlCommand CommandType(CommandType commandType)
    {
        _command.CommandType = commandType;
        return this;
    }
    public ISqlCommand Parameters(Func<ISqlCommandParameter, object> parameter)
    {
        SqlCommandParameter param = new SqlCommandParameter(_command);
        parameter.Invoke(param);
        return this;
    }
    public DataSet ToDataSet()
    {
        using (SqlDataAdapter adapter = new SqlDataAdapter(_command))
        {
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            return dataSet;
        }
    }
    public DataTable ToDataTable()
    {
        OpenConnection();
        using (SqlDataAdapter adapter = new SqlDataAdapter(_command))
        {
            DataTable table = new DataTable();
            adapter.Fill(table);
            CloseConnection();
            adapter.Dispose();

            return table;
        }
    }
    public DataRow ToDataRow(int rowIndex = 0)
    {
        DataTable table = ToDataTable();
        if (table.Rows.Count == 0) return null;
        return table.Rows[rowIndex];
    }
    public TResult ToValue<TResult>(int rowIndex = 0, int cellIndex = 0)
    {
        DataTable table = ToDataTable();
        if (table.Rows.Count == 0) return default(TResult);

        object value = table.Rows[rowIndex][cellIndex];

        if (value is TResult)
        {
            return (TResult)value;
        }
        throw new InvalidCastException();
    }
    public SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default)
    {
        OpenConnection();
        return _command.ExecuteReader(commandBehavior);
    }
    public int ExecuteNonQuery()
    {
        OpenConnection();
        int result = _command.ExecuteNonQuery();
        CloseConnection();
        return result;
    }
    public object ExecuteScalar()
    {
        OpenConnection();
        object value = _command.ExecuteScalar();
        CloseConnection();
        return value;
    }
    public TResult ExecuteScalar<TResult>()
    {
        OpenConnection();
        object value = _command.ExecuteScalar();
        if (value is TResult)
        {
            CloseConnection();
            return (TResult)value;
        }
        throw new InvalidCastException();
    }
    private void OpenConnection()
    {
        if (_connection.State != ConnectionState.Open)
        {
            _connection.Open();
        }
    }
    private void CloseConnection()
    {
        if (_connection.State != ConnectionState.Closed)
        {
            _connection.Close();
        }
    }
    #endregion

    #region IDisposable
    public void Dispose()
    {
        if (_transaction != null)
        {
            _transaction.Dispose();
        }

        if (_connection.State != ConnectionState.Open)
        {
            _connection.Close();
        }
    }
    #endregion
}

public class SqlCommandParameter : ISqlCommandParameter
{
    private readonly SqlCommand _command;
    public SqlCommandParameter(SqlCommand command)
    {
        _command = command;
    }
    public ISqlCommandParameter Add(SqlParameter parameter)
    {
        _command.Parameters.Add(parameter);
        return this;
    }
    public ISqlCommandParameter Add(string parameterName, object value)
    {
        _command.Parameters.Add(parameterName, value);
        return this;
    }
    public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType)
    {
        _command.Parameters.Add(parameterName, sqlDbType);
        return this;
    }
    public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size)
    {
        _command.Parameters.Add(parameterName, sqlDbType, size);
        return this;
    }
    public ISqlCommandParameter Add(string parameterName, SqlDbType sqlDbType, int size, string sourceColumn)
    {
        _command.Parameters.Add(parameterName, sqlDbType, size, sourceColumn);
        return this;
    }
    public ISqlCommandParameter AddWithValue(string parameterName, object value)
    {
        _command.Parameters.AddWithValue(parameterName, value);
        return this;
    }
}

Sayfa aspx cs kodum
...using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class Kategorisi : System.Web.UI.Page
{
    string SayfaKategoriId = "";
    string UstKategoriId = "SayfaKategoriId";

    protected void Page_Load(object sender, EventArgs e)
    {
        using (var helper = new SqlHelper())
        {
            SayfaKategoriId = Ayarlar.Temizle(RouteData.Values["SayfaKategoriId"].ToString());
            ltrlKategoriAdi.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + SayfaKategoriId + " ").ToValue<string>();
            ltrlKategoriAdi33.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + SayfaKategoriId + " ").ToValue<string>();
            ltrlKategoriAdi44.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + UstKategoriId + " ").ToValue<string>();
            ltrlKategoriAdititle.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + SayfaKategoriId + " ").ToValue<string>();

            var dtAltKategoriler = helper.CommandText("SELECT * FROM SayfaKategoriler WHERE UstKategoriId =" + SayfaKategoriId).ToDataTable().DataTableToList<SayfaKategoriler>();
            rptAltKategoriler.DataSource = dtAltKategoriler;
            rptAltKategoriler.DataBind();

            DataTable dtSoruCevap = helper.CommandText("Select * from Sayfalar Where SayfaKategoriId=" + SayfaKategoriId).ToDataTable();
            for (int i = 0; i < dtSoruCevap.Rows.Count; i++)
            {
                var temp = helper.CommandText("select * from SayfaResimleri where vitrin = 1 and sayfaId = " + dtSoruCevap.Rows[i][0]).ToDataRow();
                if (temp != null)
                {
                    dtSoruCevap.Rows[i]["Resmi"] = "HaberResimleri/600/" + temp["Resim"];
                }
                else
                {
                    dtSoruCevap.Rows[i]["Resmi"] = "HaberResimleri/600/yok.jpg";
                }
            }
            dlKategoriler.DataSource = dtSoruCevap;
            dlKategoriler.DataBind();
        }
    }
    protected void rptAltKategoriler_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var item = e.Item.DataItem as SayfaKategoriler;
            var hrLink = e.Item.FindControl("hrLink") as HtmlAnchor;
            var hrLink2 = e.Item.FindControl("hrLink2") as HtmlAnchor;
            var imgBox = e.Item.FindControl("imgBox") as HtmlImage;

            string url = item.Url;
            string text1 = item.KategoriAdi;
            string text2 = item.KategoriAdi + " Kategorisi için tıklatın!";
            string image = "images/kategori-icon.png";

            using (SqlHelper helper = new SqlHelper())
            {
                Sayfalar sayfa = helper.CommandText("SELECT TOP 1 * FROM Sayfalar WHERE SayfaKategoriId = " + item.SayfaKategoriId + " ORDER by SayfaId asc").ToDataTable().DataTableToList<Sayfalar>().FirstOrDefault();

                if (sayfa != null)
                {
                    var katTmp = helper.CommandText("select * from SayfaKategoriler where SayfaKategoriId=" + sayfa.SayfaKategoriId).ToDataRow();
                    url = Ayarlar.UrlSeo("sayfakategori_" + katTmp["KategoriAdi"] + "_" + sayfa.SayfaKategoriId + ".html");
                    text1 = item.KategoriAdi + " İçindekiler!";
                    text2 = item.KategoriAdi + " Listele!";
                    var tmp = helper.CommandText("select * from SayfaResimleri where vitrin = 1 and SayfaId=" + sayfa.SayfaId).ToDataRow();
                    if (tmp != null)
                    {
                        image = "HaberResimleri/600/" + tmp["Resim"].ToString();
                    }
                    else
                    {
                        image = "HaberResimleri/600/yok.jpg";
                    }
                }
            }

            imgBox.Src = image;

            hrLink.HRef = url;
            hrLink.InnerText = text1;

            hrLink2.HRef = url;
            hrLink2.InnerText = text2;
        }
    }
}

1 Cevap

0
Bağlantı dizesine MultipleActiveResultSets=true ifadesini ekle.

Örnek:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True; MultipleActiveResultSets=true;

Başvuru için:



Cevaplayan: 03.02.21 20:05
cevapsitesi
102,034p 16ü
Cevabı seçen: 08.02.21 17:03
cevapsitesi   102,034p 16ü