Reader Level:
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.
  • 0
  • 0
  • 24260

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.

COMMENT USING

Trending up