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 = (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;
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:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
dgOrgTypeDetail.DataContext = new OrganisationTypeDetailViewModel();
}