title: Object reference not set to an instance of an object in LightSwitch
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>My problem is that inside the Button Executed event , I wrote this code :</p>
  
  <pre><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
  </code></pre>
  
  <p>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.</p>
  
  <p>I tried FirstOrDefault instead of SinglelOrDefault but in vain. I also tried this line but nothing changed</p>
  
  <pre><code>          Item oldItemValue = dataEntities.Items.Where&lt;Item&gt;(i=&gt; i.Code == oldItemId).FirstOrDefault();
  </code></pre>
  
  <p>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.</p>