Saturday 7 April 2012

How to insert , update , delete in sliver light

Code of wcf enable sliver light :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using DatabaseModel;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
    DatabaseEntities o = new DatabaseEntities();
    [OperationContract]
    public void DoWork()
    {
        // Add your operation implementation here
        return;
    }

    [OperationContract]
    public void INSERT(BO p)
    {
        EMP q = new EMP();
        q.EID = p.EID;
        q.NAME = p.NAME;
        o.EMPs.AddObject(q);
        o.SaveChanges();
    }
    [OperationContract]
    public void UPDATE(BO p)
    {
        var m = (from x in o.EMPs where x.EID == p.EID select x).First();
        m.NAME = p.NAME;
        o.SaveChanges();
    }
    [OperationContract]
    public void DELETE(BO p)
    {
        var m = (from x in o.EMPs where x.EID == p.EID select x).First();
        o.EMPs.DeleteObject(m);
        o.SaveChanges();
    }
    [OperationContract]
    public IList<EMP> VIEW()
    {
        var m = from x in o.EMPs select x;
        return m.ToList();
    }
   
}
public class BO
{
    [DataMember]
    public int EID
    { get; set; }
    [DataMember]
    public string NAME
    { get; set; }
}
 then design the XAML page

<UserControl x:Class="SilverlightApplication.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="400" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Top" Height="338" Width="400">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="172*" />
            <ColumnDefinition Width="228*" />
        </Grid.ColumnDefinitions>
        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="60,57,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="EID" Grid.ColumnSpan="2" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="132,53,0,0" Name="textBox1" VerticalAlignment="Top" Width="200" Grid.ColumnSpan="2" />
        <sdk:Label Content="NAME" Height="28" HorizontalAlignment="Left" Margin="60,91,0,0" Name="label2" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="132,87,0,0" Name="textBox2" VerticalAlignment="Top" Width="200" Grid.ColumnSpan="2" />
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="132,116,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" Grid.ColumnSpan="2">
            <Button.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF37EF28" Offset="0.934" />
                    <GradientStop Color="#FFCAEFF2" Offset="1" />
                    <GradientStop Color="#FF7CA6B7" Offset="0.352" />
                </LinearGradientBrush>
            </Button.Background>
        </Button>
        <sdk:DataGrid AutoGenerateColumns="False" Height="183" HorizontalAlignment="Left" Margin="12,150,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="376" Grid.ColumnSpan="2" SelectionChanged="dataGrid1_SelectionChanged" RowEditEnded="dataGrid1_RowEditEnded">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="EID" IsReadOnly="True" Width="50" Binding="{Binding EID}"></sdk:DataGridTextColumn>
                <sdk:DataGridTextColumn Header="NAME" IsReadOnly="False" Width="150" Binding="{Binding NAME}"></sdk:DataGridTextColumn>
                <sdk:DataGridTemplateColumn Header="UPDATE" Width="90">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <HyperlinkButton x:Name="lb" Content="Edit" Width="40" Click="lb_Click"></HyperlinkButton>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn Header="DELETE" Width="90">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <HyperlinkButton x:Name="lb1" Content="Delete" Width="40" Click="lb1_Click"></HyperlinkButton>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
        <sdk:Label Height="28" Foreground="Green" HorizontalAlignment="Left" Margin="132,12,0,0" Name="label3" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" />
    </Grid>
</UserControl>

Code of XAML.CS
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;

namespace SilverlightApplication
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            view();
        }
        ServiceReference1.ServiceClient o = new ServiceReference1.ServiceClient();
        ServiceReference1.BO p = new ServiceReference1.BO();
        ServiceReference1.EMP q = new ServiceReference1.EMP();
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            p.EID =Convert.ToInt32(textBox1.Text);
            p.NAME = textBox2.Text;
            o.INSERTCompleted+=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(x);
            o.INSERTAsync(p);
        }
        void view()
        {
            o.VIEWCompleted+=new EventHandler<ServiceReference1.VIEWCompletedEventArgs>(y);
            o.VIEWAsync();
        }
        void y(object sender, ServiceReference1.VIEWCompletedEventArgs e)
        {
            IEnumerable<ServiceReference1.EMP> m = e.Result as IEnumerable<ServiceReference1.EMP>;
                dataGrid1.ItemsSource=m;
        }
        void x(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

            label3.Content = "One record saved.";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox1.Focus();
            view();
        }

        private void lb_Click(object sender, RoutedEventArgs e)
        {
            p.EID = q.EID;
            p.NAME = q.NAME;
            o.UPDATECompleted+=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(z);
            o.UPDATEAsync(p);
        }
        void z(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            label3.Content = "One record updated.";
            view();
        }
        private void lb1_Click(object sender, RoutedEventArgs e)
        {
            p.EID = q.EID;
            p.NAME = q.NAME;
           o.DELETECompleted+=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(zz);
           o.DELETEAsync(p);
        }
        private void zz(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

            label3.Content = "One record deleted.";
            view();
        }
        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            q = (ServiceReference1.EMP)dataGrid1.SelectedItem;
        }

        private void dataGrid1_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
        {
            FrameworkElement fe = dataGrid1.Columns[0].GetCellContent(e.Row);
            q.EID = Convert.ToInt32(((TextBlock)fe).Text);
            FrameworkElement fe1 = dataGrid1.Columns[1].GetCellContent(e.Row);
            q.NAME = ((TextBlock)fe1).Text;
        }

    }
}

the page looks like.

No comments:

Post a Comment