Pages

Wednesday, 8 June 2011

Silverlight

Silverlight 4 Sample program to Insert data through coding:
      Hi Dear friends in this post i want to tell about basic level of inserting the data in sql server throuth silverlight application code. Enjoy with this post(మీ అశోక్).
What is Silverlight ?
Silverlight enables development of the next generation of Microsoft .NET-based media experiences and rich interactive applications (RIAs) for the Web. Silverlight is delivered as a cross-platform and cross-browser plug-in that exposes a programming framework and features that are a subset of the .NET Framework and Windows Presentation Foundation (WPF).


1) SAMPLE SILVERLIGHT 4 (C#) APPLICATION TO INSERT THE DATA:

Here Some of the steps have to follow to create a silverlight Application.

  1.  Design the database table [ CREATE TABLE INFO(ID int IDENTITY(1,1) PRIMARY KEY, NAME VARCHAR(30), AGE INT, QUALIFICATION VARCHAR(30)) ].
  2. Design the MainPage.xaml GUI Template like this(Project Name: PersonalInfo).
  3. Go to Solution Explorer, Right click on PersonalInfo.web, and Add NewItem->Select .edmx template, Rename it as personal.edmx, Follow the wizard Select database and Table(INFO) click Finish Button.
  4. Now you can see the Table Properties in personal.exmx file, Build the project.
  5. Now Once again go to Solution Explorer, Right click on PersonalInfo.web, and Add NewItem->Select DomainService.cs Rename it personalDomainService.cs, And Select the Entity Table(INFO) Check the Edit Enable then Click ok, Now build the Project.
  6. Now go to MainPage.xaml.cs Create the DomainContext Object like below.
 namespace PersonalInfoSL
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using PersonalInfoSL.Web;
    using System.Collections;
    using System.Diagnostics;
    using PersonalInfoSL.Helpers;
    using System;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
      
        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();
            txtName.Focus();
            this.Title = ApplicationStrings.HomePageTitle;

            EmpInfo empinfo=new EmpInfo();
            LayoutRoot.DataContext = empinfo;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            if ((txtName.Text.Length != 0) && (txtAge.Text.Length != 0) && (txtQualification.Text.Length != 0))
            {
                //Calling DomainContext
                personalDomainContext employeeDomainContext = new personalDomainContext();
                //Creating Object for INFO table
                EmpInfo eInfo = new EmpInfo();
             
                eInfo.Emp_Name = this.txtName.Text.Trim();
                eInfo.Emp_Age = int.Parse(this.txtAge.Text.Trim());  
                eInfo.Emp_Qua = this.txtQualification.Text.Trim();

                employeeDomainContext.EmpInfos.Add(eInfo);
                if (employeeDomainContext.HasChanges)
                    employeeDomainContext.SubmitChanges();
                CleanSol();
                lblMsg.Content = "Record inserted Successfully";

            }
          
        }
        public void CleanSol()
        {
            txtName.Text = "";
            txtAge.Text = "";
            txtQualification.Text = "";
        }
    }
}
  
FRONT END CODE FOR REFERENCE:

<StackPanel>
                    <TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="24,13,0,0" Name="textBlock1" Text="Name" VerticalAlignment="Top" />
                    <TextBlock Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="24,8,0,0" Name="textBlock2" Text="Age" VerticalAlignment="Top" />
                    <TextBlock Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="24,8,0,0" Name="textBlock3" Text="Qualification" VerticalAlignment="Top" />
                    <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="22,10,0,0" Name="txtName" VerticalAlignment="Top" Width="120"
                             Text="{Binding Path=Emp_Name, Mode=TwoWay, ValidatesOnExceptions=True}"/>
                    <TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="22,9,0,0" Name="txtAge" VerticalAlignment="Top" Width="120"
                             Text="{Binding Path=Emp_Age, Mode=TwoWay, ValidatesOnExceptions=True}"/>
                    <TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="22,8,0,0" Name="txtQualification" VerticalAlignment="Top" Width="120"
                             Text="{Binding Path=Emp_Qua,Mode=TwoWay, ValidatesOnExceptions=True}" />
                    <Button Content="Save" Grid.Column="1" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="22,8,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
                    <sdk:Label Grid.Column="1" Grid.Row="5" Height="28" HorizontalAlignment="Left" Margin="4,20,0,0" Name="lblMsg" VerticalAlignment="Top" Width="238" Foreground="LimeGreen" />
                    <sdk:ValidationSummary x:Name="FacilityWeeklyScheduleValSummary" Visibility="Collapsed" Grid.Row="6" Grid.ColumnSpan="2"  />
                </Grid>
                </StackPanel>


This is the Simly Inserting the data in into sql database through the coding. For the Update, Delete Operation in Next version of this website i 'll post u.  Send me the mails regarding this and Future posts.
mykumarreddy@gmail.com