Walkthrough: Inserting Data with the ServerApplicationContext
For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
The ServerApplicationContext API lets you access the middle tier (a.k.a. the server tier) of your LightSwitch application from other clients such as web applications that don't understand OData. In this walkthrough you’ll create two .aspx pages to add and view data for a LightSwitch application.
To create the LightSwitch application
On the menu bar, choose File, New, Project.
In the New Project dialog, expand the Visual Basic or Visual C# node, choose LightSwitch, and then choose the LightSwitch HTML Application template.
In the Name text box, enter
BirthdayTracker, and then choose the OK button.In Solution Explorer, open the shortcut menu for the Data Sources node and choose Add Table.
In the entity designer, choose the title bar, and then in the Properties window choose the Name property and enter
Birthday.In the entity designer, add the following fields:
Name Type Required Name String Yes BirthDate Date Yes On the Perspective bar choose HTMLClient, and then on the toolbar choose the Screen button.
In the Add New Screen dialog box choose the Common Screen Set template, name it
Birthdaysand in the Screen Data list choose Birthdays, and then choose the OK button.
To add a data entry page
In Solution Explorer, open the shortcut menu for the BirthdayTracker.Server node and choose Add, Web Form.
In the Specify Name for Item dialog box, enter
AddBirthday.aspx, and then choose the OK button.In the AddBirthday.aspx designer, add the following between the <div> and </div> tags:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Add" />
On the menu bar, choose View, Code.
In the Code Editor, replace the existing code with the following code:
public class AddBirthday : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { using (ServerApplicationContext Context = ServerApplicationContext.CreateContext()) { Birthday b = Context.DataWorkspace.ApplicationData.Birthdays.AddNew(); b.Name = TextBox1.Text; b.BirthDate = System.DateTime.Today; Context.DataWorkspace.ApplicationData.SaveChanges(); } } }
The code calls the
CreateContext()method to create a temporary instance of theServerApplicationContext, and then theAddNew()and SaveChanges() methods of the context are called to add data to the Birthdays entity.
To add a display page
In Solution Explorer, open the shortcut menu for the BirthdayTracker.Server node and choose Add, Web Form.
In the Specify Name for Item dialog box, enter
TodaysBirthdays.aspx, and then choose the OK button.On the menu bar, choose View, Code.
In the Code Editor, replace the existing code with the following code:
public class TodaysBirthdays : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { Response.Write("Celebrating their birthday today are:" + "<br>" + Constants.vbCrLf); using (ServerApplicationContext Context = ServerApplicationContext.CreateContext()) { foreach (Birthday b in Context.DataWorkspace.ApplicationData.Birthdays) { if (b.BirthDate == System.DateTime.Today) { Response.Write(b.Id.ToString() + " " + b.Name + " " + b.BirthDate + "<br>" + Constants.vbCrLf); } } } } public TodaysBirthdays() { Load += Page_Load; } }
The code calls the
CreateContext()method to create a temporary instance of theServerApplicationContext, and then loops through the Birthdays collection to return all records where the BirthDate equals today’s date.
To test the application
Run the application, and in the address bar of the browser, copy the port number portion of the URL, for example: http://localhost:12345/HTMLClient/.
Open a new browser tab and enter
http://localhost:12345/AddBirthday.aspx, replacing 12345 with the port number.On the AddBirthday web page, choose the text box and enter a name, and then choose the Add button.
In the text box, replace the name that you just entered with another name, and then choose the Add button.
Switch back to the BirthdaysSet application and refresh the page.
You should see the two names that you just entered.
Choose the Add button, and in the Birthdays dialog box enter a name and a birthdate other than the current date, and then choose the Save button.
Open a new browser tab and enter
http://localhost:12345/TodaysBirthdays.aspx, replacing 12345 with the port number.You should see two entries for the names that you entered on the AddBirthday web page, but not the one you entered in the application itself.
LightSwitch ServerApplicationContext
ServerApplicationContext API Reference