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

  • SEARCH
  • Home
  • 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 8949457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:13:18+00:00 2026-06-15T13:13:18+00:00

I’m using WPF to create an application to enable an organisation to enter different

  • 0

I’m using WPF to create an application to enable an organisation to enter different pieces of data into the application.I have a tab control to allow them to do this.

Then in a separate view, I have a series of different data grids showing the user what data they have inserted into the database. Containing buttons to either, add, update or delete the data they want.

Which leads me to my question. Currently, I am able to delete, and add data with ease and with no problem. But then comes my issue with trying to get the selected item to update, which it doesn’t, resulting in a null reference exception.

If i set my property attributes programmatically though, it updates it fine. like so;public int _OrganisationTypeDetailID = 17; public int _OrganisationTypeID = 1;But I do not want this, as I want the ability for the user to select for themselves and update the data they need to.

Here’s some of the code that may help in resolving my issue;

View Model;

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

            if (orgTD != null)
            {
                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.Attach(orgTD);
                context.OrganisationTypeDetails.ApplyCurrentValues(orgTD);
                context.SaveChanges();

                MessageBox.Show("Updated Organisation Type Details");
            }
            else
            {
                MessageBox.Show("Unable to update selected 'Type'.");
            }
        }

    private OrganisationTypeDetail _SelectedType;
    public OrganisationTypeDetail SelectedType
    {
        get
        {
            return _SelectedType;
        }
        set
        {
            if (_SelectedType == value)
                return;

            _SelectedType = value;
            OnPropertyChanged("SelectedType");
        }
    }

    public List<OrganisationTypeDetail> GetOrganisationTypeDetail //Loads data 
    {
        get
        {
            using (DBEntities context = new DBEntities())
            {
                var query = from e in context.OrganisationTypeDetails
                            select e;
                return query.ToList<OrganisationTypeDetail>();
            }
        }
    }

    private ICommand showUpdateCommand;
    public ICommand ShowUpdateCommand //Update command
    {
        get
        {
            if (showUpdateCommand == null)
            {
                showUpdateCommand = new RelayCommand(this.UpdateFormExecute, this.UpdateFormCanExecute); //i => this.UpdateOrganisationTypeDetail()
            }
            return showUpdateCommand;
        }
    }

Code behind;

    private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e)
    {
        OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;
        OrganisationTypeDetailViewModel org = new OrganisationTypeDetailViewModel();

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

xaml;

            <DataGrid Name="dgOrgTypeDetail" Height="145" Width="555" 
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding GetOrganisationTypeDetail}"
            SelectedItem="{Binding SelectedType, Mode=TwoWay}">

Hope this issue can be resolved.

  • 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-15T13:13:21+00:00Added an answer on June 15, 2026 at 1:13 pm

    I would say that your best bet for this is to use commanding in the MVVM pattern to achieve this..
    It looks like you’re using a combination of MVVM and code behind and actually creating a new instance of the view model when your click event fires. Try binding the view model to your view once in the code behind of the view as the datacontext and then try updating the selected type..

    Also when you’re trying to do the update on SelectedType – look at your View using Snoop – see if the SelectedType property is still bound to the view.

    ICommand UpdateOrgTypeDetail { get;} 
    

    Then in the view model constructor declare new instance

    UpdateOrgTypeDetail = new DelegateCommand<object>(ExecuteUpdateOrgTypeDetail,    CanExecuteUpdateOrgTypeDetail);
    

    These two delegates will then allow you to click your button (which needs to bind to UpdateOrgTypeDetail)

    <Button Command="{Binding UpdateOrgTypeDetail}" />
    

    You should find that the update on the property is done correctly from here.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have just tried to save a simple *.rtf file with some websites and

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.