How can we do database operation using SilverLight? Contd.

 Posted by ArticlesMaint on 9/23/2009 | Category: Silverlight Interview questions | Views: 3415

Step 6 :- Define the grid for Customer  Name and Customer Code

Now on the silverlight side we will create a ‘Grid’ which has two columns one for ‘CustomerCode’ and the other for ‘CustomerName’. We have also specified the bindings using ‘Binding path’ in the text block.


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

            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        <TextBlock x:Name="LblCustomerCode" Grid.Column="0" Grid.Row="0" Text="Customer Code"></TextBlock>
        <TextBlock x:Name="TxtCustomerCode" Grid.Column="1" Grid.Row="0" Text="{Binding Path=CustomerCode}"></TextBlock>
        <TextBlock x:Name="LblCustomerName" Grid.Column="0" Grid.Row="1" Text="Customer Name"></TextBlock>
        <TextBlock x:Name="TxtCustomerName" Grid.Column="1" Grid.Row="1" Text="{Binding Path=Customer}"></TextBlock>


Step 7:- Bind the WCF service with the GRID

Now that our grid is created its time to bind the WCF service with the grid. So go to the behind code of the XAML file and create the WCF service object.  There are two important points to be noted when we call WCF service using from SilverLight:-

  • We need to call the WCF asynchronously, so we have called ‘getCustomerAsynch’. Please note this function is created by WCF service to make asynchronous calls to the method / function.

  • Once the function completes its work on the WCF service it sends back the message to silverlight client.  So we need to have some kind of delegate method which can facilitate this communication. You can see we have created a ‘getCustomerCompleted’ method which captures the arguments and ties the results with the grid ‘datacontext’.
public partial class Page : UserControl

        public Page()
            ServiceCustomerClient obj = new ServiceCustomerClient();
            obj.getCustomerCompleted += new EventHandler<getCustomerCompletedEventArgs>(DisplayResults);
        void DisplayResults(object sender, getCustomerCompletedEventArgs e)
            LayoutRoot.DataContext = e.Result;

You can now run the project and see how the SilverLight client consumes and displays the data.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response