对象引用未设置为LightSwitch中对象的实例 [英] Object reference not set to an instance of an object in LightSwitch

查看:103
本文介绍了对象引用未设置为LightSwitch中对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LightSwitch VS 2013创建CRUD应用程序。我想创建一个新的数据屏幕,用户可以在其中创建任何旧项目的副本,并能够更改某些细节,以轻松创建新项目。

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.

我试图通过绑定到文本框的本地属性来实现此场景,以便用户写入旧项目ID并单击副本。那么屏幕中的字段将被填入旧的项目详细信息。

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.

我的问题是在Button Executed事件中,我写了这段代码:

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.

我尝试使用FirstOrDefault而不是SinglelOrDefault,但是徒劳无功。我也尝试了这一行,但没有改变

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

甚至像var itms = dataEntities.Items这样的一行;给出例外。我确定Items不为空,并且该代码的项目存在。我相信与LightSwitch无关。这个我相信它根本不重复。

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.

推荐答案


对象引用未设置为一个对象的实例

Object reference not set to an instance of an object

意味着对该对象的引用为空。所以你的LINQ显然是查询返回null而不是一个实例。

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

这篇关于对象引用未设置为LightSwitch中对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆