ARTICLE

How to add Records programmatically into Entity in LightSwitch 2011

Posted by Jaganathan Bantheswaran Articles | LightSwitch 2012 September 05, 2011
In this article we shall discuss about what my colleague has discussed with me for help on his LightSwitch project.
Reader Level:

Foreword:

In this article we shall discuss about what my colleague has discussed with me for help on his LightSwitch project.

I.e. he needed to save a record into another table when he deleted a record.

So I showed a way to save the records programmatically in a LightSwitch 2011 application. Let us create a demo LightSwitch 2011 application to demonstrate the situation above.

Preparing the Solution:

Startup VS LightSwitch 2011; create a LightSwitch 2011 application as shown below.

EntyLS1.gif

Follow the steps shown in the figure.

Designing the Entity:

We are going to save the record into a table which has been deleted from another table. So we need to create two tables namely "Person" and "DeletedPerson" as shown in the below figures.

Person Entity:

EntyLS2.gif

DeletedPerson Entity:

EntyLS3.gif

Designing the Screen:

We are going to create two screens in which one is a List and Details Screen; another one is a Search Data Screen.

EntyLS4.gif

Here,

  1. Select the "List and Details Screen" template from the Screen template panel.
  2. Name the Screen "PeopleDetails".
  3. Select the Entity for the Screen as Person and Click OK to create the screen.

Now create another screen for showing the deleted record.

EntyLS5.gif

Here,

  1. Select the "Search Data Screen" template from the   Screen template panel.
  2. Name the Screen "SearchDeletedPerson".
  3. Select the Entity for the Screen as DeletedPerson and Click OK to create the screen.

To add the records which are deleted, we need to override the delete method of the LightSwitch application.

EntyLS6.gif

Here,

  1. Select the Command Bar of the People Detail screen.
  2. Select and right click the Delete button. A popup menu will be displayed.
  3. Select the Override Code to add the code for saving the data programmatically into the LightSwitch entity.

The Code-Behind:

EntyLS7.gif

When we click on Override code option, we will have a method called "PersonListDeleteSeleted_Execute".

Add the above highlighted code snippet into that method.

In the highlighted parts,

  1. The highlighted part in Yellow creates a new record in the DeletedPerson entity.
  2. The highlighted part in Gray assigns the deleted Person record to the DeletedPerson's properties.
  3. The highlighted part in Red deletes the Person record which has to be deleted.
  4. The highlighted part in Green saves the changes made to the DeletedPerson.

Now we are done. Just Hit F5 to see the application in Action.

Application in Action:

EntyLS8.gif

Here,

We have added a record with name Ammu. Select that record and delete that.

EntyLS9.gif

When we delete the record it will be saved in the Search Deleted Persons screen as shown in the above figure.

Summary:

In this article, we have seen about how to add records programmatically into a LightSwitch entity using Visual Studio LightSwitch 2011.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which make me to give a better article next time.

Login to add your contents and source code to this article
post comment
     

can you show us how to do this in VB code please?

Posted by majacik zajacik Jan 26, 2013

Thanks for comments. Can you post the sample project with details to my gmail id.

Posted by Jaganathan Bantheswaran Sep 21, 2011

Thanks great article. I have master and details tables that I am importing to with code. The foreign key to the master in the details file is named and given a type of master table name by LS. It is Details_Master in the SQL server. How can I set this value programatically? I get the newly inserted value from the Master with this code: newMasterId = newMstEntity.Details.Properties("Id").Value But I get an error that the types don't match when I try to set the value of the details file. The PropertyType returned from the details entity is "LightSwitchApplication.Master". Code to add details: Dim newEntity As IEntityObject = entitySetToCreateIn.AddNew() currentProperty = newEntity.Details.Properties("Master") currentProperty.Value = newMasterId Thanks in advance for pointing out what I am missing!

Posted by Donald Beaty Sep 20, 2011

Thanks Dipti...

Posted by Jaganathan Bantheswaran Sep 06, 2011

Nice Article.

Posted by Diptimaya Patra Sep 06, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.