Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Im trying to populate a lixtbox using a class,I get an error " Object reference not set to an instance of an object"

SQL
//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;


   }



C#
//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
C#
protected void cboCurriculums_SelectedIndexChanged(object sender, EventArgs e)
    {
        int currID = Convert.ToInt32(cboCurriculums.SelectedIndex.ToString());
        populateListbox(currID);
    }
Posted

1 solution

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();

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900