1

When running the following code I get the exception "System.Nullreferenceexception:object reference not set to an instance of an object". I believe it has something to with not initializing the allStudents variable but I'm not sure what type allStudents is.

Any help is appreciated

    private void showStudents(string c)
    {
        try
        {
            using (SMDataClassesDataContext db = new SMDataClassesDataContext())
            {

                var allStudents = from t in db.tbl_students
                                  where t.current_class == c
                                  select t;
              dgViewStudents.ItemsSource = allStudents;
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }
phil
  • 879
  • 1
  • 10
  • 20
Monty Swanson
  • 695
  • 16
  • 41
  • 1
    allStudents won't be the problem. Have you used the debugger? What variable is `null`? – Jeroen Vannevel Sep 07 '13 at 18:06
  • 2
    As a side-note, if you're ever unsure about the type of a `var`, hover the mouse over it, and Intellisense will tell you what type it is, if it can. – Chris Mantle Sep 07 '13 at 18:08
  • Are you sure the linq query returns something? – Nithin Nayagam Sep 07 '13 at 18:11
  • the message disappeared when I write dgViewStudents = new DataGrid(); However, the data is no longer loaded to dgViewStudents like this. – Monty Swanson Sep 07 '13 at 18:11
  • 1
    If you're using winforms, be sure to let the InitializeComponent -method get called before trying to access the components from your own code. – bobblez Sep 07 '13 at 18:14
  • @MontySwanson: Is this code executing in the form that contains dgvViewStudents? Or is it the case that this is in a class module and you forgot to pass in a reference to dgvViewStudents? If you put a breakpoint (F9) on the line `dgViewStudents.ItemsSource = allStudents;` and then inspect the value of dgvViewStudents, is it null? – Stephen Byrne Sep 07 '13 at 19:15
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Sep 07 '13 at 19:28
  • Yes, the GridView is on a page inside a frame called via a RibbonTab. The value from the breakpoint is null. – Monty Swanson Sep 07 '13 at 19:31

2 Answers2

1

I managed to solve the problem. Adding a check for null values solved the problem like this:

   if (dgViewStudents != null)
                    dgViewStudents.ItemsSource = allStudents.ToList();
Monty Swanson
  • 695
  • 16
  • 41
0

You need to force the evaluation of your query and set it up as a DataSource for your DataGridView....assuming the dgViewStudents variable itself is not null and that the allStudents query brings back results, this should work I think.

var bindingSource = new BindingSource();
var allStudents = from t in db.tbl_students
                  where t.current_class == c
                  select t;
bindingSource.DataSource = allStudents.ToList();
dgViewStudents.DataSource = bindingSource;
phil
  • 879
  • 1
  • 10
  • 20