写法不好.
最好在ITEMDATABOUND里面做绑定..而且直接ddlconn.Open();
有可能会出错
open前先CLOSE了

解决方案 »

  1.   

    DropDownList对象的声明怎么能在循环语句里面写呢,我感觉这个地方有问题.
      

  2.   

    你的代码我基本看不懂,不过按我的理解应该是这个样子DropDownList ddl_Depart = new DropDownList();
    ddl_Depart = (DropDownList)DataGrid1.FindControl("ddl_Depart");while(ddlsdr.Read())
    {
        ddl_Depart.Items.Add(ddlsdr[0].ToString());
    }
      

  3.   

    lz的代码我都不知道在做什么,不过按我的理解应该是这个样子DropDownList ddl_Depart = new DropDownList();
    ddl_Depart = (DropDownList)DataGrid1.FindControl("ddl_Depart");while(ddlsdr.Read())
    {
        ddl_Depart.Items.Add(ddlsdr[0].ToString());
    }
    直接写的,不知道对不对,lz自己看吧
      

  4.   

    感谢各位的回复,我要实现的功能就是把DataGrid模块列中的DropDownList 控件在后台代码里绑定到数据源,我把代码写在了ItemCommand事件里面,而且我换成了英文字段,还是一样的报错,并且,我现在用的是数据集,但问题依旧,代码如下:(不可意思,因为是新手,代码写的不够简洁)
    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    SqlConnection ddlconn = new SqlConnection("server=(local);database=北风贸易;uid=sa;pwd=pengsg");
    string ddlstr="select distinct address from employee";
    SqlDataAdapter sda = new SqlDataAdapter(ddlstr,ddlconn);
    ddlconn.Open();
    DataSet ds = new DataSet();
    sda.Fill(ds,"address");
    DataView dv = ds.Tables[0].DefaultView;
    if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
    DropDownList ddl_Depart = ((DropDownList)e.Item.FindControl("ddl_Depart"));
    ddl_Depart.DataSource = dv;
    ddl_Depart.DataTextField= "";
    ddl_Depart.DataValueField = "";
    ddl_Depart.DataBind();
    }
    }
    报错信息如下:
    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 153: {
    行 154: DropDownList ddl_Depart = ((DropDownList)e.Item.FindControl("ddl_Depart"));
    行 155: ddl_Depart.DataSource = dv;
    行 156: ddl_Depart.DataTextField= "";
    行 157: ddl_Depart.DataValueField = "";
     
      

  5.   

    ItemCommand里面写数据库绑定操作不是太好,等于你每多一条记录,就要进行一次数据库连接,
    由于你每个dropdownlist里面的item都一样,可以事先取出来,将所有记录组成一个字符串,在itemCommand事件里拆分后添加到dropdownlist里另外出现未处理的异常貌似是由于你用dataset绑定dropdownlist造成的
    数据库用sqldatareader取出数据后,循环添加到dropdownlist里面,如下
    do while reader.read
       dropdownlist.item.add(new listitem(reader("id"),reader("name")))
    end while
      

  6.   

    没有找到FindControl("ddl_Depart");
      

  7.   

    你的dataGrid里也没有DropDownList  当然出错了...要是没有你为什么用
    ((DropDownList)e.Item.FindControl("ddl_Depart"));
    也就是你的dataGrid里得有一个名叫ddl_Depart控件..
      

  8.   

    1. 你是在DataGrid的什么模版列里加了个DropDownList,如果是在ItemTemplate中加的,其实这里放DropDownList就没必要,如果是在EditItemTemplate中加的在ItemCommand事件中就找不到了(除非你用其他手段),而且你看看DropDownList的名字是否为ddl_Depart2. 如果你想给模版列中的DropDownList中添加数据,一般是在DataGrid的ItemDataBound中做
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {  
    DropDownList dropTemp;
    if(e.Item.ItemType == ListItemType.EditItem)
    {
    dropTemp = (DropDownList)e.Item.FindControl("DropDownList1");
    if(dropTemp != null)
    {
    //绑定你的dropTemp }
    }
    }
      

  9.   

    谢谢各位的回复,回楼上的,我的DataGrid的模板列的DropDownList的名字肯定是为ddl_Depart,我是在EditItemplate中的加的DropDownList,我按照楼上所说的,我把代码加到ItemDataBound事件,还是一样的出错,出错信息如下:未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 129: DropDownList ddl_Depart = new DropDownList();
    行 130: ddl_Depart = (DropDownList)e.Item.FindControl("ddl_Depart");
    行 131: ddl_Depart.DataSource = dv;
    行 132: ddl_Depart.DataBind();
    行 133:
     
    代码如下:
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    SqlConnection ddlconn = new SqlConnection("server=(local);database=北风贸易;uid=sa;pwd=pengsg");
    string ddlstr="select distinct address from employee";
    SqlDataAdapter sda = new SqlDataAdapter(ddlstr,ddlconn);
    ddlconn.Open();
    DataSet ds = new DataSet();
    sda.Fill(ds,"address");
    DataView dv = ds.Tables["address"].DefaultView;
    DropDownList ddl_Depart;
    ddl_Depart = (DropDownList)e.Item.FindControl("ddl_Depart");
    if(ddl_Depart != null)
    {
    ddl_Depart.DataSource = dv;
    ddl_Depart.DataBind();
    }
    }
    我要实现的功能就是在DataGrid中单击编辑按扭后,模块列中的DropDownList能绑定到数据源上并取得数据