Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 14: Visual Basic (VB) and WPF Support

A few folks commented that they’d like to see something in my series updating my Mix09 talk “building business applications with Silverlight 3” in Visual Basic.  VB is *super* important in the business application space, so I have no problem accommodating that request.   By while I was at it I thought i’d also show a WPF client for RIA Services via the very cool ADO.NET Data Services support we have.  This is much like the WinForms support I showed earlier. 

The demo requires (all 100% free and always free):

  1. VS2008 SP1 (Which includes Sql Express 2008)

  2. Silverlight 3 RTM

  3. .NET RIA Services July '09 Preview

Also, download the full demo files and, of course, check out the running application.

You can see the full series here

In Part 1: Navigation Basics there is no code whatsoever, so the VB version looks just like what I posted.

In Part 2: Rich Data Query there is some more interesting code snippets.   First, let’s look at the DomainService

   1: <EnableClientAccess()> _
   2: Public Class SuperEmployeeDomainService
   3:     Inherits LinqToEntitiesDomainService(Of NORTHWNDEntities)
   5:     Public Function GetSuperEmployees() As IQueryable(Of SuperEmployee)
   6:         Dim q = From emp In Me.Context.SuperEmployeeSet _
   7:                 Where emp.Issues > 100 _
   8:                 Order By emp.EmployeeID
   9:         Return q
  10:     End Function
  13:     Public Sub UpdateSuperEmployee(ByVal currentSuperEmployee As SuperEmployee)
  14:         Me.Context.AttachAsModified(currentSuperEmployee, Me.ChangeSet.GetOriginal(currentSuperEmployee))
  15:     End Sub
  16:     Public Sub InsertSuperEmployee(ByVal superEmployee As SuperEmployee)
  17:         Me.Context.AddToSuperEmployeeSet(superEmployee)
  18:     End Sub

Here we are defining the Query, Update and Insert methods.

Then to show off some of our POCO support, I created a method to return the origin counts

Public Function GetOrigins() As IQueryable(Of Origin)
     Dim q = (From emp In Context.SuperEmployeeSet _
              Select emp.Origin).Distinct().Select(Function(name) New Origin With {.Name = name, .Count = Context.SuperEmployeeSet.Count(Function(emp) emp.Origin.Trim() = name.Trim())})
     q = q.Where(Function(emp) emp.Name IsNot Nothing)
     Return q
 End Function

and defined a POCO class to return that data to the client to show in my AutoComplete box

Public Class Origin
    Public Sub New()
    End Sub
    Private _Name As String
    <Key()> _
    Public Property Name() As String
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    Private _Count As Integer
    Public Property Count() As Integer
            Return _Count
        End Get
        Set(ByVal value As Integer)
            _Count = value
        End Set
    End Property
End Class


On the client side in the Silverlight project I end up doing most things through binding in Xaml, so those are exactly the same in VB.  But I did create a AddNewEmployee ChildWindow

Partial Public Class AddNewWindow
    Inherits ChildWindow
    Private _NewEmployee As SuperEmployee
    Public Property NewEmployee() As SuperEmployee
            Return _NewEmployee
        End Get
        Set(ByVal value As SuperEmployee)
            _NewEmployee = value
        End Set
    End Property
    Public Sub New()
        NewEmployee = New SuperEmployee()
        NewEmployee.LastEdit = DateTime.Now.[Date]
        Me.newEmployeeForm.CurrentItem = NewEmployee
    End Sub
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.DialogResult = True
    End Sub
    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.DialogResult = False
    End Sub
End Class

And then we just raise this event

Private Sub AddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim w = New AddNewWindow()
    AddHandler w.Closed, AddressOf addNewWindow_Closed
End Sub
Private Sub addNewWindow_Closed(ByVal sender As Object, ByVal e As EventArgs)
    Dim win = TryCast(sender, AddNewWindow)
    Dim context = TryCast(dds.DomainContext, SuperEmployeeDomainContext)
    If win.DialogResult = True Then
    End If
End Sub


Part 3: Authentication – No code whatsoever here…   This is exactly the same between VB and C#.

Part 4: SEO, Export to Excel and Out of Browser

For SEO, we enabled deeplinking by handing a couple of events…

'Executes when the user navigates to this page.
Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
    Dim qs = NavigationContext.QueryString
    If qs.ContainsKey("EmpId") Then
        dds.FilterDescriptors.Add(New FilterDescriptor("EmployeeID", FilterOperator.IsEqualTo, qs("EmpId")))
    End If
End Sub



Private Sub dataGrid1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    Dim emp = TryCast(dataGrid1.SelectedItem, SuperEmployee)
    If emp IsNot Nothing Then
        PermalinkTextBox.Text = (Application.Current.Host.Source.ToString().Replace("ClientBin/MyApp.xap", "") & "#/Home?EmpId=") & emp.EmployeeID
    End If
End Sub


The server side code for Sitemap.aspx and the alternate content in default.aspx is identical.. expect for a small bit of URL rewritting code in default.aspx to ensure both the server and client have access to the deep link.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim empId As String = Request.QueryString("EmpId")
    Dim deepLink = "/Home?EmpId=" & empId
    If empId IsNot Nothing Then
        Response.Write("<script type=text/javascript>window.location.hash='#" & deepLink & "';</script>")
    End If
End Sub

The bit about exporting to Excel is super easy as well..

Private Sub ExportToExcel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim context = TryCast(dds.DomainContext, SuperEmployeeDomainContext)
    Dim s = Application.GetResourceStream(New Uri("excelTemplate.txt", UriKind.Relative))
    Dim dialog = New SaveFileDialog()
    dialog.DefaultExt = "*.xml"
    dialog.Filter = "Excel Xml (*.xml)|*.xml|All files (*.*)|*.*"
    If dialog.ShowDialog() = False Then
        Exit Sub
    End If
    Using sw = New StreamWriter(dialog.OpenFile())
        Dim sr = New StreamReader(s.Stream)
        While Not sr.EndOfStream
            Dim line = sr.ReadLine()
            If line = "***" Then
                Exit While
            End If
        End While
        For Each emp In context.SuperEmployees
            sw.WriteLine("<Cell><Data ss:Type=""String"">{0}</Data></Cell>", emp.Name)
            sw.WriteLine("<Cell><Data ss:Type=""String"">{0}</Data></Cell>", emp.Origin)
            sw.WriteLine("<Cell><Data ss:Type=""String"">{0}</Data></Cell>", emp.Publishers)
            sw.WriteLine("<Cell><Data ss:Type=""Number"">{0}</Data></Cell>", emp.Issues)
        While Not sr.EndOfStream
        End While
    End Using
End Sub

After a bit of formatting… we get:

















Finally, in  Part 5: Astoria, Add Service Reference and WinForms…  Defining the ADO.NET Data Services in the web project looks like:

Public Class SuperEmployeeWebDataService
    Inherits DataService(Of [SuperEmployeeDomainService])
    Implements IServiceProvider
    ' This method is called only once to initialize service-wide policies.
    Public Shared Sub InitializeService(ByVal config As IDataServiceConfiguration)
        ' TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        ' Examples:
        config.SetEntitySetAccessRule("*", EntitySetRights.All)
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All)
    End Sub


Then for some variety,  I created a WPF client for this service.  I also made use of the DataGrid control from the very cool WPF Control Toollit

first I load the data…

Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles LoadButton.Click
    Context = New SuperEmployeeDomainService( _
        New Uri("http://localhost:4558/SuperEmployeeWebDataService.svc/"))
    Context.MergeOption = MergeOption.AppendOnly
    Dim q = From emp In Context.SuperEmployee _
    Where emp.Issues > 10 _
    Order By emp.Name _
    Select emp
    Dim savedCursor = Cursor
    Cursor = Cursors.Wait
    Me.DataGrid1.ItemsSource = q.ToList()
    Cursor = savedCursor
End Sub

Then update whenever there is a change.

Private Sub DataGrid1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedItem As SuperEmployee = Me.DataGrid1.CurrentItem
    If selectedItem Is Nothing Then
    End If
    Dim q = From emp In Context.SuperEmployee _
                Where emp.EmployeeID = selectedItem.EmployeeID _
                Select emp
    Dim employee = q.FirstOrDefault()
    employee.Gender = selectedItem.Gender
    employee.Issues = selectedItem.Issues
    employee.LastEdit = selectedItem.LastEdit
    employee.Name = selectedItem.Name
    employee.Origin = selectedItem.Origin
    employee.Publishers = selectedItem.Publishers
    employee.Sites = selectedItem.Sites
    Dim savedCursor = Cursor
    Cursor = Cursors.Wait
    Cursor = savedCursor
End Sub


Comments (10)

  1. Very Great Article. I like it. I will try this style in my next project. Keep it up.

  2. mvcguy says:

    Excellent Series Of Articles

    Please How to integrate Silverlight Controls With ASP.Net MVC ?

  3. bell says:

    after building the solution I still have ten errors.

    Error 2 The tag ‘DataGrid’ does not exist in XML namespace ‘‘. Line 6 Position 10. D:vs9projectsabrams.MyApp.VBWpfApplication1Window1.xaml 6 10 WpfApplication1

    Error 3 Name ‘InitializeComponent’ is not declared. D:vs9projectsabrams.MyApp.VBMyAppApp.xaml.vb 7 9 MyApp

    Error 5 ‘loginContainer’ is not a member of ‘MyApp.MainPage’. D:vs9projectsabrams.MyApp.VBMyAppMainPage.xaml.vb 8 9 MyApp

    What does all of this mean?

    Trying to run the app fails of course but not for the reasons above; still the main issue is that you’ve not provided a workaround (as far as I’ve seen) for the sql server 2008 express db. Can you tell us how to replace that ref with one to nw installed on a standard sql 1008 instance?

  4. BradA says:

    Bell — i am not sure.. do you have the WPF Toolkit installed?

  5. bell says:

    The WPF Toolkit fixed the project errors, thanks.

    Any quick way to workaround the db issue? I’m not eager to install sql 2008 express just for this app.

  6. BradA says:

    Sure — you could use the in memory data scenario I describe here..

    I didn’t port it to VB, but it shouldn’t be too hard to pull over..

  7. bell says:

    Isn’t is strange that while the sln will build, when the app is run, and after the db error, there are 52 errors? They go away if I rebuild, at least till the app tries to run again…but isn’t that kind of weird?

  8. There’s a small bug when creating a new Silverlight Business Application if you have Option Strict set to On by default. Line 38 of LoginControl.xaml.vb uses IIf which returns an Object. It should use If instead which will avoid the implicit conversion.

  9. BradA says:

    William Bartholomew  – thanks!  We will get that fixed.

Skip to main content