DataList 绑定:“连接未关闭。连接的当前状态为打开。”
我试图将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试先关闭连接而不是阅读器
Try to First Close the connection than reader
您可以先检查连接是否已关闭:
C#:
You could check if the connection was closed first:
C#:
你在哪里声明连接?
看起来您是在提供的代码之外声明它并事先在其他地方打开连接。
为了避免尝试打开它两次,您可以进行简单的检查以查看连接是否打开......
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...