Oct 31 2009

How to insert data into Database using Silverlight

First Create Silverlight application

Start -> Programs -> VS2008 -> File -> New -> Project -> SilverlightApplication -> ByDefault application name is SilverlightApplication1 change name as SilverlightDB

Silverlight-solution-explorer

Silverlight-solution-explorer

Step2: Design Registration form in Page.xaml

Silverlight Grid with 8 Rows and 2 Columns using grid RowDefinition and ColumnDefinition

<UserControl xmlns:controls=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit”  x:Class=”SilverlightDB.Page”

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

Width=”400″ Height=”300″>

<Border BorderBrush=”Black” BorderThickness=”1″ >

<Grid x:Name=”LayoutRoot” Background=”White” ShowGridLines=”False” >

<Grid.RowDefinitions>

<RowDefinition Height=”35″></RowDefinition>

<RowDefinition Height=”30″></RowDefinition>

<RowDefinition Height=”30″></RowDefinition>

<RowDefinition Height=”30″></RowDefinition>

<RowDefinition Height=”30″></RowDefinition>

<RowDefinition Height=”30″></RowDefinition>

<RowDefinition Height=”40″></RowDefinition>

<RowDefinition Height=”70″></RowDefinition>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width=”130″></ColumnDefinition>

<ColumnDefinition Width=”270″></ColumnDefinition>

</Grid.ColumnDefinitions>

<StackPanel Grid.Row=”0″ Grid.Column=”0″ Grid.ColumnSpan=”2″ Width=”400″ Background=”Gray” >

<controls:Label  FontSize=”14″ FontWeight=”bold” HorizontalAlignment=”Center”

Content=”Registration Form” Margin=”6,5,-5,5″></controls:Label>

</StackPanel>

<controls:Label Grid.Row=”1″ Grid.Column=”0″ Content=”FirstName” Margin=”10,5,5,5″ HorizontalAlignment=”Center”></controls:Label>

<TextBox Grid.Column=”1″ Grid.Row=”1″ Height=”25″ Width=”150″ HorizontalAlignment=”Left”></TextBox>

<controls:Label Grid.Row=”2″ Grid.Column=”0″ Content=”EmailID” Margin=”10,5,5,5″ HorizontalAlignment=”Center”></controls:Label>

<TextBox Grid.Row=”2″ Grid.Column=”1″ Height=”25″ Width=”150″ HorizontalAlignment=”Left”></TextBox>

<controls:Label Grid.Row=”3″ Grid.Column=”0″ Content=”PhoneNo” Margin=”10,5,5,5″ HorizontalAlignment=”Center”></controls:Label>

<TextBox Grid.Row=”3″ Grid.Column=”1″ Height=”25″ Width=”150″ HorizontalAlignment=”Left”></TextBox>

<controls:Label Grid.Row=”4″ Grid.Column=”0″ Content=”LoginName” Margin=”10,5,5,5″ HorizontalAlignment=”Center”></controls:Label>

<TextBox Grid.Row=”4″ Grid.Column=”1″ Height=”25″ Width=”150″ HorizontalAlignment=”Left”></TextBox>

<controls:Label Grid.Row=”5″ Grid.Column=”0″ Content=”Password” Margin=”10,5,5,5″ HorizontalAlignment=”Center”></controls:Label>

<TextBox Grid.Row=”5″ Grid.Column=”1″ Height=”25″ Width=”150″ HorizontalAlignment=”Left”></TextBox>

<Button Grid.Row=”6″ Grid.Column=”1″ Width=”100″ Height=”25″ HorizontalAlignment=”Left” Content=”Register”></Button>

</Grid>

</Border>

</UserControl>

Run the application

Silverlight-Registration-form

Silverlight-Registration-form

CREATE TABLE [dbo].[Registration](

[registerID] [int] IDENTITY(1,1) NOT NULL,

[FirstName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[EmailID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[PhoneNo] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[LoginName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[Password] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED

(

[registerID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

Create Linq to SQL Classes

Right click in SilverlightDB.Web (see above figure)

-> Add -> New Item -> Linq to SQL classes ->

By Default Linq to SQL Classes (DataClasses1.dbml) change its name into Silverlight.dbml

-> Add -> Then it opens dbml Editor click ServerExplorer ->

Then it display Silverlight (database name in list of Databases)

Choose Silverlight

-> Right click choose Modify Connections (Modify ConnectionString by choosing ServerName : your server name

UserName : your username

Password : your password

Select Database: Silverlight

Then Expand Silvelright database to view tables in Server Explorer (View ->ServerExplorer)

silverlight-ServerExplorer

silverlight-ServerExplorer

Drag Registration table into Silverlight.dbml Editor then see Solution Explorer it creates Silverlight.dbml with two files

Silverlight.dbml.layout file

Silverlight.designer.cs file

Silverlight-dbml

Silverlight-dbml

Create WCF Service in Silverlight Project

Right click in SilverlightDB.Web (see above figure)

-> Add -> New Item -> choose Silverlight from Categories -> right side choose

Service-enabled WCF Service

silverlight enabled WCF Service

silverlight enabled WCF Service

Change Name: Service1.svc into Register.svc then click Add Button it generates below code

Register.svc.cs

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.Collections.Generic;

using System.Text;

namespace SilverlightDB.Web

{

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class Register

{

[OperationContract]

public void DoWork()

{

// Add your operation implementation here

return;

}

// Add more operations here and mark them with [OperationContract]

}

}

For inserting data using WCF and Silverlight

namespace SilverlightDB.Web

{

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class Register

{

[OperationContract]

public void DoWork()

{

// Add your operation implementation here

return;

}

// Add more operations here and mark them with [OperationContract]

[OperationContract]

public void InsertData(string firstname,string emailid,string phoneno,string loginuser,

string password)

{

//SilverlightDataContext is Dbml file Silverlight.dbml //(SilverlightDataContext)

SilverlightDataContext db = new SilverlightDataContext();

//From Silverlight.dbml.cs file Registeration(sqlserver name table name row

//  [Table(Name="dbo.Registration")]

//public partial class Registration : INotifyPropertyChanging, INotifyPropertyChanged

//{

//    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

//    private int _registerID;

//    private string _FirstName;

//    private string _EmailID;

//Registration from Silverlight.dbml table name

Registration row = new Registration()

{

FirstName = firstname,

EmailID = emailid,

PhoneNo = phoneno,

LoginName = loginuser,

Password = password

};

// Add the new object to the collection.

db.Registrations.InsertOnSubmit(row);

// Submit the change to the database.

db.SubmitChanges();

}

}

}

Add Web Service Reference to Silverlight Application (Pages like Page.xaml…)

Goto SilverlightDB in Solution Explorer -> choose Referenes -> click Add Service Reference

Add-Service-Reference

Add-Service-Reference

Then the below DailogBox Opens

Service-Reference-Discover

Service-Reference-Discover

Click Discover -> It display all WCF services which Created in Project we created only one WCF service i.e. Register.svc it displays all operations included in Register.svc

First it display Register.svc with expand and Collapse + symbol click (+) symbol . Now it displays Register webservice with expand and collapse click (+) symbol

Now it displays Register in Services and right side it displays Operations.

Operations

  • DO Work
  • Insert Data
Register-WCF-Operations

Register-WCF-Operations

Click Ok then it creates Service Reference all Silverlight Pages (page.xaml…)

It creates ServiceReference to Silverlight Application see below figure.

ServiceReferences

ServiceReferences

Insert Data into SqlServer using Silverlight Xaml file

Now goto Page.xaml (Registration form)

click in Submit Button right click

Click Navigate to Event Handler

<Button Grid.Row=”6″ Grid.Column=”1″ Width=”100″ Height=”25″ HorizontalAlignment=”Left” Content=”Register” Click=”SubmitRegister_Click” ></Button>

using SilverlightDB.ServiceReference1;

private void SubmitRegister_Click(object sender, RoutedEventArgs e)

{

RegisterClient webservice = new RegisterClient();

webservice.InsertDataAsync(txtFirstName.Text, txtEmailID.Text, txtPhoneno.Text, txtLoginName.Text, txtPassword.Text);

}

Explaination:

RegisterClient webservice=new RegisterClient()

Register is WCF service name

Client is ByDefault is Client Service

So Register+Client = RegisterClient

InsertDataAsync

InsertData = WCF service (Register.svc) Operation (Function) is assigned to insert data into database.

Async = ServiceReference to InsertData

So InsertData+Async = InsertDataAsync

Hit F5 to run the application

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy