5,699,997 members and growing! (23,680 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Beginner License: The Code Project Open License (CPOL)

ASP.Net 3.5 Sample Application of LINQ, WFC, JSON and AJAX

By ToddHileHoffer

A Simple Sample of LINQ, WCF, JSON and AJAX
C# (C# 3.0, C#), Javascript, HTML, XHTML, .NET (.NET, .NET 3.5), ASP.NET, WCF, Ajax, LINQ, Dev

Posted: 4 Sep 2008
Updated: 4 Sep 2008
Views: 6,879
Bookmarked: 19 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.61 Rating: 3.61 out of 5
1 vote, 10.0%
1
1 vote, 10.0%
2
3 votes, 30.0%
3
1 vote, 10.0%
4
4 votes, 40.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

The purpose of this article is to provide a working sample of a new .net technologies. This sample contains a working example of calling a WCF service from AJAX and using the results in JavaScript via JSON. It will also provide you with an example of how to call a stored procedure in LINQ. This sample uses the Northwind database that you can download from MS.In order to run the sample you must change the connection string "NorthwindConnectionString" in the web.config to point to your copy of Northwind. The SearchCustomers stored procedure code is provided in a text file in the solution.

Background

Often developers are to busy actually working on applications to take the time to learn all of the new tools provided by MS. I have seen some examples of LINQ, WCF and JSON but I wanted to put a simple example of each in one place.

An Entity class to use with JSON

The class below is an entity that will be accessible in JavaScript. In order to access the properties via JSON you need to add the appropriate attributes.

    
using System;
using System.Runtime.Serialization;
 

namespace WCF_AJAX.Data
    {
    [Serializable]
    [DataContract]
    //Take note of the attributes Serializable, DataContract and DataMember
    //These are what make the object easily usable to you via JSON
    public class CustomerItem
    {   

        [DataMember]
        public string CustomerID {get;  set;}

        [DataMember]
        public string ContactName { get; set; }

        
        public CustomerItem()
        {

        }


    }
}

Accessing the Entity object via JSON

The markup file below gives an example of using the list of CustomerItems that is returned from the WCF Service. In order to use the javascript below you need to have a script manager and a reference to the ApplicationService (the WCF).

 <asp:scriptmanager id="ScriptManager1" runat="server">
            <services>
                <asp:servicereference path="~/Services/ApplicationService.svc">
            </asp:servicereference>
        </services>
</asp:scriptmanager>"

The Javascript

 
   function Search() {
       var textInput =$get("txtSearch");
       ApplicationService.searchCustomers(textInput.value, OnSearchComplete, OnError);
       $get("ddlCustomers").focus();
   }

    function OnSearchComplete(result) {
       var selectOfCustomers = $get("ddlCustomers");
       var customerItems = result;
       var pos = 0;
       for (var customerNo in customerItems) {
          var oOption = document.createElement("OPTION");
          oOption.text = customerItems[customerNo].ContactName;
          oOption.value = customerItems[customerNo].CustomerID
          selectOfCustomers.add(oOption, pos);
          pos += 1;
       }
    }

    function OnError(result) {
       alert(result.get_message());
    } 

Creating the WCF Service using LINQ to get your data

The class below is the WCF called from the client. The AspNetCompatibilityRequirements attributes are required.

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Collections.Generic;

namespace WCF_AJAX.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ApplicationService
    {
        // Add [WebGet] attribute to use HTTP GET
        [OperationContract]
        public ListsearchCustomers(string searchText)
        {
            var listOfCustomerItems = new List();
            var dataContext = new Data.NorthwindDataContext();
            foreach (var result in dataContext.SearchCustomers(searchText))
            {
                var item = new Data.CustomerItem();
                item.CustomerID = result.CustomerID;
                item.ContactName = result.ContactName;
                listOfCustomerItems.Add(item);
            }
            return listOfCustomerItems;
        }

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

The NorthwindDataContext Class was created by the designer. There is no coding to create the class. In order to use a stored procedure you have to chose "Add New Item" to the solution and pick LINQ to SQL Classes. VS 2008 will add a dbml file to the project. Then open the dbml file in design mode and drag the stored procedure from the server explorer tab's Data Connections.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ToddHileHoffer


Life.
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralWhat changes need to be done in cross domain case?memberprasad v pathak2:52 11 Nov '08  
QuestionNot getting jsdebug filememberprasad v pathak23:29 10 Nov '08  
GeneralReminder: This Does Not Work Across DomainsmemberKen Cox12:18 18 Oct '08  
GeneralTitlememberPIEBALDconsult17:33 4 Sep '08  
GeneralRe: TitlememberToddHileHoffer3:07 5 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Sep 2008
Editor: Chris Maunder
Copyright 2008 by ToddHileHoffer
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project