Tuesday 13 March 2012

how to save,update and delete in sliverlights

add new projdect
cliclk add

 click ok.
right click on the sliver light project
add a database on ur project

add a table in ur database that is EMP

add sliver light enable wcf service

add code on wcf :

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data.Linq;
using System.Collections.Generic;
using System.Configuration;

namespace SilverlightApplication7.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        DataClasses1DataContext o = new DataClasses1DataContext(ConfigurationManager.ConnectionStrings["Database1ConnectionString"].ToString());
        [OperationContract]
        public void INSERT(EMP a)
        {
            EMP p = new EMP() { EID=a.EID,NAME=a.NAME };
            o.EMPs.InsertOnSubmit(p);
            o.SubmitChanges();
        }
        [OperationContract]
        public void UPDATE(EMP a)
        {
            var m = (from x in o.EMPs where x.EID == a.EID select x).First();
            m.NAME = a.NAME;
            o.SubmitChanges();
        }
        [OperationContract]
        public void DELETE(EMP a)
        {
            var m = (from x in o.EMPs where x.EID == a.EID select x).First();
            o.EMPs.DeleteOnSubmit(m);
            o.SubmitChanges();
        }
        [OperationContract]
        public IList<EMP> view()
        {
            var m = from x in o.EMPs select x;
            return m.ToList();
        }

       
    }
}
then right click on the wcf
then copy the link




then right click on the reference select


then paste the link and click go, click ok
design the silver light page
<UserControl x:Class="SilverlightApplication8.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</UserControl>
the page looks like
the code :

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.Windows.Browser;

namespace SilverlightApplication7
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            view();
        }
        IEnumerable<ServiceReference1.EMP> y;
        ServiceReference1.Service1Client o = new ServiceReference1.Service1Client();
        private void button1_Click(object sender, RoutedEventArgs e)
        {
             
            o.INSERTCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ym);
            ServiceReference1.EMP p=new ServiceReference1.EMP();
            p.EID=Convert.ToInt32(textBox1.Text);
            p.NAME=textBox2.Text;
            o.INSERTAsync(p);
         
        }
        void ym(object m,System.ComponentModel.AsyncCompletedEventArgs e)
        {
              System.Windows.Browser.HtmlPage.Window.Alert("One record saved.");
            view();
        }
        void view()
        {
            o.viewCompleted +=new EventHandler<ServiceReference1.viewCompletedEventArgs>(x);
            o.viewAsync();
        }
        public void x(object m,ServiceReference1.viewCompletedEventArgs e)
        {
             y = e.Result as IEnumerable<ServiceReference1.EMP>;
            dataGrid1.ItemsSource = y;
        }
        int i;
        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          ServiceReference1.EMP p1 = new ServiceReference1.EMP();
            p1 =(ServiceReference1.EMP)dataGrid1.SelectedItem;
            textBox1.Text =p1.EID.ToString();
            textBox2.Text = p1.NAME.ToString();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            o.UPDATECompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(o_UPDATECompleted);
           ServiceReference1.EMP p=new ServiceReference1.EMP();
            p.EID=Convert.ToInt32(textBox1.Text);
            p.NAME=textBox2.Text;
            o.UPDATEAsync(p);
          
        }
        void o_UPDATECompleted(object m,System.ComponentModel.AsyncCompletedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Alert("One record updated.");
             view();
             System.Windows.Browser.HtmlPage.Document.Submit();
        }
        private void button3_Click(object sender, RoutedEventArgs e)
        {
           o.DELETECompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(o_DELETECompleted);
           ServiceReference1.EMP p = new ServiceReference1.EMP();
           p.EID = Convert.ToInt32(textBox1.Text);
           o.DELETEAsync(p);
           
        }
        void o_DELETECompleted(object m, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Alert("One record deleted.");
            view();
            System.Windows.Browser.HtmlPage.Document.Submit();
        }
    }
}


No comments:

Post a Comment