Click here to Skip to main content
15,889,808 members

Unable to select Item from Data-grid “Object reference not set to an instance of an object.”

gregsagan asked:

Open original thread
I have an issue in regards to my application, and not selecting an item for it to get updated using EF.

I'm creating an application for a user to enter in their organisation details. Seems simple enough. After data has been entered using a tab control, I have a view that contains a series of data grids, giving the user the ability to update, or insert another piece of data if required or have the ability to delete.

within my view-model I have a series of different things (which may not be the best way of implementing MVVM but trying to get there eventually).

I have my properties implementing INOTIFYPROPERTYCHANGE. Commands to save, update and delete. And my entity methods (CRUD opererations using Entity Framework - Will use a Repository system later on when I learn more about MVVM etc).

So far, I'm having to use the code-behind of the view to load my data which works perfectly (obviously I shouldn't be doing this so any advice to show me otherwise would be much appreciated) like so;

C#
private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        DBEntities context = new DBEntities();

        var orgTypeDetails = (from o in context.OrganisationTypeDetails
                              select o).ToList();

        dgOrgTypeDetail.ItemsSource = orgTypeDetails;
    }

I also have a event-handler which only allows you to select a row and then to update that specific row;

C#
private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e)
    {
        OrganisationTypeDetailViewModel org = new OrganisationTypeDetailViewModel();

        OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;

        if (selected == null)
            MessageBox.Show("You must select a 'Type' before updating.");
        else
        {
            OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();

            update.ShowDialog();
    org.UpdateOrganisationTypeDetail(selected);
            Page_Loaded(null, null);
        }
    }

and finally, my method in which updates the table using EF from View-Model;

C#
public void UpdateOrganisationTypeDetail(OrganisationTypeDetail orgTypeDetail)
    {
        using (DBEntities context = new DBEntities())
        {
            //var orgTD = context.OrganisationTypeDetails.Where(otd => otd.OrganisationTypeDetailID == OrganisationTypeDetailID).FirstOrDefault();

            var orgTD = (from a in context.OrganisationTypeDetails
                         where a.OrganisationTypeDetailID == OrganisationTypeDetailID
                         select a).FirstOrDefault();

            orgTD.OrganisationTypeDetailID = OrganisationTypeDetailID;
            orgTD.OrganisationTypeID = OrganisationTypeID;
            orgTD.Title = Title;
            orgTD.FirstName = FirstName;
            orgTD.Surname = Surname;
            orgTD.Position = Position;
            orgTD.DateOfBirth = DateOfBirth;
            orgTD.Address = Address;
            orgTD.Country = Country;
            orgTD.Postcode = Postcode;
            orgTD.PhoneNumber = PhoneNumber;
            orgTD.MobileNumber = MobileNumber;
            orgTD.FaxNumber = FaxNumber;
            orgTD.Email = Email;
            orgTD.NINumber = NINumber;

            context.OrganisationTypeDetails.ApplyCurrentValues(orgTD);
            context.SaveChanges();

            MessageBox.Show("Updated Organisation Type Details");
        }

When executing this application, it produces a Null Reference exception.

In my properties, to ensure that it doesn't crash, I have had to set programmatically the ID's; public int _OrganisationTypeDetailID=17; But I want to be able to select a row at my choice, rather then only having the option to get the row from 17.

I've tried using both Linq and Lamba expressions but neither seem to work.

Sorry if there's a lot to take in from reading this and would happy add more code or explanation if required :).


------- EDIT-------

Ive changed the way the code loads the data
View-Model;
XML
public List<OrganisationTypeDetail> GetOrganisationTypeDetail
        {
            get
            {
                using (DBEntities context = new DBEntities())
                {
                    var query = from e in context.OrganisationTypeDetails
                                select e;
                    return query.ToList<OrganisationTypeDetail>();
                }
            }
        }


Code-behind:
C#
private void Page_Loaded(object sender, RoutedEventArgs e)
        {
dgOrgTypeDetail.DataContext = new OrganisationTypeDetailViewModel();
}
Tags: C#, WPF, DataGrid

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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