-1

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.

Ahmed Kamal
  • 1,478
  • 14
  • 27
  • 3
    I suspect the actual line where the error happens is when you are access a property in the oldItemValue object. most likely it is null. – Ahmed ilyas Jan 05 '15 at 19:02
  • You should take a look at what `XOrDefault` returns as default value if it doesn't find anything. – Jeroen Vannevel Jan 05 '15 at 19:02
  • Echoing @Ahmedilyas: You need to see on which line the exception tells you the error happens. – Corey Adler Jan 05 '15 at 19:07
  • Have you checked that `dataEntities.Items` is not null and contains `oldItemID` manually? Set a breakpoint on your query line and step through, checking all variable values each step. – IllusiveBrian Jan 05 '15 at 19:08
  • The exception occurs in the linq statement , plz check the image attached https://www.dropbox.com/s/f74c5b44cchhlke/lightswitch.png?dl=0 @Ahmedilyas – Ahmed Kamal Jan 05 '15 at 19:30
  • @Namfuak Yea , I did , the items is already there and Items has a lot of items but when I try to do this programatically inside the function it gives me exception , even var itms = dataEntities.Items; – Ahmed Kamal Jan 05 '15 at 19:35
  • @JeroenVannevel even var itms = dataEntities.Items; gives exception , please check the question again to see the update – Ahmed Kamal Jan 05 '15 at 19:40
  • what does the `ConstructionDBData` class look like? if you are getting an exception at `var items = dataEntities.Items;` then clearly your Items property isn't being instantiated. – Claies Jan 05 '15 at 19:57
  • It is like the entityframework class , due to visual studio documentation "Provides members to query and update data in the ConstructionsDBData datasource" – Ahmed Kamal Jan 05 '15 at 20:03

1 Answers1

-2

Object reference not set to an instance of an object

means that the reference to the object is null. So your LINQ apparently query returns null instead of an instance.

Mr Balanikas
  • 1,657
  • 15
  • 28
  • 1
    Or it could simply be one of the properties OF the returned object is null... – user2366842 Jan 05 '15 at 19:09
  • Yea , I knew that too. I'm looking for the real cause. The item already exists and I'm sure of that. Just trying to write sth like var itms = dataEntities.Items; gives exception too ! – Ahmed Kamal Jan 05 '15 at 19:34