[Silverlight 2.0, C#] Creating a database-driven Line Of Business application
Today I am going to step through creating a database-driven multipage application that will allow new users to register a new profile, and then log in thereafter. In addition, the app will feature an admin log in for admin to view registered users with the results displaying in a datagrid. In later posts, we’ll explore styling of the datagrid.
This post will consist of a couple of posts as the explanation is too long for one article. First a disclaimer: I am not a graphics designer, so if you can improve on the UI, please go ahead. I hope you’ll appreciate what I’ve put together.
We’ll include a typical form that collects data from the user and then a summary page that will display the information back to the user for final confirmation. Once the user has been successfully registered, a login screen will show allowing the user to log in. The data entered will be written to a SQL database.
We’ll be using a Silverlight-enabled WCF Service and LINQ to SQL to access the data from the SQL database. We’ll be creating the SQL database from scratch as well.
I am going to assume that you have the required software and tools to follow along with this post. If you’re unsure, here is a quick overview of what you’ll need:
1. Visual Studio 2008, or Visual Web Developer E2008 Express (download it here, if need be: http://www.microsoft.com/express/vwd/).
2. Visual Studio SP1 (download it here, if need be: http://www.microsoft.com/downloads/details.aspx?familyid=FBEE1648-7106-44A7-9649-6D9F6D58056E&displaylang=en).
3. Microsoft® Silverlight™ 2 Tools for Visual Studio 2008 SP1 (download it here, if need be: http://www.microsoft.com/downloads/details.aspx?familyid=C22D6A7B-546F-4407-8EF6-D60C8EE221ED&displaylang=en).
4. Microsoft Expression Blend 2 Trial Version (download it here, if need be: http://www.microsoft.com/downloads/details.aspx?FamilyId=5FF08106-B9F4-43CD-ABAD-4CC9D9C208D7&displaylang=en).
5. Microsoft Expression Blend 2 SP1 (download it here, if need be: http://www.microsoft.com/downloads/details.aspx?FamilyId=EB9B5C48-BA2B-4C39-A1C3-135C60BBBE66&displaylang=en).
6. Microsoft SQL Server 2005 Express (download it here, if need be: http://www.microsoft.com/downloads/details.aspx?familyid=220549b5-0b07-4448-8848-dcc397514b41&displaylang=en).
7. Something extra that will help is Microsoft SQL Server Management Studio Express to aid in creating SQL databases and navigate your tables, import databases, etc. (download it here, if need be: http://www.microsoft.com/downloads/details.aspx?familyid=C243A5AE-4BD1-4E3D-94B8-5A0F62BF7796&displaylang=en).
That’s it. If you need help in installing any of these tools, please send me an comment and I’ll be sure to point you in the right direction. Oh, by the way, please follow the steps for installation. I found out the hard way that failing to do so results in a broken installation of the Silverlight Tools in Visual Studio. I will also assume that you are starting from scratch and have no prior installation of Visual Studio. If you have Visual Studio 2005, please uninstall it, including all associated tools, or you may have problems later.
Right, now that we have handled some admin, let’s get on with the post, shall we?
Start a new Silverlight 2 application and name it MultiPageApp. If you are unsure how to do this, please refer to my post on creating a pop up window, which has details on how to do this: http://2browndogs.com/?p=11 (passing an image to a pop up window).
Here follows a couple screenshots of what we are going to build:
First screenshot shows the main page that initially opens in the browser

This screenshot shows the user registration page

This screenshot shows the user login page

Finally, last screenshot showing the admin login

So as you can see, not a very complicated UI at all. By the way, the company logo will serve a button to return the user to the main page to aid in navigation.
Once your new Silverlight 2 application startup files have been created in Visual Studio, you should have both your client and server files showing. Page.xaml will automatically open for you. You’ll notice that the page opens showing dual view of both XAML and Preview mode. If you would prefer to have Visual Studio automatically open XAML pages in the XAML code rather, go to Tools, Options, Text Editor, scroll down to XAML, then Miscellaneous, and click the box for Default View to always open documents in full XAML view.
Let’s immediately add our extra XAML pages that we’ll need for user login, user registration, admin login and navigation. We’ll call them Login.xaml, Register.xaml, Admin.xaml, and Navigation.xaml respectively. Just to reiterate, to add these pages, right click on the client-side project file, (MultiPageApp, not MultiPageApp.Web) and choose Add -> New Item ->Silverlight User Control.
Visual Studio automatically creates the code behind pages for you. Great! Let’s start with Page.xaml and add our elements. Bear in mind that our 3 buttons are actually part of Navigation.xaml and at runtime we’ll load in this page as a child element to a grid that we’ll place on Page.xaml. It’ll make sense once we go through the steps. The benefit of following this practice is to improve efficiency of your app to avoid unnecessary reloading of page elements.
With Page.xaml open, let’s lay our page out as follows. Remove the height and width properties for the user control. This will enable our page to render in the entire browser window. Our app is going to have a liquid layout and will automatically adjust to the browser window without any extra coding. I like this feature as it eliminates the need for any JavaScript. This is a cross-browser, cross-platform application after all. Next remove the background color of our grid (LayoutRoot). Now add a row and a column:
<Grid.RowDefinitions>
<RowDefinition Height=”*” />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”*” />
</Grid.ColumnDefinitions>
We’ll set the height and width using an ‘*’, which allows the height and width to be proportionate to the browser window and the user control’s elements.
Now we’ll nest a grid within our LayoutRoot grid, assign rows and columns, set our background as a color gradient, and add our company logo:
<Grid Height=”Auto” HorizontalAlignment=”Stretch” Margin=”0,0,0,0″ VerticalAlignment=”Stretch” Width=”Auto”>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”20″/>
<ColumnDefinition Width=”100″/>
<ColumnDefinition Width=”900″/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=”20″/>
<RowDefinition Height=”100″/>
<RowDefinition Height=”700″/>
</Grid.RowDefinitions>
<Grid.Background>
<LinearGradientBrush EndPoint=”0.5,1″ StartPoint=”0.5,0″>
<GradientStop Color=”#FF0A2148″/>
<GradientStop Color=”#FFD3D7DD” Offset=”1″/>
</LinearGradientBrush>
</Grid.Background>
<Image Height=”Auto” Width=”Auto” Source=”Images/MyLogo.png” RenderTransformOrigin=”0.51,0.42″ Margin=”0,0,0,0″ Grid.Row=”1″ Grid.Column=”1″ />
</Grid>
The company logo is included in my source files; I created that so you are welcome to use it. Lastly, we are going to add an extra grid, which will act as a parent grid, into which we’ll load its children that being our navigation, login, etc.
<Grid Margin=”0,0,0,0″ x:Name=”grdMain” HorizontalAlignment=”Center” VerticalAlignment=”Center” />
This grid’s name is grdMain and you’ll see how we refer to it in our code behind page.
Let’s create the elements on the Navigation.xaml page, which will consist of a stackpanel with 3 buttons. You’ll need to remove the grid first. Next add a stackpanel, and then add 3 buttons. Remove the height and width properties for the user control:
<StackPanel Margin=”0,0,0,0″ Orientation=”Horizontal” x:Name=”spNavigation”>
<Button Height=”118.865″ Width=”172.638″ Content=”User Registration” x:Name=”btnRegister” Cursor=”Hand” FontFamily=”./Fonts/Fonts.zip#Arial Rounded MT” FontSize=”14″ Foreground=”#FF1F3B53″ Click=”btnRegister_Click” />
<Button Height=”119″ Width=”173″ Content=”User Login” Cursor=”Hand” x:Name=”btnLogin” FontFamily=”./Fonts/Fonts.zip#Arial Rounded MT” FontSize=”14″ Foreground=”#FF1F3B53″ Margin=”30,0,30,0″ Click=”btnLogin_Click” />
<Button Height=”119″ Width=”173″ Content=”Admin Login” x:Name=”btnAdminLogin” Cursor=”Hand” Foreground=”#FF1F3B53″ FontFamily=”./Fonts/Fonts.zip#Arial Rounded MT” FontSize=”14″ Click=”btnAdminLogin_Click”/>
</StackPanel>
You’ll notice that I’m making use of the Arial Rounded MT font, which I’ve embedded, since Blend does not include this font within your font license library. If you do make use of this font outside production, you may need to ensure that you have the required publishing license for that font, or you may run into some issues later.
I’ve given each button a name and set it’s cursor to a hand, so help the user identify the button as such. In addition, each button has a click event. I let Visual Studio add them for me, by tabbing through the code after typing in “Click”. If you navigate to the code behind, you’ll notice that Visual Studio adds the event handler code for each event.
So if you haven’t already done so, please open Navigation.xaml.cs to edit the event handlers for the 3 buttons.
Add the following code to each event handler:
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
this.Content = new Register();
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
this.Content = new Login();
}
private void btnAdminLogin_Click(object sender, RoutedEventArgs e)
{
this.Content = new Admin();
}
Allow me to explain. What we are doing here is changing out the current child to a new child. In other words, this.Content = new Register(); means take this content’s child, remove it and then add a newly instantiated page, called Register to grdMain, as its new child and then display it to the user. Pretty simple and efficient.
Right, let’s now go to Page.xaml.cs to add a loaded event, to add this page at runtime to our grdMain.
In the constructor, add a loaded event delegate:
Loaded += new RoutedEventHandler(Page_Loaded);
Visual Studio will automatically add “Page_Loaded” after you begin typing “Loaded” and then “+=”. When you get to this point, immediately press the tab key and the Page_Loaded event is added.
Now add the following code to this event, but first remove the line of code that is contained:
public void Page_Loaded(object sender, RoutedEventArgs e)
{
grdMain.Children.Clear();
Navigation navigation = new Navigation();
grdMain.Children.Add(navigation);
}
This event will accomplish 3 things:
1. Clear any children that grdMain may have loaded.
2. Instantiate a new user control, that being Navigation.
3. Next, add this user control to our grid, grdMain as a child.
You might recall when we lay Page.xaml out, we added an image for our company logo. Let’s now add some extra properties to that image element. Add:
MouseLeftButtonDown=”Page_Go” Cursor=”Hand” ToolTipService.ToolTip=”Take me back to the home page”
to that image element. Make sure you image look like this:
<Image Height=”Auto” Width=”Auto” Source=”Images/MyLogo.png” RenderTransformOrigin=”0.51,0.42″ Margin=”0,0,0,0″ Grid.Row=”1″ Grid.Column=”1″ MouseLeftButtonDown=”Page_Go” Cursor=”Hand” ToolTipService.ToolTip=”Take me back to the home page”/>
I’ve added a cursor and a ToolTipService property as well. The ToolTipService is a handy way of providing a special message when the user’s cursor hovers over the image.
Build the app, run it and you should now see Page.xaml open with Navigation.xaml dynamically loaded in as a child in grdMain. If so, then you’re on the right track! Your screen should look like the screenshot I have at the top of this page.
So far so good, now we’ll add the user control elements for each of the pages that the buttons will open. Open Login.xaml and let’s add a border to our grid. Remember to remove the width and height properties for this user control. This border will wrap around any elements we nest within our grid:
<Border Margin=”0,0,0,0″ CornerRadius=”10,10,10,10″ Background=”#FFB9D0E8″ BorderBrush=”#FF113150″ BorderThickness=”2,2,2,2″>
</Border>
Our border has a radius, a background color, a border stroke, and a stroke thickness. Next we nest a stackpanel within our border element:
<StackPanel Height=”Auto” Width=”Auto” HorizontalAlignment=”Center” VerticalAlignment=”Center” x:Name=”spWrapper” Margin=”20,20,20,20″>
</StackPanel>
Let’s add a textblock for our page title:
<TextBlock Height=”Auto” HorizontalAlignment=”Center” VerticalAlignment=”Center” Width=”Auto” FontFamily=”./Fonts/Fonts.zip#Arial Rounded MT” FontSize=”14″ FontWeight=”Bold” Foreground=”#FF113150″ Text=”User Login” TextWrapping=”NoWrap” TextAlignment=”Center” Margin=”0,0,0,0″ x:Name=”txtTitle”/>
Next, we’ll add 3 stackpanels; the first for the username label and textbox, the second for the password label and textbox, and the last for the 2 buttons, for login and for forgot password:
First Stackpanel
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spUsername” Margin=”0,10,0,5″>
</StackPanel>
Second Stackpanel
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spPassword” Margin=”0,5,0,5″>
</StackPanel>
Third Stackpanel
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Center” VerticalAlignment=”Center” x:Name=”spButtons” Margin=”0,20,0,0″>
</StackPanel>
Right before we go on, we need to add a reference to the new Silverlight 2 Toolkit March 2009 Release, so that we can utilize the new password box. If you need to download the binary, you’ll find it here: http://silverlight.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=20430. Make sure you download the Silverlight 2 version. Once installed, go to your client side and right click on References, add Reference. An Add Reference dialog window opens. On the .Net tab, scroll down to System.Tools.Controls. Click OK. Great, now let’s add the binary to our page. At the top of Login.xaml, in the user control element, add the following property:
xmlns:t=”clr-namespace:System.Windows.Controls;assembly=System.Windows”
Incidently, I added the “:t” just after the “xmlns”. By doing so, you are now able to reference this binary within your xaml, by typing <t: …>. Visual Studio automatically adds all the controls of that binary to intellisense, and all you have to do is make your choice. Now we can access the password control.
Next, we need to add a reference to System.Windows.Controls.Toolkit, to access the Label control:
xmlns:u=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit”
So, let’s go ahead and first add our label and textbox for username to our first stackpanel, between the opening and closing tag:
<u:Label Height=”Auto” Width=”120″ Content=”Username:” Margin=”0,0,0,0″ x:Name=”lblUsername” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtUsername” Width=”150″ />
As I mentioned before, open a new tag, start typing ‘u’, and intellisense takes over. Scroll down the list until you find Label.
Next, we add the label and password box for the second stackpanel:
<u:Label Height=”Auto” Width=”120″ Content=”Password:” Margin=”0,0,0,0″ x:Name=”lblPassword” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<t:PasswordBox x:Name=”txtPassword” Width=”150″ />
Lastly, we create the 2 buttons to be placed in the third stackpanel:
<Button Height=”32″ Width=”102″ Content=”Login” Margin=”0,0,5,0″/>
<Button Height=”32″ Width=”102″ Content=”Forgot password” Margin=”5,0,0,0″/>
Let’s build our app and run it. If you click on the user login button and the navigation stackpanel will be replaced by the login page you just completed. Excellent! 3 more pages to go.
Let’s now add the elements to our admin login page. We’ll come back to the register page later. Admin.xaml is almost identical to Login.xaml. Here is the completed code, with no explanation required:
UserControl x:Class=”MultiPageApp.Admin”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:t=”clr-namespace:System.Windows.Controls;assembly=System.Windows”
xmlns:u=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit”>
<Grid x:Name=”LayoutRoot”>
<Border Margin=”0,0,0,0″ CornerRadius=”10,10,10,10″ Background=”#FFB9D0E8″ BorderBrush=”#FF113150″ BorderThickness=”2,2,2,2″>
<StackPanel Height=”Auto” Width=”Auto” HorizontalAlignment=”Center” VerticalAlignment=”Center” x:Name=”spWrapper” Margin=”20,20,20,20″>
<TextBlock Height=”Auto” HorizontalAlignment=”Center” VerticalAlignment=”Center” Width=”Auto” FontFamily=”./Fonts/Fonts.zip#Arial Rounded MT” FontSize=”14″ FontWeight=”Bold” Foreground=”#FF113150″ Text=”Admin Login” TextWrapping=”NoWrap” TextAlignment=”Center” Margin=”0,0,0,0″ x:Name=”txtTitle”/>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spUsername” Margin=”0,10,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Username:” Margin=”0,0,0,0″ x:Name=”lblUsername” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtUsername” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spPassword” Margin=”0,5,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Password:” Margin=”0,0,0,0″ x:Name=”lblPassword” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<t:PasswordBox x:Name=”txtPassword” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Center” VerticalAlignment=”Center” x:Name=”spButtons” Margin=”0,20,0,0″>
<Button Height=”32″ Width=”102″ Content=”Login” Margin=”0,0,5,0″/>
<Button Height=”32″ Width=”102″ Content=”Forgot password” Margin=”5,0,0,0″/>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</UserControl>
So be sure to build and test your app. If successful, when you click on the admin login button, the navigation stackpanel should be replaced with the UI you just completed.
Now lets create the elements for the registration page. This page is similar to the login page, but with some extra controls for the extra information we’ll need to write to the database to register the user.
So I won’t go into too much detail here. Once again, the completed source code:
<UserControl x:Class=”MultiPageApp.Register”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:u=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit”>
<Grid x:Name=”LayoutRoot”>
<Border Margin=”0,0,0,0″ CornerRadius=”10,10,10,10″ Background=”#FFB9D0E8″ BorderBrush=”#FF113150″ BorderThickness=”2,2,2,2″>
<StackPanel Height=”Auto” Width=”Auto” HorizontalAlignment=”Center” VerticalAlignment=”Center” x:Name=”spWrapper” Margin=”20,20,20,20″>
<TextBlock Height=”Auto” HorizontalAlignment=”Center” VerticalAlignment=”Center” Width=”Auto” FontFamily=”./Fonts/Fonts.zip#Arial Rounded MT” FontSize=”14″ FontWeight=”Bold” Foreground=”#FF113150″ Text=”User Login” TextWrapping=”NoWrap” TextAlignment=”Center” Margin=”0,0,0,0″ x:Name=”txtTitle”/>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spUsername” Margin=”0,10,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Username:” Margin=”0,0,0,0″ x:Name=”lblUsername” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtUsername” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spPassword” Margin=”0,5,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Passsword:” Margin=”0,0,0,0″ x:Name=”lblPassword” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtPassword” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spFName” Margin=”0,5,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Full Name:” Margin=”0,0,0,0″ x:Name=”lblFName” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtFName” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spLName” Margin=”0,5,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Last Name:” Margin=”0,0,0,0″ x:Name=”lblLName” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtLName” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” x:Name=”spEmail” Margin=”0,5,0,5″>
<u:Label Height=”Auto” Width=”120″ Content=”Email:” Margin=”0,0,0,0″ x:Name=”lblEmail” FontFamily=”Arial” FontSize=”12″ Foreground=”#FF000000″ />
<TextBox x:Name=”txtEmail” Width=”150″ />
</StackPanel>
<StackPanel Height=”Auto” Width=”Auto” Orientation=”Horizontal” HorizontalAlignment=”Center” VerticalAlignment=”Center” x:Name=”spButtons” Margin=”0,20,0,0″>
<Button Height=”32″ Width=”102″ Content=”Register” Margin=”0,0,0,0″ Cursor=”Hand” x:Name=”btnRegister”/>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</UserControl>
Now once again, build your app and you should be good to go. Click on the user register button and you should see the register UI.
Well that’s it for now. I hope you’ve enjoyed this post. Please be sure to send any comments or suggestions you may have.
Please return soon as I’ll be posting the second part of this post.
You may download the source code HERE.















