Object reference not set to an instance of an object in LightSwitch [duplicate]

你离开我真会死。 提交于 2019-12-13 08:43:38

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/27786045/object-reference-not-set-to-an-instance-of-an-object-in-lightswitch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!