
Projects By LANGUAGE
Libraries
Articles & seminars
Source Code

| Displaying SQL Database Data in a DataGrid using LINQ and WCF | |||||||||||
Getting Started | |||||||||||
To begin, create a project named SQLData, but be sure to choose Web Application Project as we want both a Silverlight project and also a Server project in which we can create a WCF Web Service (to connect to the database). |
|||||||||||
| Visual Studio 2008 will create two projects under one solution. | |||||||||||
The solution and first project are named SQLData. That first project is the Silverlight application and has the same files that you've seen in previous tutorials.The second project, SQLData_Web is created for you as a test environment for the Silverlight project and it has four potential entry points, | |||||||||||
|
|||||||||||
SQLDataTestPage.aspx is specifically designed to test the Silverlight controls and quick examination shows that it includes an AJAX ScriptManager and an ASP:Silverlight control whose source is the .xap file (pronounced zap file) that will be produced by the Silverlight project, |
|||||||||||
|
| |||||||||||
<form id="form1" runat="server" style="height:100%;"> <asp:ScriptManager ID="ScriptManager1" runat="server"/> </form> |
|||||||||||
|
|||||||||||
public interface IService1 { [OperationContract] void DoWork(); } |
|||||||||||
|
We can replace this "dummy" contract with whatever contract we want our web service to provide. For the purposes of this tutorial we want to contract that our web service will return a list of Customer objects given a string that represents the beginning of a customer's last name.Thus, we'll modify the method from returning void to returning List
.. However, as soon as you start to change the return type Intellisense is able to pop up to help you, specifically because we created this type in the LINQ class we defined earlier.
|
|||||||||||
| The convention for a method that returns a set of foo given a bar is to name it GetFoosByBar. Thus, we'll name this GetCustomersByLastName. | |||||||||||
| public interface IService1 { [OperationContract] List<Customer> GetCustomersByLastName(string lastName); } |
|||||||||||
|
| |||||||||||
|
Having changed the contract in the interface, you must be sure to change the implementation in the .cs file. But why work so hard? When you get to the cs file, just click on the interface and a smart tag will appear. Open the tag and it will offer to create the implementation skeleton for you!Throw away the DoWork method and fill in the GetCustomersByLastName with the LINQ query,
|
|||||||||||
| public class Service1 : IService1 { #region IService1 Members public List<Customer> GetCustomersByLastName(string lastName) { DataClasses1DataContext db = new DataClasses1DataContext(); var matchingCustomers = from cust in db.Customers where cust.LastName.StartsWith(lastName) select cust; return matchingCustomers.ToList(); } #endregion } |
|||||||||||
| WCF uses wsHttpBinding as its default binding, in the Web.config file, | |||||||||||
| <services> <service behaviorConfiguration="SQLData_Web.Service1Behavior" name="SQLData_Web.Service1"> <endpoint address="" binding="wsHttpBinding" contract="SQLData_Web.IService1"> <identity> <dns value="localhost"/> </identity> </endpoint> </service> </services> |
|||||||||||
| Silverlight, however, supports only basic binding (SOAP 1.1, etc.), so you will need to change the binding accordingly, | |||||||||||
| <endpoint address="" binding="basicHttpBinding" contract="SQLData_Web.IService1"> |