.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

LINQ To SQL In Silverlight Tutorial

Posted By:Diptimaya Patra       Posted Date: May 14, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

LINQ To SQL In Silverlight Tutorial. In this Tutorial we would see how can we use Linq to Sql ORM in Silverlight Application for interactinig with SQL Server Database.
 

Introduction

In this article we would see how can we use Linq to Sql ORM in Silverlight 3 Application for interactinig with SQL Server Database.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as LINQ2SQLSL3.

LinqToSQLSilverlight1.gif

Now that you have created the Silverlight 3 Application, we would add a LINQ to SQL file that is *.dbml .

LinqToSQLSilverlight2.gif

For your information, I have created a Database named "EmployeeDB" with a table "EMP_BASIC_INFO" with the following details.

LinqToSQLSilverlight3.gif

Now, connect to the SQL Server from Server Explorer as shown in below figure.

LinqToSQLSilverlight4.gif

Drag and Drop the above above displayed table to the Object Relational Designer for Linq to SQL.

LinqToSQLSilverlight5.gif

Now we would change the above names to be user friendly.

LinqToSQLSilverlight6.gif

To interact with LINQ to SQL we need to have a service that client can call at any time.

Add Silverlight enabled WCF Service to the Web Project.

LinqToSQLSilverlight7.gif

In the Service.svc.cs add methods as per your requirement.

I have written two methods such as:

LinqToSQLSilverlight8.gif

And

LinqToSQLSilverlight9.gif

After writing the service build the project and get this service's reference in Silverlight 3 Project as shown in below figure.

LinqToSQLSilverlight10.gif

After you add the service it would create the ServiceReferences.ClientConfig file.

Now it's time to design our Silverlight UI in Blend, I have gone for a simple approach, where simple buttons and textbox would help us getting the result which would be displayed in DataGrid.

LinqToSQLSilverlight11.gif

The following XAML is for your reference:

LinqToSQLSilverlight12.gif

Code view of the above Image


	
		
		
	


	
	
	
	



	


Now we would write client side code to call the service and display the result.

Following code is for Show All Button Click and displaying the result.

LinqToSQLSilverlight13.gif

Code View of the above Image

private void btnShowAll_Click(object sender, RoutedEventArgs e)
{
    ServiceClient webService = new ServiceClient();
    webService.GetAllEmployeesCompleted += new 
        EventHandler(webService_GetAllEmployeesCompleted);
    webService.GetAllEmployeesAsync();
}

void webService_GetAllEmployeesCompleted(object sender, GetAllEmployeesCompletedEventArgs e)
{
    dgResults.ItemsSource = e.Result;
}


Following code is for Show the text typed in txtSearch and based on that the result would be displayed.

LinqToSQLSilverlight14.gif

Code View of the above Image

private void btnSearch_Click(object sender, RoutedEventArgs e)
{
    ServiceClient webService = new ServiceClient();
    webService.GetEmployeeByFirstNameCompleted += new 
        EventHandler
        (webService_GetEmployeeByFirstNameCompleted);
    webService.GetEmployeeByFirstNameAsync(txtSearch.Text);
}

void webService_GetEmployeeByFirstNameCompleted
    (object sender, GetEmployeeByFirstNameCompletedEventArgs e)
{
    dgResults.ItemsSource = e.Result;
}


I guess that's it. Now we can test our application.

LinqToSQLSilverlight15.gif

When some text is typed and Search Button is clicked all the Employee whose name starts with the given text would be displayed.

LinqToSQLSilverlight16.gif

You can also download the sample application used in the above example

Hope this article helps.


 Subscribe to Articles

     

Further Readings:

Responses
Author: Withers         Company URL: http://www.dotnetspark.com
Posted Date: November 15, 2010

I'm having some issues with this! is there anyway i could send you my project for help or you could figure out my issue?


System.ServiceModel.CommunicationException was unhandled by user code
Message=The remote server returned an error: NotFound.
StackTrace:
at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
at Drug_Search.MyServiceReference.ServiceClient.ServiceClientChannel.EndGetAllEmployees(IAsyncResult result)
at Drug_Search.MyServiceReference.ServiceClient.Drug_Search.MyServiceReference.Service.EndGetAllEmployees(IAsyncResult result)
at Drug_Search.MyServiceReference.ServiceClient.OnEndGetAllEmployees(IAsyncResult result)
at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
InnerException:


System.ServiceModel.CommunicationException was unhandled by user code
Message=The remote server returned an error: NotFound.
StackTrace:
at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
at Drug_Search.MyServiceReference.ServiceClient.ServiceClientChannel.EndGetEmployeeByFirstName(IAsyncResult result)
at Drug_Search.MyServiceReference.ServiceClient.Drug_Search.MyServiceReference.Service.EndGetEmployeeByFirstName(IAsyncResult result)
at Drug_Search.MyServiceReference.ServiceClient.OnEndGetEmployeeByFirstName(IAsyncResult result)
at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
InnerException:


Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend