Adding Default Value in LightSwitch 2012

Here we will see how to display a default value in a LightSwitch Application (Visual C#) in Visual Studio 2012.

The following is the procedure of displaying a default value.

Step 1

Open the Solution Explorer.

solution ex.jpg

Step 2

In the Solution Explorer, right-click on the Server and choose "Add Table".

Add table.jpg

Step 3

The table appears.

Emp.jpg

Here in the table select the DOJ (Date Of Joining) field and go to the property window. Uncheck the "Display by default" checkbox.

prop win display.jpg

Step 4

To add a default value, click on the "Write code" option and select the Employee_Created.

created method.jpg

In the code designer, do the following coding:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.LightSwitch;

namespace LightSwitchApplication

{

    public partial class Employee

    {

        partial void Employee_Created()

        {

            this.DOJ = DateTime.Now;

        }

    }

}

Step 5

In the Solution Explorer, right-click on the Screens and choose "Add Screen".

add src.jpg

Step 6

The Add New Screen dialog box appears. Select the "New Data Screen" from the Screen Template, under screen information, choose "Employee" under screen data and provide some name to the Screen, uncheck the Employee Detail option and click the "OK" button.

New Data Src.jpg

Step 7

The Screen Designer appears for the New Data Screen.

src desi.jpg

Step 8

This time we will add a Search Data Screen as screen type, provide the screen name and data and click the "OK" button.

search src.jpg

Step 9

The Screen Designer for the Search Data Screen appears. Note that there is no DOJ field in the screen designer.

search designer.jpg

In order to add the field, drag it onto the command bar.

add doj to screen.jpg

Change its control type to label.

Choose Label.jpg

Step 10

Press F5 to run the application. Provide the data for the new data screen and click on the "Save" button. Here we will see that there is no DOJ field.

output1.jpg

Now go to the Taskbar and click on the "Search Employees Screen". Note that today's date will be successfully entered.

output2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all