I'm using LightSwitch VS 2013 to create a CRUD application. I want to create a new Data screen where the user can create a copy of any old item and be able to change some details too to create a new Item easily.
I'm trying to achieve this scenario through a local property that is bound to a textbox such that the user writes the old item id and clicks copy. then the fields in the screen will be filled with the old item details.
My problem is that inside the Button Executed event , I wrote this code :
ConstructionDBData dataEntities = new ConstructionDBData();
Item oldItemValue = (from Item i in dataEntities.Items
where i.Code == oldItemId
select i).SingleOrDefault();
this.ItemProperty.Name = oldItemValue.Name;
this.ItemProperty.Date = oldItemValue.Date;
//.... setting remaining properties
The code gives me an exception with the message "Object reference not set to an instance of an object ". The exception happens in the line used for retrieving the data.
I tried FirstOrDefault instead of SinglelOrDefault but in vain. I also tried this line but nothing changed
Item oldItemValue = dataEntities.Items.Where<Item>(i=> i.Code == oldItemId).FirstOrDefault();
even a line like var itms = dataEntities.Items; gives exception. I'm sure that Items isn't null and that the item with this code exists. I believe that has nothing to do with LightSwitch. That'why I believe it isn't duplicate at all.