Click here to Skip to main content
Click here to Skip to main content

Update Data in SQL Server via WCF REST Service in Windows 8 XAML Store Application

, 15 Sep 2013
Rate this:
Please Sign up or sign in to vote.
How to update data in SQL Server via WCF REST Service in Windows 8 XAML Store application

Introduction

Window 8 XAML, HTML/JavaScript store applications can display the data from WCF service properly. However, up to now, WinJS in Window 8 HTML/JavaScript seems not to allow applications to do CRUD unitofwork on SQL server. Only Window 8 XAML store can do CRUD on SQL server via WCF service. This tip will show you the details of this functionality and document this as a backup.

Main Tasks

1. Create a WCF Service

Select WCF from VS 2012 and create a WCF service application. This WCF service will use Entity framework to connect to SQL Server (localdb)\v11.0. An entity model is created as below:

namespace WcfSerforwind8
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class EFDBSalesEntities : DbContext
    {
        public EFDBSalesEntities()
            : base("name=EFDBSalesEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet< Product> Products { get; set; }
    }
}

namespace WcfSerforwind8
{
    using System;
    using System.Collections.Generic;

    public partial class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public string Type { get; set; }
        public string Description { get; set; }
        public string maker { get; set; }
        public Nullable< System.DateTime> make_date { get; set; }
        public Nullable< System.DateTime> expired_date { get; set; }
        public Nullable< decimal> unit_price { get; set; }
        public Nullable< int> stock_level { get; set; }
        public Nullable< bool> deleted { get; set; }
    }
}

Four CRUD operation, contracts are developed in this C# based WCF service. See below codes shown:

namespace WcfSerforwind8
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
         void insertData(int id, string name);
        [OperationContract]
        List< Product> Getallproducts();
        [OperationContract]
         void deletedata(int id);
        [OperationContract]
        void updatedata(int id, string name);
    }
}
namespace WcfSerforwind8
{
    public class Service1 : IService1
    {
       EFDBSalesEntities efb = new EFDBSalesEntities();
        public void insertData(int id, string name) {
            efb.Products.Add(new Product { ProductId =id, Name =name });
            efb.SaveChanges();
        }
        public List< Product> Getallproducts()
        {
             return (from pp in efb.Products select pp).ToList();
        }
        public void deletedata(int id)
        {
            Product pdt = efb.Products.First(pp => pp.ProductId == id);
            efb.Products.Remove(pdt);
            efb.SaveChanges();
        }
        public void updatedata(int id, string name)
        {
            Product pdt = efb.Products.First(pp => pp.ProductId == id);
            pdt.Name = name;
            efb.SaveChanges();
        }
}
}

2. Develop a Windows 8 XAML Store Application in Blend

We need to simply add product ID and Product name into SQL server database and then display all added data in screen. Therefore, the following mainpage.xaml interface is created in Blend.

3. Add Code Behind in Windows 8 XAML Store Application

Now we need to add WCF web service reference into this Windows 8 XAML store application and update the code behind of mainpage.xaml as below:

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        ServiceReference1.Service1Client scl;
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            scl= new ServiceReference1.Service1Client();
        }
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await scl.insertDataAsync(Convert .ToInt32 (txtid.Text .Trim  ()),txtnm.Text);
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            gvproduct.ItemsSource = scl.GetallproductsAsync().Result.ToList ();
        } 
 	private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            await scl.updatedataAsync(Convert.ToInt32(txtid.Text.Trim()), txtnm.Text);
        }

        private async void Button_Click_3(object sender, RoutedEventArgs e)
        {
            await scl.deletedataAsync(Convert.ToInt32(txtid.Text.Trim()));
        }
    }
}

After we add a new product into SQL server and click Display button, we can see the final result as the image shown below:

4. Summary

We can find out that Windows 8 XAML store application can easily manipulate data in SQL server via REST WCF service and entity framework.

License

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

About the Author

lizhong huang
Software Developer
Australia Australia
Movement is life
Renew yourself to survive

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberhmshafeeq22-Sep-13 23:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Sep 2013
Article Copyright 2013 by lizhong huang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid