DataList 绑定:“连接未关闭。连接的当前状态为打开。”

发布于 2024-11-18 21:05:25 字数 2274 浏览 1 评论 0原文

我试图将 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();
}

}

I'm trying to bound a DataList control with selected data from a SQL Table:

     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();
    }

When i run this code, i get "The connection was not closed. The connection's current state is open."

My previous version of the method was this:

    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();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-11-25 21:05:25

尝试先关闭连接而不是阅读器

Try to First Close the connection than reader

不及他 2024-11-25 21:05:25

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

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();
}

You could check if the connection was closed first:

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();
}
少跟Wǒ拽 2024-11-25 21:05:25

你在哪里声明连接?

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

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

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

Where are you declaring connection?

It seems like you are declaring it outside of the code provided and opening the connection somewhere else before hand.

To avoid trying to open it twice, you can put in a simple check to see if the connection is open...

if (conn == null || conn.State == ConnectionState.Closed)
                OpenDBConnection();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文