How to transfer data from two tables using LightSwitch in Visual Studio 2012
This article describes how to transfer data from two tables using LightSwitch in Visual Studio 2012.
Procedure for transfering data from two tables.
Step 1
- Open the Visual Studio 2012.
- Go to "File" => "New" => "Project..."
- In "New Project" => "Installed" => "Template"
- In "Template" => "LightSwitch"
- Select "LightSwitch Application (Visual C#)".
- Enter the Name and choose the location.
- Click "OK".
Step 2
Go to the Solution Explorer, right-click on the Data Source and select "Add Table".
Step 3
In the following step we will create three tables. The first two tables (Table1 and Table2) contain the various data and the third table will contain the data from both Table1 and Table2.
Insert the data into Table1:
Similarly, inserting the data in Table2 and Table3:
Table3 is used to have the combined data from Table1 and Table2.
Step 4
Now in this following step we will add a screen for Table1 as in the following:
Now the Appliaction Designer appears.
Step 5
From the Application Designer click on the Add Data Item Option in the Menu Bar to add the second table.
Step 6
The Add Data Item Dialog Box appears:
Step 7
Select the Query option and from the query option select the second table.
Step 8
Note that both tables have been added together.
Step 9
Add another screen since we will bind the TransferData Table with the following screen:
Step 10
In the Application Designer of the Table3Set choose the SearchTable3Set_created method from the Write Code option provided in the Menu Bar:
Step 11
For getting the records from both the tables we perform the following coding:
IEnumerable<Table1> obj1 = this.DataWorkspace.ApplicationData.Table1Set.GetQuery().Execute();
IEnumerable<Table2> obj2 = this.DataWorkspace.ApplicationData.Table2Set.GetQuery().Execute();
Inserting the Table1 record into Table3 we will perform the following coding:
foreach (var item in obj1)
{
Table3 newRecord = Table3Set.AddNew();
newRecord.TransferData = item.Item1;
}
Inserting the Table2 record into Table3 we will perform the following coding:
foreach (var item in obj1)
{
Table3 newRecord = Table3Set.AddNew();
newRecord.TransferData = item.Item1;
}
For sorting the table3 record by TransferData:
Table3Set.OrderBy(sort => sort.TransferData);
Step 12
We will do the following coding for transferring the data:
using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
public partial class SearchTable3Set
{
partial void SearchTable3Set_Created()
{
IEnumerable<Table1> obj1 = this.DataWorkspace.ApplicationData.Table1Set.GetQuery().Execute();
IEnumerable<Table2> obj2 = this.DataWorkspace.ApplicationData.Table2Set.GetQuery().Execute();
foreach (var item in obj1)
{
Table3 newRecord = Table3Set.AddNew();
newRecord.TransferData = item.Item1;
}
foreach (var item in obj2)
{
Table3 newRecord = Table3Set.AddNew();
newRecord. TransferData = item.AnotherItem;
}
Table3Set.OrderBy(sort => sort.TransferData);
}
}
}
Press F5 to run the application.