Introduction:
In this article let’s know how to insert and retrieve data using Silverlight
RIA enabled service.
In other words "This application enables insertion of data in
the database with the help of the Silverlight RIA enabled service. It uses a
base platform of Entity Framework to communicate with the database".
Step 1: Create a database named "Peers" with a Silverli8db table in it.
Step 2: Create New
Silverlight Application
New project >> Select Silverlight Application.
New project >> Select Silverlight Application.
Step 3: Check
"Enable WCF RIA Services" check box
Step 4: Create
Entity Data model.
Step 5: Select
tables, views and stored procedure from given list to add it to the entity
data model.
Step 6: Create
Domain Service.
Note:* Before adding a new domain service, build the web project.
Note:* Before adding a new domain service, build the web project.
Step 7: Map or
enable entity(s) with domain service. In the Entities list you get all tables
(also called entities) available in the entity model.
Step 8: Now build
both projects; Silverlight as well as web. After compiling you will have
automatically generated code in the Silverlight project which is responsible
for communication between Silverlight and the Domain service.
The Domain service has many built-in methods for selecting,
inserting, updating and deleting the entity(s). You may also add new methods as
per your requirement.
Step 9: the complete code of MainPage.xaml looks like
this:
<UserControl x:Class="SilverlightInsert.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="63,10,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="Name" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="63,48,0,0" Name="label2" VerticalAlignment="Top" Width="120" Content="Place"
/>
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="63,86,0,0" Name="label3" VerticalAlignment="Top" Width="120" Content="Designation"
/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="191,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="191,43,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="191,76,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="85,158,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"
/>
<Button Content="Retrieve" Height="23" HorizontalAlignment="Left" Margin="201,158,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click"
/>
<sdk:DataGrid AutoGenerateColumns="False" Height="100" HorizontalAlignment="Left" Margin="12,188,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="376" />
</Grid>
</UserControl>
Step 10: The complete code of MainPage.xaml.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightInsert.Web;
using System.ServiceModel.DomainServices.Client;
namespace SilverlightInsert
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
DomainService1 objDomain = new DomainService1();
LoadOperation<Silverli8db>
Silverli8db = objDomain.Load(objDomain.GetSilverli8dbQuery());
dataGrid1.ItemsSource = Silverli8db.Entities;
dataGrid1.AutoGenerateColumns = true;
}
DomainService1 objDomain = new DomainService1();
private void
button1_Click(object sender, RoutedEventArgs e)
{
EntityQuery<Silverli8db>
query = objDomain.GetSilverli8dbQuery();
objDomain.Load(query, InsertLoad, null);
MessageBox.Show("Data
Inserted Successfully");
}
public void
InsertLoad(LoadOperation<Silverli8db> emp)
{
Silverli8db objEmployee = new
Silverli8db();
objEmployee.Name = textBox1.Text;
objEmployee.Place = textBox2.Text;
objEmployee.Designation =textBox3.Text;
objDomain.Silverli8dbs.Add(objEmployee);
objDomain.SubmitChanges();
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
textBox3.Text = string.Empty;
}
private void
button2_Click(object sender, RoutedEventArgs e)
{
LoadOperation<Silverli8db>
Silverli8db = objDomain.Load(objDomain.GetSilverli8dbQuery());
dataGrid1.ItemsSource = Silverli8db.Entities;
dataGrid1.AutoGenerateColumns = true;
}
//public void
selectload()
//{
//}
}
}
Now run and see the output
Demo: