这段代码是从2003转到2005的,我直接在2005中写这段代码没有任何问题,但如果先在2003中写,再转到2005中来就出这个问题。Components.User user = new Components.User();
DataTable dt = user.GetAllUser();
if (dt.Rows.Count > 0)
{
   this.ListBox1.DataTextField = "UserName";//未将对象的引用设置到对象的实例
   this.ListBox1.DataValueField = "UserId";
   this.ListBox1.DataSource = dt;
   this.ListBox1.DataBind();
}

解决方案 »

  1.   

    this.ListBox1.DataSource = dt;
    this.ListBox1.DataTextField = "UserName";
    this.ListBox1.DataValueField = "UserId";
    this.ListBox1.DataBind();
    好像顺序错了
      

  2.   


    Components.User user = new Components.User();
    DataTable dt = user.GetAllUser();
    ------------------------------
    this.ListBox1.DataSource = dt;
    this.ListBox1.DataBind();
    ----------------------------
    if (dt.Rows.Count > 0)
    {
       this.ListBox1.DataTextField = "UserName";//未将对象的引用设置到对象的实例
       this.ListBox1.DataValueField = "UserId";
       this.ListBox1.DataSource = dt;
       this.ListBox1.DataBind();
    }
      

  3.   

    在2003中,控件会自动生成声明的代码,而在2005中没有生成这些代码,如果是2003的程序转换为2005后,代码的规则还是会遵循2003的规则,当你使用一个控件的时候,由于代码没有自动生成声明,2003的规则就不会认这个控件,不管调用什么方法或属性都会造成null错误。