Yazılım.
CevapSitesi.com Beta!
Çözüm Noktası
Bu siteyi 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

  1. ...Server Error in '/' Application.
  2. There is already an open DataReader associated with this Command which must be closed first.
  3. 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.
  4.  
  5. Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
  6.  
  7. Source Error:
  8.  
  9.  
  10. Line 99: {
  11. Line 100: DataTable table = new DataTable();
  12. Line 101: adapter.Fill(table);
  13. Line 102: CloseConnection();
  14. Line 103: adapter.Dispose();
  15.  
  16. Source File: c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\App_Code\SqlHelper.cs Line: 101
  17.  
  18. Stack Trace:
  19.  
  20.  
  21. [InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.]
  22. System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) +1731690
  23. System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +314
  24. 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
  25. System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +84
  26. System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +207
  27. System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +214
  28. System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +465
  29. System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +147
  30. SqlHelper.ToDataTable() in c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\App_Code\SqlHelper.cs:101
  31. SqlHelper.ToValue(Int32 rowIndex, Int32 cellIndex) in c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\App_Code\SqlHelper.cs:116
  32. Kategorisi.Page_Load(Object sender, EventArgs e) in c:\Inetpub\vhosts\izmirdental.com\izmirtermal.izmirdental.com\Kategorisi.aspx.cs:21
  33. System.Web.UI.Control.OnLoad(EventArgs e) +108
  34. System.Web.UI.Control.LoadRecursive() +90
  35. System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1533
  36.  
  37. 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
  1. ...using System;
  2. using System.Data;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Data.SqlClient;
  9. using System.Web.UI.HtmlControls;
  10.  
  11. public partial class Kategorisi : System.Web.UI.Page
  12. {
  13. string SayfaKategoriId = "";
  14. string UstKategoriId = "SayfaKategoriId";
  15.  
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. using (var helper = new SqlHelper())
  19. {
  20. SayfaKategoriId = Ayarlar.Temizle(RouteData.Values["SayfaKategoriId"].ToString());
  21. ltrlKategoriAdi.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + SayfaKategoriId + " ").ToValue<string>();
  22. ltrlKategoriAdi33.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + SayfaKategoriId + " ").ToValue<string>();
  23. ltrlKategoriAdi44.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + UstKategoriId + " ").ToValue<string>();
  24. ltrlKategoriAdititle.Text = helper.CommandText("Select KategoriAdi from SayfaKategoriler Where SayfaKategoriId=" + SayfaKategoriId + " ").ToValue<string>();
  25.  
  26. var dtAltKategoriler = helper.CommandText("SELECT * FROM SayfaKategoriler WHERE UstKategoriId =" + SayfaKategoriId).ToDataTable().DataTableToList<SayfaKategoriler>();
  27. rptAltKategoriler.DataSource = dtAltKategoriler;
  28. rptAltKategoriler.DataBind();
  29.  
  30. DataTable dtSoruCevap = helper.CommandText("Select * from Sayfalar Where SayfaKategoriId=" + SayfaKategoriId).ToDataTable();
  31. for (int i = 0; i < dtSoruCevap.Rows.Count; i++)
  32. {
  33. var temp = helper.CommandText("select * from SayfaResimleri where vitrin = 1 and sayfaId = " + dtSoruCevap.Rows[i][0]).ToDataRow();
  34. if (temp != null)
  35. {
  36. dtSoruCevap.Rows[i]["Resmi"] = "HaberResimleri/600/" + temp["Resim"];
  37. }
  38. else
  39. {
  40. dtSoruCevap.Rows[i]["Resmi"] = "HaberResimleri/600/yok.jpg";
  41. }
  42. }
  43. dlKategoriler.DataSource = dtSoruCevap;
  44. dlKategoriler.DataBind();
  45. }
  46. }
  47. protected void rptAltKategoriler_ItemDataBound(object sender, RepeaterItemEventArgs e)
  48. {
  49. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  50. {
  51. var item = e.Item.DataItem as SayfaKategoriler;
  52. var hrLink = e.Item.FindControl("hrLink") as HtmlAnchor;
  53. var hrLink2 = e.Item.FindControl("hrLink2") as HtmlAnchor;
  54. var imgBox = e.Item.FindControl("imgBox") as HtmlImage;
  55.  
  56. string url = item.Url;
  57. string text1 = item.KategoriAdi;
  58. string text2 = item.KategoriAdi + " Kategorisi için tıklatın!";
  59. string image = "images/kategori-icon.png";
  60.  
  61. using (SqlHelper helper = new SqlHelper())
  62. {
  63. Sayfalar sayfa = helper.CommandText("SELECT TOP 1 * FROM Sayfalar WHERE SayfaKategoriId = " + item.SayfaKategoriId + " ORDER by SayfaId asc").ToDataTable().DataTableToList<Sayfalar>().FirstOrDefault();
  64.  
  65. if (sayfa != null)
  66. {
  67. var katTmp = helper.CommandText("select * from SayfaKategoriler where SayfaKategoriId=" + sayfa.SayfaKategoriId).ToDataRow();
  68. url = Ayarlar.UrlSeo("sayfakategori_" + katTmp["KategoriAdi"] + "_" + sayfa.SayfaKategoriId + ".html");
  69. text1 = item.KategoriAdi + " İçindekiler!";
  70. text2 = item.KategoriAdi + " Listele!";
  71. var tmp = helper.CommandText("select * from SayfaResimleri where vitrin = 1 and SayfaId=" + sayfa.SayfaId).ToDataRow();
  72. if (tmp != null)
  73. {
  74. image = "HaberResimleri/600/" + tmp["Resim"].ToString();
  75. }
  76. else
  77. {
  78. image = "HaberResimleri/600/yok.jpg";
  79. }
  80. }
  81. }
  82.  
  83. imgBox.Src = image;
  84.  
  85. hrLink.HRef = url;
  86. hrLink.InnerText = text1;
  87.  
  88. hrLink2.HRef = url;
  89. hrLink2.InnerText = text2;
  90. }
  91. }
  92. }

1 Cevap


0


Bağlantı dizesine MultipleActiveResultSets=true ifadesini ekle.

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

Başvuru için:



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

Bir cevap yazın: