How to consume WCF Service and fill datagrid from database in Silverlight?


Step 1.Create one table i.e tblEmp with Id,EmpName,EmpAddress field

Step2:Create one class file Emp.cs
and declare property field like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SilverlightApplication1.Web
{
public class Emp
{
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpAddress { get; set; }

}
}

step3:Create one SilverlightEnabled WCF File like this image and write this code

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SilverlightApplication1.Web
{
[ServiceContract(Namespace = “”)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
SqlConnection con = new SqlConnection(“Data

Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User

Instance=True”);
[OperationContract]
public List<Emp>  GetAllEmp()
{
List<Emp> Emps=new List<Emp>();
con.Open();
SqlCommand cmd = new SqlCommand(“Select *from tblEmp”, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Emp emp1 = new Emp();
emp1.Id = int.Parse(dr[“Id”].ToString());
emp1.EmpName = Convert.ToString(dr[“EmpName”]);
emp1.EmpAddress = Convert.ToString(dr[“EmpAddress”]);
Emps.Add(emp1);
}
con.Close();
return Emps;
}

}
}

Step4: Consume the WCF like this image

Step5: In silverlight page, drag and drop datagrid control and give some name like this

<data:DataGrid x:Name=”GridTest” Height=”150″ Width=”250″ Background=”LightGray”
RowBackground=”LightYellow” AlternatingRowBackground=”LightBlue”  />

Step6: add the namespace in codebehind file

using SilverlightApplication1.ServiceReference1;

Step7: write this code in code behind file and fill the datagrid

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 SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
FillDataGrid();

}

private void FillDataGrid()
{
Service1Client client = new Service1Client();
client.GetAllEmpCompleted += new

EventHandler<GetAllEmpCompletedEventArgs>(client_GetAllEmpCompleted);
client.GetAllEmpAsync();

}

void client_GetAllEmpCompleted(object sender, GetAllEmpCompletedEventArgs e)
{
GridTest.ItemsSource = e.Result;

}
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.