Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:03:41+00:00 2026-06-15T06:03:41+00:00

I have an issue in regards to my application, and not selecting an item

  • 0

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;

    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;

    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;

    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 :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:03:42+00:00Added an answer on June 15, 2026 at 6:03 am

    FirstOrDefault returns default value of type if query doesn’t return any result, so allays check if the result is null after FirstOrDefault.

    It should look something like this

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

    If you are sure, that query returns at least one element, use First instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue in regards the alignment of the marquee. You can see
I have an issue using xsd dataTime with regard to integrating a web client
We are new to ROR, We have issue in creating Login/Logout process in ROR
I have an issue which I could not find answer for across the web.
I have a customItem in my application and I am using keypressed event for
Hi i have develop an android application in which i want to prevent user
I have made an iphone fitness application. The issue i am unable to resolve
I have python threaded application + Postgres. I am using Django's ORM to save
I have a VS 2005 Web Site project (not the web application project model,
we have an issue with Windows Server 2008 when using it as a domain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.