使用类填充列表框 [英] Populate listbox using a class

查看:58
本文介绍了使用类填充列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用类填充lixtbox,但出现错误对象引用未设置为对象的实例"

Hi
Im trying to populate a lixtbox using a class,I get an error " Object reference not set to an instance of an object"

//Method to populate listbox in a class
   public DataSet populateAvailableCoursesListBox(int currID)
   {
           dbConnection();
           dbCmd.CommandText = "SELECT DISTINCT courseCode FROM  Courses WHERE currID = @currID;";
           dbCmd.Parameters.AddWithValue("@currID", currID);
           dbAdapter = new OleDbDataAdapter(dbCmd);
           dbAdapter.SelectCommand = dbCmd;
           dbAdapter.Fill(ds);
           dbConn.Close();

           return ds;


   }







//Code behind to populate listbox
   public void populateListbox(int currID)
   {
       dtClass = new DataCls();
       lstCourses.DataSource = dtClass.populateAvailableCoursesListBox(currID);
       lstCourses.Text = "courseCode";
   }




//当单击下面的组合框时,它将在其中运行方法




//When the below combobox is clicked it runs the method inside it

protected void cboCurriculums_SelectedIndexChanged(object sender, EventArgs e)
    {
        int currID = Convert.ToInt32(cboCurriculums.SelectedIndex.ToString());
        populateListbox(currID);
    }

推荐答案

正如错误所述,您的对象在使用前未初始化.因此,对其进行调试,您将得到原因. 并且您列出的列表的人口代码不完整.我是这样的.
As error says, your object is not initialised before getting used. So debug it, you''ll get the cause.
and also you list population code of list is not complete. I have done like this.
lstEmployees.DataSource = employess;
lstEmployees.DataTextField = "Name";
lstEmployees.DataValueField = "Id";
lstEmployees.DataBind();


这里的employees是一个包含雇员对象的列表,而Name和Id是Employee类的属性


Here employees is a list containing employee objects and Name and Id are the property of the Employee class


这篇关于使用类填充列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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