54行出错,DropDownList1没有选中的ITEM,请检查DropDownList1中是否有数据

解决方案 »

  1.   

    SqlDataAdapter myComm=new SqlDataAdapter("select id,image_name from image_lou where louid="+louid,myConn);
    DataSet ds=new DataSet();
    myComm.Fill(ds,"louming");
    this.DropDownList1.DataSource=ds.Tables["louming"].DefaultView;
    this.DropDownList1.DataTextField="image_name";
    this.DropDownList1.DataValueField="id";
    this.DropDownList1.DataBind();
    myConn.Close();
    上面这段代码写在
    if(!Page.IsPostBack)
    {
       //codehere
    }
      

  2.   

    DropDownList1绑定数据后,默认是不选中任何一项的,所以strSql+=this.DropDownList1.SelectedItem.Value;
    会得到“未将对象引用设置到对象的实例”错误。
    你可以在绑定后,给DropDownList1选择一个缺省选项,如第0项:
    DropDownList1.SelectedIndex=0;
      

  3.   

    我发现数据库搜索结果为空,
    请教高手,用在dropdownlist中用什么方法或属性获得这个空值呢?
      

  4.   

    ListItem litem=DropDownList1.SelectedItem;
    if(litem!=null)
    strSql+=litem.Value;
      

  5.   

    if(this.DropDownList1.SelectedIndex>=0)
    {
      strSql+=this.DropDownList1.SelectedItem.Value;
    }
    else
    {
      strSql+="-1";//数据库中永远没有ID=-1,也就是找不到数据
    }