Objective
In this tutorial, We will fetch data from SQL Server database using LINQ and display that data in Silver Light, while we are following ASP.Net MVC Framework
Step 1
Create a Silver Light application.
File -> New -> Project -> Silver Light
Step 2
To hosting SILVERLIGHT application there are three options available for hosting a Silver Light application. Select ASP.Net MVC Web project. Then after, Select option NO for creating UNIT Test Project.
Step 3
Right click on Controller. Click on Add-> New Item and select add LINQ to SQL classes. Give a name.
Step 4
Here, I have already created a TEST data base in my database. I am going to display data from this database.
- Open Server Explorer
- Right click on Data Connection
- Click Add New Data Connection.
Now give a Server name and select database to connect
Expand test.dbo. Here there are 2 tables in Test database. One is Test_Details and other is testsample. In this tutorial, I am going to display data from Test_Detatils table. So select this table from Server explorer and drag it on Test.dbml page. Now if you click on Test.Dbml.CS file. You will find code has been created for you.Up to this step, Linq class has been created.
Step 5
We will add here one more action called List inside Home Controller. Purpose of this action is to fetch out all records of Test_Details table and return List of records as JSON. Add following code in Home Controller class. Don’t forget to include at the top of HomeController.cs .
using SilverLightMvcTest.Web.Models;public ActionResult List()
{ TestDataContext _obj = new TestDataContext(); var _res = from r in _obj.Test_Details select r; return Json(_res); } |
The above action List is returning JSON of all the records fetch from Test_Dtails table of Test Database.So now the complete code of HomeController.cs will look like,
HomeController.cs
using System;using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.Mvc; using SilverLightMvcTest.Web.Models; namespace SilverLightMvcTest.Web.Controllers { [HandleError] public class HomeController : Controller public ActionResult Index() { ViewData[“Message”] = “Welcome to ASP.NET MVC!”; return View(); } public ActionResult About() { return View(); } public ActionResult List() { TestDataContext _obj = new TestDataContext(); var _res = from r in _obj.Test_Details select r; return Json(_res); } } } |
Step 6
Now click on View -> Home -> Right Click -> Add -> View.Give name of the View exactly of action name. Here action name is List so name of view will be List. Leave other things as default and click on Add. It will create a List.aspx page.
List.aspx will have following code.
<%@ Page Title=”” Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master” Inherits=”System.Web.Mvc.ViewPage” %> <asp:Content ID=”Content1″ ContentPlaceHolderID=”head” runat=”server”> <title>List</title> </asp:Content> <asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”> <h2>List</h2> </asp:Content> |
Remove
<h2>List</h2>
And add below code
<p><object data=””data:application/x-silverlight-2” type=”application/x-silverlight-2″ width=”300px” height=”300px”> <param name=”source” value=”/ClientBin/SilverLightMvcTest.xap“/> <param name=”minRuntimeVersion” value=”2.0.31005.0″ /> <param name=”windowless” value=”true” /> <param name=”Background” value=”#00FFFFFF” /> </object> </p> |
Here at value property of source, name is SilverLightMvcTest.xap . Here make sure you are giving the name, which name you gave to your SilverLight Project at Step 1
So now complete code for List.aspx would be
List.aspx
<%@ Page Title=”” Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master” Inherits=”System.Web.Mvc.ViewPage” %> <asp:Content ID=”Content1″ ContentPlaceHolderID=”head” runat=”server”> <title>List</title> </asp:Content> <asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”> <p> <object data=””data:application/x-silverlight-2” type=”application/x-silverlight-2″ width=”300px” height=”300px”> <param name=”source” value=”/ClientBin/SilverLightMvcTest.xap” /> <param name=”minRuntimeVersion” value=”2.0.31005.0″ /> <param name=”windowless” value=”true” /> <param name=”Background” value=”#00FFFFFF” /> </object> </p> </asp:Content> |
Step 8
Page.Xaml.cs
<UserControl x:Class=”SilverLightMvcTest.Page”xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data” Width=”400″ Height=”300″> <Grid x:Name=”LayoutRoot” Background=”White”> <Grid.RowDefinitions> <RowDefinition Height=”Auto” /> <RowDefinition Height=”Auto” /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width=”Auto” /> </Grid.ColumnDefinitions> <Button x:Name=”b1″ Height=”100″ Width=”100″ Click=”b1_Click” Grid.Row=”0″ Grid.Column=”1″/> <ColumnDefinition Width=”Auto” /> |
Step 9
Right click on SilverLight Project and add new class . Give it name Test_Details.Add following code in Test_Details.cs
Test_Details.cs
namespace SilverLightMvcTest{
public class Test_Details public string testId { get; set; } public string testName { set; get; } public int testMaxMarks { set; get; } } } |
Make sure here property name and return type is exactly same of the column name and types in table of database.
Step 10
- Run your application, by clicking F5.
- Below screen will come
-
Click on address bar and type
http://localhost:2675/home/list
Copy this URL.
Step 11
- Add a reference to System.RunTime.Serilization and System.ServiceModel.Web to SilverLight Project.
-
Include namespace
using System.Runtime.Serialization.Json to Page.Xaml.Cs
Page.Xaml.CS
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 System.Runtime.Serialization.Json; namespace SilverLightMvcTest { public partial class Page : UserControl public Page() { InitializeComponent(); } private void b1_Click(object sender, RoutedEventArgs e) { WebClient _client = new WebClient(); _client.OpenReadCompleted += newOpenReadCompletedEventHandler(_client_OpenReadCompleted); client.OpenReadAsync(newUri(“http://localhost:2675/home/list”)); } void _client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<Test_Details>)); IList<Test_Details> res = (List<Test_Details>)(json.ReadObject(e.Result)); MyGrid.ItemsSource = res; } } } |
Run the application.
Leave a Reply