Introducing Two-Way Data Binding using Silverlight 2.0, WCF and LINQ to SQL - Developing a Silverlight 2.0 page to demonstrate Add functionality, continued
(Page 3 of 5 )
This is a continuation from the previous section.
<UserControl x:Class="DemoSL.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="0.133*"/>
<RowDefinition Height="0.137*"/>
<RowDefinition Height="0.14*"/>
<RowDefinition Height="0.143*"/>
<RowDefinition Height="0.147*"/>
<RowDefinition Height="0.3*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.35*"/>
<ColumnDefinition Width="0.55*"/>
</Grid.ColumnDefinitions>
<TextBox Margin="8,8,46,8" Grid.Column="1" Text="" TextWrapping="Wrap" x:Name="txtEmpno"/>
<TextBox Margin="8,8,46,8" Text="" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="1" d:LayoutOverrides="Height" x:Name="txtEname"/>
<TextBox Margin="8,8,46,8" Text="" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="2" d:LayoutOverrides="Height" x:Name="txtSal"/>
<TextBox Margin="8,8,46,8" Text="" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="3" d:LayoutOverrides="Height" x:Name="txtDeptno"/>
<TextBlock Margin="17,8,43,8" Text="Empno:" TextWrapping="Wrap" x:Name="lblEmpno"/>
<TextBlock Margin="17,8,43,8" Text="Ename:" TextWrapping="Wrap" Grid.Row="1" x:Name="lblEname"/>
<TextBlock Margin="17,8,43,8" Text="Salary:" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Row="2" d:LayoutOverrides="Height" x:Name="lblSal"/>
<TextBlock Margin="17,8,43,9.5" Text="Deptno:" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Row="3" d:LayoutOverrides="Height" x:Name="lblDeptno"/>
<TextBlock Margin="8,8,64,0" Text="" TextWrapping="Wrap" Height="23.9" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Column="1" Grid.Row="5" d:LayoutOverrides="Height" x:Name="lblMsg"/>
<Button HorizontalAlignment="Stretch" Margin="8,8,90,8" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="4" Content="Add Employee" x:Name="btnAdd"/>
</Grid>
</UserControl>
The above code creates the following screen:

In the code behind, modify the "Page2" class as follows:
Partial Public Class Page2
Inherits UserControl
Public Sub New
InitializeComponent()
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
Me.lblMsg.Text = "Communicating..."
Dim objService As New EmpService.EmpServiceClient
AddHandler objService.AddCompleted, AddressOf AddEmployeeCompleted
objService.AddAsync(New EmpService.Emp With {.Empno = Me.txtEmpno.Text, .Ename = Me.txtEname.Text, .Sal = Me.txtSal.Text, .Deptno = Me.txtDeptno.Text})
End Sub
Private Sub AddEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Me.lblMsg.Text = "Added Successfully!"
End Sub
End Class
Press F5 and you should be able to add new employee information (row) to the database using the Silverlight 2.0 page along with WCF and LINQ to SQL.
If you run into any problems, I dedicated few sections to troubleshooting in my previous article. Please go through all of the troubleshooting steps in my previous article. If you do that, you should have no problems. If you need a complete walk-through, consider reading my previous two articles.
Next: Developing a Silverlight 2.0 page to demonstrate Add functionality: Using two-way binding >>
More Windows Scripting Articles
More By Jagadish Chaterjee