【问题标题】:DataList binding : "The connection was not closed. The connection's current state is open."DataList 绑定:“连接未关闭。连接的当前状态为打开。”
【发布时间】:2011-07-06 14:37:02
【问题描述】:

我正在尝试将 DataList 控件与 SQL 表中的选定数据绑定:

     private void ShowPossiblePurchases(string CategoryName)
{

    string selectSQL = "SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()";
    SqlCommand cmd = new SqlCommand(selectSQL, connection);
    cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
    SqlDataReader reader;

    DataList DataList1 = (DataList)lgnView.FindControl("DataList1");

    try
    {

        connection.Open();
        reader = cmd.ExecuteReader();
        DataList1.DataSource = reader;
        DataList1.DataBind();

        reader.Close();

    }
    catch (Exception ex)
    {
        Label lblError = (Label)lgnView.FindControl("lblError");
        lblError.Text = ex.Message;
    }
    finally
    {
        connection.Close();
    }

当我运行这段代码时,我得到“连接没有关闭。连接的当前状态是打开的。”

我以前版本的方法是这样的:

    private void ShowPossiblePurchases(string CategoryName)

{

string selectSQL = "SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()";
SqlCommand cmd = new SqlCommand(selectSQL, connection);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
SqlDataReader reader;

DataSet myDataSet = new DataSet();
myDataSet.Tables.Add("Products");


myDataSet.Tables["Products"].Columns.Add("ProductID");
myDataSet.Tables["Products"].Columns.Add("CategoryID");
myDataSet.Tables["Products"].Columns.Add("ProductName");
myDataSet.Tables["Products"].Columns.Add("Price");

DataList DataList1 = (DataList)lgnView.FindControl("DataList1");

try
{
    connection.Open();
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {

        DataRow rowNew = myDataSet.Tables["Products"].NewRow();
        rowNew["ProductID"] = reader["ProductID"];
        rowNew["CategoryID"] = reader["CategoryID"];
        rowNew["ProductName"] = reader["ProductName"];
        rowNew["Price"] = reader["Price"];
        myDataSet.Tables["Products"].Rows.Add(rowNew);
    }

    DataList1.DataSource = myDataSet.Tables["Products"];
    DataList1.DataBind();
}
catch(Exception ex)
{
    Label lblError = (Label)lgnView.FindControl("lblError");
    lblError.Text = ex.Message;
}
finally
{
    connection.Close();
}

}

【问题讨论】:

  • 我不认为你可以像这样直接绑定到阅读器,你可能需要将结果复制到另一个集合中并将其用作数据源
  • 我会在你的连接上设置一个断点。打开然后单步执行,看看你在哪里得到了那个错误。
  • @w69rdy :我已经编辑了我的第一篇文章(现在您还可以阅读我之前使用 DataSet 绑定 DataList 控件的方法版本)。即使是那个版本,也会产生上述错误。
  • @esastincy:我会试试的。谢谢。
  • 我认为错误出现在第二个打开的连接上...您如何以及在何处调用 ShowPossiblePurchases ?使用异步方法还是静态方法?如果是,请尝试在打开之前检查连接状态 ... if(connection.State == ConnectionState.Open)

标签: c# asp.net data-binding datalist


【解决方案1】:

尝试比阅读器先关闭连接

【讨论】:

  • 但是,我不知道为什么我需要在'finally'块中定期关闭连接时对 close() 方法进行补充调用。
  • 实际上你的“finally”运行在函数的末尾,这样你就可以在连接关闭后将你的阅读器关闭
  • 我指的是代码中其他地方的其他“finally”块...谢谢您的建议!
【解决方案2】:

您可以先检查连接是否关闭:

If ((cmd.Connection.State And System.Data.ConnectionState.Open) _
         <> System.Data.ConnectionState.Open) Then
    cmd.Connection.Open()
End If

C#:

if (((cmd.Connection.State & System.Data.ConnectionState.Open) != System.Data.ConnectionState.Open)) {
    cmd.Connection.Open();
}

【讨论】:

    【解决方案3】:

    你在哪里声明连接?

    您似乎是在提供的代码之外声明它并事先在其他地方打开连接。

    为避免尝试打开两次,您可以进行简单的检查以查看连接是否打开...

    if (conn == null || conn.State == ConnectionState.Closed)
                    OpenDBConnection();
    

    【讨论】:

    • 连接对象在超类中声明,子类(如包含上述方法的子类)可以从中继承。
    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多