0

I have this simple piece of code for populating a ComboBox :

 DataSet dt = new DataSet();

 dt = db.getCourses(depID, academicYearValue, semID);
 if (dt.Tables[0].Rows.Count > 0)
 {
     dropdownCourses.DataSource = dt.Tables[0];
     dropdownCourses.DisplayMember = "Course";
     dropdownCourses.ValueMember = "ID";
 }

I have 4 ComboBoxes on the Form all work with the code above and get populated, only for this ComboBox when populating DataSouce i get exception :

Object reference not set to an instance of an object.

And i cannot figure out anything , any suggestions what to look for?

confusedMind
  • 2,573
  • 7
  • 33
  • 74
  • Probably it's null when you are assigning – Sajeetharan Mar 28 '15 at 08:29
  • The Table has Rows in it approx 48 i confirmed that. It will enter the loop when if (dt.Tables[0].Rows.Count > 0) so it has something.. – confusedMind Mar 28 '15 at 08:29
  • add a condition if(dt.Tables.count > 0) – Sajeetharan Mar 28 '15 at 08:31
  • @Sajeetharan is it not automatically confirming dt.Tables.count > 0) if it confirms dt.Tables[0].Rows.Count > 0 – confusedMind Mar 28 '15 at 08:32
  • Object References Not Set.... [is well explained here](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Just start up your debugger and you will find out. – Steve Mar 28 '15 at 08:33
  • @Steve that is the issue i am debugging i see the Data in datatable and still i get the error above. Same piece of code on 4 other comboboxes working fine. – confusedMind Mar 28 '15 at 08:37
  • Using the debugger will tell you the line that triggers the exception, hovering with the mouse over the elements involved will tell you which one is the null one. Keep in mind that the acting of assigning a property could trigger an event where the error kicks. – Steve Mar 28 '15 at 08:44
  • This is the line with the error : dropdownCourses.DataSource = dt.Tables[0]; and the element involved here is Dt which i see is not null. So why the exception. – confusedMind Mar 28 '15 at 08:47
  • and `dropdownCourses` ? – Steve Mar 28 '15 at 09:00
  • @Steve dropdownCourses is a comboBox – confusedMind Mar 28 '15 at 14:19
  • 1
    @Steve , You were rite i mistakenly do dropdownCourse=null somewhere in the code which was causing this issue. – confusedMind Mar 28 '15 at 14:24

1 Answers1

0

There is a possibility when you have handled SelectedIndexChanged or SelectedValueChanged or any other event. That will be called when you set the DataSource property of your combobox.

This happens because ValueMember is not set when DataSource is being assigned. Suppose you have written a line of code in the SelectedValueChanged event of your combobox like this.

 String myVal = dropdownCourses.SelectedValue.ToString();

Here, I am trying to convert SelectedValue into string type value.

This code will get executed immediately after assigning DataSource of Combobox and before assigning ValueMember property. So, that means the combobox doesn't have the ValueMember field.

So, the value in SelectedValue property will be null and null cannot have any property or method where we are trying to convert it into string. So, this will throw the error object reference of instance of an object is not set.

Solution:

You should set the ValueMember and DisplayMember properties before assigning DataSource to the ComboBox.

dropdownCourses.ValueMember = "ID"; //First
dropdownCourses.DisplayMember = "Course";  //Second
dropdownCourses.DataSource = dt.Tables[0]; //Third

The second way is that you can create a boolean flag to avoid the unnecessary event call during the populating combobox.

bool bLoading = false;

{
    dt = db.getCourses(depID, academicYearValue, semID);
    if (dt.Tables[0].Rows.Count > 0)
    {
        bLoading = true;
        try
        {
            dropdownCourses.DataSource = dt.Tables[0];
            dropdownCourses.DisplayMember = "Course";
            dropdownCourses.ValueMember = "ID";
        }
        catch{}
        finally{
            bLoading = false; //make sure that this variable must be false after populating combobox otherwise event will not work
        }
    }
}

private void dropdownCourses_SelectedValueChanged(....)
{
    if (!bLoading)
    {
        Write your code here.
    }
}

I would like to suggest second option if you don't want to call event unnecessarily.

Shell
  • 6,818
  • 11
  • 39
  • 70