Insert Data Using Silverlight RIA Enabled Service

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in Silverlight.

Question: What is insert data using Silverlight RIA enabled service?

In simple terms "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 "Company" with an employee table in it.
 

TableDesign1.png

 

Step 2: Open Visual Studio and create a new Silverlight application enabled with RIA Services.

 

Step 3: When the project is created, right-click on the RIA Service project and add a new entity data model framework and set it up for the previously created database.

 

Step 4: Again right-click on the RIA service project and add a Domain Service Class as a new item.

 

Step 5: The complete code of EmployeeDomain.cs looks like this (Domain Service Class):

 

namespace SilverlightRIAInsert.Web

{

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.DataAnnotations;

    using System.Data;

    using System.Linq;

    using System.ServiceModel.DomainServices.EntityFramework;

    using System.ServiceModel.DomainServices.Hosting;

    using System.ServiceModel.DomainServices.Server;

    // Implements application logic using the CompanyEntities context.

    // TODO: Add your application logic to these methods or in additional methods.

    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access

    // Also consider adding roles to restrict access as appropriate.

    // [RequiresAuthentication][EnableClientAccess()]public class EmployeeDomain : LinqToEntitiesDomainService<CompanyEntities>{

    // TODO:// Consider constraining the results of your query method.  If you need additional input you can

    // add parameters to this method or create additional query methods with different names.

    // To support paging you will need to add ordering to the 'Employee'

    query.public IQueryable<Employee> GetEmployee()

{

    return this.ObjectContext.Employee;

}

    public void InsertEmployee(Employee employee)

{

    if ((employee.EntityState != EntityState.Detached))

{

    this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);

}

    else

{

    this.ObjectContext.Employee.AddObject(employee);

}

}

    public void UpdateEmployee(Employee currentEmployee)

{

    this.ObjectContext.Employee.AttachAsModified(currentEmployee, this.ChangeSet.GetOriginal(currentEmployee));

}

    public void DeleteEmployee(Employee employee)

{

    if ((employee.EntityState != EntityState.Detached))

{

    this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Deleted);

}

    else

{

    this.ObjectContext.Employee.Attach(employee);

    this.ObjectContext.Employee.DeleteObject(employee);

}

}

}

}

 

Step 6: Now rebuild the solution file.

 

Step 7: the Complete code of MainPage.xaml looks like this:
 

<usercontrolx:class="SilverlightRIAInsert.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="543"><Gridx:Name="LayoutRoot">

   <TextBlockHeight="23"HorizontalAlignment="Left"Margin="152,29,0,0"Name="textBlock1"Text="FirsName"FontFamily="Verdana"FontSize="15"VerticalAlignment="Top"/>

   <TextBoxHeight="23"HorizontalAlignment="Left"Margin="252,25,0,0"Name="textBox1"VerticalAlignment="Top"Width="120"/>

   <TextBlockHeight="23"HorizontalAlignment="Left"Margin="150,72,0,0"Name="textBlock2"Text="LastName"FontFamily="Verdana"FontSize="15"VerticalAlignment="Top"/>

   <TextBoxHeight="23"HorizontalAlignment="Left"Margin="252,68,0,0"Name="textBox2"VerticalAlignment="Top"Width="120"/>

   <TextBlockHeight="23"HorizontalAlignment="Left"Margin="150,113,0,0"Name="textBlock3"Text="Age"FontFamily="Verdana"FontSize="15"VerticalAlignment="Top"/>

   <TextBoxHeight="23"HorizontalAlignment="Left"Margin="252,113,0,0"Name="textBox3"VerticalAlignment="Top"Width="120"/>

   <ButtonContent="Insert"FontFamily="Verdana"FontSize="19"Background="DeepSkyBlue"Height="44"HorizontalAlignment="Left"Margin="252,156,0,0"Name="button1"VerticalAlignment="Top"Width="120"Click="button1_Click"/>

   </Grid>

</usercontrol>
 

Step 8: The complete code of MainPage.xaml.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.ServiceModel.DomainServices.Client;

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 SilverlightRIAInsert.Web;

namespace SilverlightRIAInsert

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

        EmployeeDomain objDomain = new EmployeeDomain();

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            EntityQuery<Employee> query = objDomain.GetEmployeeQuery();

            objDomain.Load(query, InsertLoad, null);

            MessageBox.Show("Data Inserted Successfully");

        }

        public void InsertLoad(LoadOperation<Employee> emp)

        {

            Employee objEmployee = new Employee();

            objEmployee.FirstName = textBox1.Text;

            objEmployee.LastName = textBox2.Text;

            objEmployee.Age = int.Parse(textBox3.Text);

            objDomain.Employees.Add(objEmployee);

            objDomain.SubmitChanges();

            textBox1.Text = string.Empty;

            textBox2.Text = string.Empty;

            textBox3.Text = string.Empty;

        }

    }

}

Step 9: The output of the application looks like this:

 

OutPut_Result1.png
 

Step 10: Data entering output of application looks like this:

 

OutPut_Result2.png
 

Step 11: Data inserted output of the application looks like this:

 

OutPut_Result3.png

I hope this article is useful for you.

Next Recommended Readings
MVC Corporation
MVC Corporation is consulting and IT services based company.