在Gridview中填充下拉列表时,我遇到错误以下错误 [英] While Populating Dropdown Inside Gridview I Am Getting Error Below Error

查看:54
本文介绍了在Gridview中填充下拉列表时,我遇到错误以下错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

描述:执行   current 网络请求。请查看堆栈跟踪了解有关错误的更多信息 其中源自 代码。 

异常详细信息:System.InvalidOperationException:连接已关闭。连接' 当前状态已打开。





我哪里错了?

<前lang =c#> 受保护 void OnRowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
conn.Open();

var ddl =(DropDownList)e.Row.FindControl( < span class =code-string> ddlCompany1);

SqlCommand cmd = new SqlCommand( SELECT CompanyName FROM Company,conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

conn.Close();

ddl.DataSource = ds;
ddl.DataTextField = CompanyName;
ddl.DataValueField = ID;
ddl.DataBind();
ddl.Items.Insert( 0 new ListItem( 选择 0\" ));
}
}

解决方案

错误消息显示连接仍然​​打开。

  //  以下行会导致错误消息: 
conn.Open();



您需要检查连接状态 [ ^ ]。

  if (conn.State == ConnectionState.Closed){conn.Open();} 


我会这样做,



  protected   void  OnRowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl =(DropDownList)e.Row.FindControl( ddlCompany1);
if (ddl!= null
{
DataSet ds = GetData( SELECT CompanyName,ID FROM Company);
ddl.DataSource = ds.Tables [ 0 ];
ddl.DataTextField = CompanyName;
ddl.DataValueField = ID;
ddl.DataBind();
ddl.Items.Insert( 0 new ListItem( 选择 0\" ));
}
}
}

私有 DataSet GetData( string query)
{
string conString = ConfigurationManager.ConnectionStrings [ constr]。ConnectionString;
使用(SqlConnection con = new SqlConnection(conString))
使用(SqlCommand cmd = new SqlCommand(query,con))
{
使用(SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
}
}





请注意,

1尝试将您的UI代码和数据访问代码分开

2.可重用的方法

3.您没有在select语句中选择ID列,但是将其设置为值字段!

4.当您拥有类级别或全局共享连接对象时,连接处理并不容易,尝试使用使用块并在需要时创建连接,它将自动关闭连接


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: The connection was not closed. The connection's current state is open.



Where i am wrong?

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    conn.Open();

    var ddl = (DropDownList)e.Row.FindControl("ddlCompany1");

    SqlCommand cmd = new SqlCommand("SELECT CompanyName FROM  Company", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    conn.Close();

    ddl.DataSource = ds;
    ddl.DataTextField = "CompanyName";
    ddl.DataValueField = "ID";
    ddl.DataBind();
    ddl.Items.Insert(0, new ListItem("Select", "0"));
  }
}

解决方案

The error message says that connection is still opened.

//below line causes error message:
conn.Open();


You need to check connection state[^].

if (conn.State==ConnectionState.Closed) {conn.Open();}


I would do as below,

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
      var ddl = (DropDownList)e.Row.FindControl("ddlCompany1");
      if(ddl !=null)
      {
          DataSet ds = GetData("SELECT CompanyName, ID FROM  Company");
          ddl.DataSource = ds.Tables[0];
          ddl.DataTextField = "CompanyName";
          ddl.DataValueField = "ID";
          ddl.DataBind();
          ddl.Items.Insert(0, new ListItem("Select", "0"));
      }
   }
}

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    using (SqlCommand cmd = new SqlCommand(query,con))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        {
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
    }
}



Note that,
1. try to separate your UI code and the data access codes
2. Reusable methods
3. you haven't select ID column in your select statement but you set it as value field!
4. Connections handling is not easy when you have class level or global shared connection object, try to use "using blocks" and create the connection when you need and it will close the connection automatically.


这篇关于在Gridview中填充下拉列表时,我遇到错误以下错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆