Database operations using Silverlight 2.0 WCF and LINQ to SQL - Developing a Silverlight 2.0 page to demonstrate CRUD functionality: code behind
(Page 4 of 5 )
This is a continuation from the previous section. In the previous section, we developed the Siverlight 2.0 UI for CRUD operations against a WCF service. Now, we have to develop the code behind to access the WCF Service. Make sure that you have added a reference to the WCF service in your Silverlight 2.0 project.
The following is the code behind for the UI markup provided in the previous section. The explanation is provided in the next section.
Partial Public Class Page4
Inherits UserControl
Public Sub New()
InitializeComponent()
InitiateContext()
InitiateHandlers()
End Sub
#Region "Declarations"
Private objEmp As EmpService.Emp
Dim objService As EmpService.EmpServiceClient
#End Region
#Region "Helpers"
Private Sub InitiateContext()
objService = New EmpService.EmpServiceClient
objEmp = New EmpService.Emp
DoBinding()
End Sub
Private Sub DoBinding()
Me.LayoutRoot.DataContext = objEmp
End Sub
Private Sub InitiateHandlers()
AddHandler objService.AddCompleted, AddressOf AddEmployeeCompleted
AddHandler objService.UpdateCompleted, AddressOf UpdateEmployeeCompleted
AddHandler objService.DeleteCompleted, AddressOf DeleteEmployeeCompleted
AddHandler objService.GetEmployeeCompleted, AddressOf EmployeeFetched
End Sub
Private Sub ShowMessage(ByVal Msg As String)
Me.lblMsg.Text = Msg
End Sub
#End Region
#Region "Handlers"
Private Sub AddEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
ShowMessage("Added Successfully!")
End Sub
Private Sub UpdateEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
ShowMessage("Updated Successfully!")
End Sub
Private Sub DeleteEmployeeCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
ShowMessage("Deleted Successfully!")
btnClear_Click(Nothing, Nothing)
End Sub
Private Sub EmployeeFetched(ByVal sender As Object, ByVal e As EmpService.GetEmployeeCompletedEventArgs)
If e.Result Is Nothing Then
ShowMessage("Employee not found!")
Else
objEmp = e.Result
DoBinding()
ShowMessage("Fetched!")
End If
End Sub
#End Region
#Region "Button Events"
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClear.Click
objEmp = New EmpService.Emp
DoBinding()
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
ShowMessage("Communicating...")
objService.AddAsync(objEmp)
End Sub
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
ShowMessage("Communicating...")
objService.DeleteAsync(objEmp.Empno)
End Sub
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnUpdate.Click
ShowMessage("Communicating...")
objService.UpdateAsync(objEmp)
End Sub
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
ShowMessage("Communicating...")
objService.GetEmployeeAsync(objEmp.Empno)
End Sub
#End Region
End Class
Press F5 and you should be able to do all CRUD operations against the “Emp” table in database using Silverlight 2.0 page together with WCF and LINQ to SQL.
If you run into any problems, please refer tp my dedicated sections (especially on troubleshooting) in my previous articles. Please go through all of the troubleshooting steps in my previous articles; do so, and you should have no problems.
Next: Developing a Silverlight 2.0 page to demonstrate CRUD functionality: Explanation >>
More Windows Scripting Articles
More By Jagadish Chaterjee