I’m Back

Posted: April 18, 2012 in Uncategorized

Dear all,

I ‘m back to my blog and will be sharing exciting updates related to Application Development, Database Administration, Data Analysis and DevInfo soon………..

 

 

 

Chandan’s Profile

Posted: August 16, 2010 in Uncategorized

http://www.linkedin.com/in/chandandeo
Chandan Deo

Create Your Badge

Application Deployment (C#)

Posted: July 20, 2010 in Uncategorized

In this tutorial, you will learn about two different options for deploying SQL Server 2005 Compact Edition with your applications: SQL Server 2005 Compact Edition central installation and SQL Server 2005 Compact Edition private application installation. At the completion of this tutorial, you will know how to perform both central installations and private installations of SQL Server 2005 Compact Edition and be able to identify the advantages and disadvantages of each SQL Server 2005 Compact Edition installation technique.

In this tutorial, you will perform the following exercises:

  • Application deployment using central (or shared) deployment of SQL Server 2005 Compact Edition
  • Application deployment using private (or local) deployment of SQL Server 2005 Compact Edition

Exercise 1: Performing Application Deployment Using Central (or Shared) Deployment of SQL Server 2005 Compact Edition

In this exercise, you will learn how to perform application deployment using central (or shared) deployment of SQL Server 2005 Compact Edition. When using a central install of SQL Server 2005 Compact Edition, all applications that use SQL Server 2005 Compact Edition will use the same SQL Server 2005 Compact Edition files.

The main benefits of centrally installing SQL Server 2005 Compact Edition are that all applications on the computer use a common SQL Server 2005 Compact Edition installation and Microsoft Update automatically updates and services the SQL Server 2005 Compact Edition installation. The main concern when centrally installing SQL Server 2005 Compact Edition is that the user performing the application installation must have administrative rights on the computer to successfully complete the install.

To open the project

Note The project that you use in this tutorial is a slightly modified version of the Visual Studio project created in the tutorial entitled “Updatable Cursor Programming with ResultSet (C#)”. The only modifications to the project are the addition of two text boxes located at the bottom of the application form: one displays the location of the application directory and the other displays the location of the data directory.

  1. Start Visual Studio 2005 by clicking StartAll ProgramsMicrosoft Visual Studio 2005Microsoft Visual Studio 2005.
  2. Click FileOpenProject/Solution.
  3. Navigate within the Open Project dialog box to the folder in which you installed this tutorial’s files, and then to the Exercises\SSCEDeploymentCSharpsubdirectory, select the SSCEDeploymentCSharp.sln file, and click Open.Visual Studio opens the solution.

To configure the application for deployment with SQL Server Compact Edition centrally installed

Note As listed in the Tutorial Requirements near the beginning of this manual, you must have Visual Studio 2005 Service Pack 1 installed to successfully complete this tutorial.

  1. If the Solution Explorer pane is not already visible, open it by clicking ViewSolution Explorer in Visual Studio.
  2. In the Solution Explorer, expand the SSCEDeploymentCSharp project.
  3. Double-click Properties, which is displayed under SSCEDeploymentCSharp, as shown in Figure 1.

    Figure 1. Properties listed in the Solution Explorer

    Visual Studio displays the Project Designer, which you use to configure the available project settings. The configuration setting values are grouped into panes, with each pane accessible by way of the tabs listed on the left edge of the Project Designer.

  4. Click on the Publish tab to display the publication settings pane, as shown in Figure 2.

    Click here for larger image

    Figure 2. The publication settings pane (Click on the picture for a larger image)

  5. Click Prerequisites… listed under the Install Mode and Settings section on the Publish pane.The Prerequisites dialog box is displayed, as shown in Figure 3.

    Figure 3. The Prerequisites dialog box

  6. Both SQL Server 2005 Compact Edition and .NET Framework 2.0 should already be checked. If either of these two prerequisites is not already checked, check it now.

    Note If SQL Server 2005 Compact Edition was not already checked when you opened the Prerequisites dialog box, your computer probably doesn’t have Visual Studio 2005 Service Pack 1 installed. With Visual Studio 2005 Service Pack 1 installed, Visual Studio automatically makes SQL Server 2005 Compact Edition a prerequisite to the project when a SQL Server Compact Edition database file is added to the project. In the case of this project, Visual Studio made SQL Server 2005 Compact Edition a project prerequisite when the Northwind.sdf file was added to the project. If you do not have Visual Studio 2005 Service Pack 1 installed, you might encounter difficulty in completing this tutorial.

  7. Select the radio button for Download prerequisites from the same location as my application.

    Note Choosing Download prerequisites from the same location as my application places the setup files for SQL Server Compact Edition on your Web site in addition to the setup files for your application. At the time that this tutorial is being written you must choose to download the SQL Server Compact Edition files from the same location as your application because SQL Server Compact Edition is not yet a released product; therefore, the SQL Server Compact Edition setup files referenced by ClickOnce are not yet available from the vendor’s (Microsoft) Web site. If you are doing this tutorial after the public release of SQL Server Compact Edition, you might want to try selectingDownload prerequisites from the component vendor’s Web site. Configuring the ClickOnce package to download SQL Server Compact Edition from the vendor’s Web site reduces the download burden on your Web site and ensures that the user gets the most up-to-date version of SQL Server Compact Edition.

  8. Click OK to close the Prerequisites dialog box.
  9. Click FileClose on the Visual Studio menu to close the Project Designer.

Understanding the DataDirectory substitution string

  1. In the Solution Explorer, right-click on the Form1.cs file and click View Code.Visual Studio displays a window containing the code associated with the form.
  2. Within the Form1 constructor, notice that the connection string that is passed to the SqlCeConnection constructor includes the |DataDirectory|substitution string in the data source path as shown in the following line of code.
    _conn = new SqlCeConnection(@"Data Source = |DataDirectory|\Northwind.sdf");

    At run time, the DataDirectory substitution string is replaced with the appropriate directory path for the application’s current execution environment. Without the DataDirectory substitution string, your application would have to programmatically determine the location of the database file based on the current execution environment and then dynamically build the path used in the connection string.

  3. Close the Form1.cs window.

To publish your application for ClickOnce deployment

  1. In the Solution Explorer pane, right-click the SSCEDeploymentCSharp project and select Publish, as shown in Figure 4.

    Figure 4. Selecting Publish on the SSCEDeploymentCSharp context menu

    The Publish Wizard appears.

  2. In response to the question on where to publish the application, leave the default location of http://localhost/SSCEDeploymentCSharp/, and clickFinish, as shown in Figure 5.

    Figure 5. Specifying the location to publish the application

    After the wizard builds and publishes the application and verifies the publication, the wizard opens Internet Explorer with the Web page that it has published, as shown in Figure 6. It might take a few moments before the Web page is displayed.

    Figure 6. The generated publication to be used to install the SSCEDeploymentCSharp application

    Notice the two links on the generated page. The Launch link launches the application without performing any prerequisites checks. The Install link installs any of the prerequisites that are not already installed and then installs and launches the application. You are now ready to test installing your application.

To prepare your computer so that the ClickOnce install experience is consistent with the end-user’s experience

For the purposes of this tutorial, if you already have SQL Server 2005 Compact Edition installed on your computer, you will uninstall it. With SQL Server Compact Edition removed from your computer, you can test the ClickOnce installation behavior as an end user who does not have SQL Server 2005 Compact Edition already installed experiences the ClickOnce installation.

  1. Close Visual Studio by choosing FileExit. If you are prompted to save files, click Yes.This is necessary because Visual Studio is using a reference to SQL Server 2005 Compact Edition and will therefore interfere with the SQL Server Compact Edition uninstall.
  2. Open the Windows Control Panel by selecting Control Panel from the Windows Start menu
  3. Choose Add or Remove Programs from the Windows Control Panel.
  4. If Microsoft SQL Server 2005 Compact Edition is listed in the Currently installed programs list, select it and then click Remove. Otherwise, proceed to the next task.
  5. Click Yes on the Add or Remove Programs confirmation dialog box.You have successfully removed SQL Server Compact Edition from your computer.

To install and run your application with ClickOnce

  1. On the publication Web page, click Install. Your computer then downloads the ClickOnce setup program that Visual Studio generated when you published your application. The setup program checks that your computer has all of the necessary prerequisites installed and installs any prerequisites that are missing.
  2. In response to the File Download – Security Warning dialog box, click Run, as shown in Figure 7.

    Figure 7. The File Download security warning

  3. In response to the security warning that the publisher cannot be verified, click Run, as shown in Figure 8.

    Figure 8. The Setup security warning

    The setup program executes and as part of the prerequisites check determines that SQL Server 2005 Compact Edition is not installed. The setup program then displays an installation dialog box, as shown in Figure 9. The setup program automatically combines the End User License Agreement (EULA) for each of the required prerequisites and displays the combined EULA on the setup dialog box. In this exercise, the setup dialog box displays only the SQL Server Compact Edition EULA because SQL Server Compact Edition is the only prerequisite that the setup program is installing.

    Figure 9. SSCEDeploymentCSharp Setup, indicating that the SQL Server 2005 Compact Edition prerequisite is about to be installed

  4. Click Accept on the SSCEDeploymentCSharp Setup dialog box.The SSCEDeploymentCSharp Setup installs SQL Server 2005 Compact Edition.
  5. In response to the security warning that the publisher cannot be verified, click Install, as shown in Figure 10.

    Figure 10. The Application Install security warning

    After installation, the application appears, as shown in Figure 11.

    Figure 11. The deployed application

    The application is now fully installed on your local computer. In addition to installing your application, the ClickOnce installation process has centrally installed SQL Server Compact Edition; therefore, SQL Server Compact Edition is available for use by all applications on your computer.

    All aspects of your program are now locally installed on your computer including the SQL Server Compact Edition database file. All changes that you make to the data in the application are stored to the local database. Notice that your program is now available under StartAll ProgramsMicrosoft |SSCEDeploymentCSharp. You can exit the application then launch it from the Start menu and see that any changes that you make to the application data are maintained between program executions. You can also use Add or Remove Programs in the Windows Control Panel to uninstall the application.

    The Application Directory and Data Directory values at the bottom of the application form show the current locations of the application directory and data directory. You’ll recall that when you ran the program directly from the executable file created by Visual Studio that the SQL Server Compact Edition database file was located in the same directory as the application. When you install the application by way of ClickOnce, the application directory and data directory are now in different locations. This change in the location of the SQL Server Compact Edition database file relative to the application directory is why it is crucial to use the DataDirectory substitution string when specifying the path to the data source instead of using a fixed path.

  6. Close the application.

Exercise 2: Performing Application Deployment Using Private (or Local) Deployment of SQL Server 2005 Compact Edition

In this exercise, you will learn how to perform application deployment using private (or local) deployment of SQL Server 2005 Compact Edition. When using a private install, the SQL Server 2005 Compact Edition files are deployed with the application and placed in the application directory. A private SQL Server 2005 Compact Edition installation makes SQL Server 2005 Compact Edition only available to the program with which it is installed; no other programs are affected by the installation.

The main benefits of privately installing SQL Server 2005 Compact Edition are that the user performing the application installation can do so without having administrative rights and when privately installing SQL Server 2005 Compact Edition, the application author has control over the updates applied to the SQL Server 2005 Compact Edition files used by the application. The main concern when privately installing SQL Server 2005 Compact Edition is that the application author assumes the responsibility of updating and servicing the SQL Server 2005 Compact Edition files; the privately installed SQL Server 2005 Compact Edition files are not updated or serviced by Microsoft Update.

To open the project

  1. Start Visual Studio 2005 by clicking StartAll ProgramsMicrosoft Visual Studio 2005Microsoft Visual Studio 2005.
  2. Click FileOpenProject/Solution.
  3. Navigate within the Open Project dialog box to the folder in which you installed this tutorial’s files, and then to the Exercises\SSCEDeploymentCSharpsubdirectory, select the SSCEDeploymentCSharp.sln file, and click Open.Visual Studio opens the project you used in Exercise 1.

To configure the application for ClickOnce deployment with SQL Server Compact Edition privately installed

  1. If the Solution Explorer pane is not already visible, open it by clicking ViewSolution Explorer in Visual Studio.
  2. In the Solution Explorer, expand the SSCEDeploymentCSharp project.
  3. Double-click Properties, which is displayed under SSCEDeploymentCSharp, as shown in Figure 12.

    Figure 12. Properties listed in the Solution Explorer

    Visual Studio displays the project designer tabs to allow you to configure project settings.

  4. Click on the Publish tab to display the publication settings pane, as shown in Figure 13.

    Click here for larger image

    Figure 13. The publication settings pane (Click on the picture for a larger image)

  5. Click Prerequisites…, listed under the Install Mode and Settings section on the Publish pane.The Prerequisites dialog box is displayed.
  6. Uncheck the box next to SQL Server 2005 Compact Edition, as shown in Figure 14.You remove SQL Server Compact Edition as a project prerequisite because you are copying the SQL Server Compact Edition files as part of your application. With SQL Server Compact Edition marked as a prerequisite, the ClickOnce setup utility will perform a central install of SQL Server Compact Edition on the target machine as it did in Exercise 1 of this tutorial.

    Figure 14. The Prerequisites dialog box

  7. Click OK to dismiss the Prerequisites dialog box.
  8. Close the project properties window.

To add the SQL Server 2005 Compact Edition files to the project

To privately install the SQL Server Compact Edition with your application, you must include the SQL Server Compact Edition DLL files with your application and copy the DLL files to the application directory.

  1. In the Solution Explorer, right-click on the SSCEDeploymentCSharp project.
  2. On the context menu that appears, click AddExisting Item….
  3. In the Add Existing Item dialog box, change the Files of type drop-down box to the value All Files (*.*).
  4. Still in the Add Existing Item dialog box, navigate to the location where the SQL Server Compact Edition DLL files are installed, which by default isC:\Program Files\Microsoft SQL Server Compact Edition\v3.1.
  5. Select the files sqlceca30.dllsqlcecompact30.dllsqlceer30EN.dllsqlceme30.dllsqlceoledb30.dllsqlceqp30.dll, and sqlcese30.dll, as shown in Figure 15.

    Figure 15. Selecting SQL Server 2005 Compact Edition DLLs

  6. Click Add.The selected DLL files are added to the project and are now visible in the Solution Explorer pane, as shown in Figure 16.

    Figure 16. SQL Server 2005 Compact Edition DLLs in the Solution Explorer

  7. Select all seven of the SQL Server Compact Edition DLL files in the Solution Explorer.
  8. With the seven SQL Server Compact Edition DLL files selected, right-click on the selected DLL files and click Properties.
  9. In the Properties pane, set the value of the Copy to Output Directory property to Copy if newer.With the Copy to Output Directory property set to Copy if newer, Visual Studio copies the SQL Server Compact Edition DLL files to the application directory when building or publishing the project. Visual Studio copies the SQL Server Compact Edition DLL files to the application directory if the DLL files do not exist in the application directory or if the SQL Server Compact Edition DLL files in the project directory are newer than the existing DLL files in the application directory.
  10. Expand References in the Solution Explorer pane, and select System.Data.SqlServerCe.
  11. In the Properties pane, change the value of the Copy Local property to True.With the Copy Local property set to True, Visual Studio copies the System.Data.SqlServerCe assembly to the application directory when building or publishing the project. When you centrally install SQL Server Compact Edition as you did in Exercise 1, the installation process installs theSystem.Data.SqlServerCe assembly in the GAC. In the case of a private SQL Server Compact Edition installation, the System.Data.SqlServerCe assembly might not be available from the GAC; therefore, your application needs the assembly to be in the application directory. When you privately install SQL Server Compact Edition with your application, the application always uses the copy of the System.Data.SqlServerCe assembly in the application directory even if SQL Server Compact Edition is centrally installed on the computer running your application because the .NET assembly loader always prefers a local copy of an assembly over a copy installed in the GAC.

To test that your application copies all required files to the output directory

  1. In Visual Studio, click BuildRebuild Solution and wait for the build to complete.
  2. Display the entire contents of the project directory and subdirectories within the Solution Explorer by clicking the Show All Files button located at the top of the Solution Explorer pane.
  3. Still in the Solution Explorer, expand the bin folder.
  4. Expand the Debug folder.Notice that Visual Studio copies all of the specified SQL Server Compact Edition DLL files, including the System.Data.SqlServerCe assembly, to the output directory, as shown in Figure 17.

    Note If you do not see the DLL files and the assembly listed under the bin\Debug directories, refresh the Solution Explorer view by either clicking the Refresh button on the Solution Explorer or selecting ViewRefresh from the Visual Studio menu.

    Figure 17. Viewing output files in the Solution Explorer pane

To view the list of files that will be installed with your application

  1. In the Solution Explorer, double-click Properties to open the Visual Studio Project Designer.
  2. Select the Publish tab to display the publication settings pane.
  3. On the publication settings pane, click Application Files…, which is located under the Install Mode and Settings section.The Application Files dialog box is displayed, as shown in Figure 18. The ApplicationFiles dialog box lists all of the files that are installed with your application including whether each file is installed in the application directory or the data directory.

    Figure 18. The Application Files dialog box, displaying a list of files to be installed with the application

  4. Dismiss the Application Files dialog box by clicking Cancel.

To publish your application for ClickOnce deployment

  1. In the Solution Explorer pane, right-click the SSCEDeploymentCSharp project.
  2. On the context menu that pops up, click Publish, as shown in Figure 19.

    Figure 19. Clicking Publish on the SSCEDeploymentCSharp context menu

  3. On the Publish Wizard Where do you want to publish the application? dialog box, leave the default location ofhttp://localhost/SSCEDeploymentCSharp/, as shown in Figure 20. Click Finish to complete the wizard.

    Figure 20. Specifying the location to publish the application

    After the wizard builds and publishes the application, the wizard opens Internet Explorer displaying the application installation Web page, as shown in Figure 21. It might take a few moments before the Web page is displayed.

    Figure 21. The generated publication to be used to install the SSCEDeploymentCSharp application

    Notice that the application installation page that Visual Studio generates in this case is simpler than the application installation page from Exercise 1 of this tutorial. By converting the application to install SQL Server Compact Edition locally rather then installing SQL Server Compact Edition centrally, you have eliminated the need for application prerequisite installs; therefore, the application installation page need only provide a way to install the actual application.

To install and run your application with ClickOnce

  • On the publication Web page, click Install to test your application’s installation. Click Install in response to any security warnings.After installation, the application appears, as shown in Figure 22.

    Figure 22. The deployed application

    The application is now fully installed on your local computer and is completely self-contained with all of the SQL Server Compact Edition DLL files and assemblies located in the application directory. The ClickOnce private installation of SQL Server Compact Edition makes SQL Server Compact Edition fully available to your application but does not centrally install SQL Server Compact Edition on the local computer and does not impact any other applications on the local computer. By configuring the application installation to privately install SQL Server Compact Edition, any user can install the application because the user performing the install can do so without being required to have administrator rights.

    Other then installing the SQL Server Compact Edition DLL files and assembly in the application directory, all aspects of your program are identical to when you performed the ClickOnce install in Exercise 1 of this tutorial. The ClickOnce install installs the SQL Server Compact Edition database file on the local computer in the appropriate data directory and all changes that you make to the data in the application are stored to the local database. Just as in Exercise 1, your program is now available under StartAll ProgramsMicrosoftSSCEDeploymentCSharp and you can exit the application then launch it from the Start menu and see that any changes that you make to the application data are maintained between program executions. You can also useAdd or Remove Programs in the Windows Control Panel to uninstall the application.

To verify that the SQL Server 2005 Compact Edition files are installed with the application

  1. If the application is not already running, start it now.
  2. Highlight and copy the contents of the application’s Application Directory text box.
  3. Select StartRun on the Windows Start bar and paste the value that you copied from the Application Directory text box into the Run dialog box.
  4. Click OK.
  5. A Windows Explorer Window opens and displays the contents of the application directory, as shown in Figure 23.In this window, you can see that the application, the SQL Server Compact Edition DLL files, and the System.Data.SqlServerCe assembly are installed into the application directory.

    Figure 23. The contents of the application directory

  6. Now, highlight and copy the contents of the application’s Data Directory text box.
  7. Select StartRun on the Windows Start bar and paste the value that you copied from the Data Directory text box into the Run dialog box.
  8. Click OK.
  9. A Windows Explorer Window opens and displays the contents of the data directory, as shown in Figure 24. In this window, you can see that theNorthwind.sdf data file is installed into the directory.

    Figure 24. The contents of the data directory

Conclusion

In this tutorial, you performed the following exercises:

  • Application deployment using central (or shared) deployment of SQL Server 2005 Compact Edition
  • Application deployment using private (or local) deployment of SQL Server 2005 Compact Edition

In this tutorial, you learned about two different deployment scenarios available for SQL Server 2005 Compact Edition applications. You learned how to perform both a central installation and a private installation of SQL Server 2005 Compact Edition with your application.

This is the forth part in a series of posts about building applications with Microsoft .Net RIA Services and Silverlight 3. In PART 1 of this series I created a new application, created a simple data model and used the Domain Service and Domain Context to retrieve data and bind it to a DataGrid. In PART 2 we replaced the manual work needed to get the data from the server with the DomainDataSource that provided some more advanced scenarios such as filtering and sorting. In this post we’ll introduce the DataForm control and talk more about data validation.

Using the DataForm control

Add A DataForm Control and bind it to the current item of the DataGrid. Open Views\HomePage.xaml, and add a DataForm Control right after the DataPager.

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

<dataControls:DataPager PageSize=”3″

Source=”{Binding Data, ElementName=customersDataSource}” />

<dataControls:DataForm x:Name=”customerDataForm”

Header=”Customer Data”

CurrentItem=”{Binding ElementName=dataGrid, Path=SelectedItem}”>

</dataControls:DataForm>



</Grid>

If you run the application, you should see the DataForm below the DataGrid. Move between the items, and see how the displayed customer’s data is changing as you do that.

.Net RIA Services DataForm Validation

To edit an item, click the pencil icon on the DataForm.

.Net RIA Services DataForm Validation

When you do that, the controls will be replaced with input controls, and Save and Cancel buttons will show up.

.Net RIA Services DataForm Validation

If you change a value of a property in a customer’s data, you’ll get a notification that this data was changed and the customer is not “dirty”.

.Net RIA Services DataForm Validation

Notice that changes that are made here are tracked only in memory. This means, that by pressing the Save button, you’ll switch over to Display more, but the data is not saved to the server. To enable submitting changes back to the server, we need to add a submit button below the DataForm.

<Button x:Name=”btnSubmit”

Width=”120″

Click=”btnSubmit_Click”

Content=”Submit” />

As the implementation of the submit button, you’ll need to commit the changes of the current item that is being edited (similar to EndCurrentEdit() in Windows Forms), and only then pushing the changes back the server.

private void btnSubmit_Click(object sender, RoutedEventArgs e)

{

this.customerDataForm.CommitItemEdit();

this.customersDataSource.SubmitChanges();

}

Adding Validation Logic using Data Annotations

One of the great things in .Net RIA Services is that code that we wrote in the server side can be resued in the client side. This means that we can reuse the validation annotations that we have for our domain entities.

To annotate entities with validation semantics, open the metadata file for the domain service (BankDomainService.metadata.cs in the sample). In this file, you’ll find partial classes that match your domain classes (class Customer in this sample), decorated with MetadataTypeAttribute pointing to a metadata type the is also generated here as an inner class. The metadata class has fields / properties that match the properties that the original class has, that you can annotate with validation requirements.

[MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]

public partial class Customer

{

internal sealed class CustomerMetadata

{

public int CustomerID;

public bool IsBusiness;

public string Name;

public string City;

public string Email;

public EntitySet<Account> Accounts;

}

}

In this examples we’ll use the Regular Expression attribute, the String Length Attribute and the Required Attribute to validate the customer entity, but I encourage you to explore the System.ComponentModel.DataAnnotations namespace more deeply.

internal sealed class CustomerMetadata

{

public int CustomerID;

public bool IsBusiness;

[Required(ErrorMessage=”Please provide a name for the customer”)]

[StringLength(30,

ErrorMessage=”Customer’s name cannot be more than 30 characters”)]

public string Name;

[StringLength(30,

ErrorMessage = “Customer’s city cannot be more than 15 characters”)]

public string City;

[RegularExpression(@”^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$”,

ErrorMessage=”Please provide a valid email address”)]

public string Email;

public EntitySet<Account> Accounts;

}

Build the project and Visual Studio will regenerate the client side code for the domain context, that will now also contain the metadata.

If you now run the application, and try to update a customer’s entity with values that do not match the validations, you will get a visual notification and the data will not be updated to the server.

.Net RIA Services DataForm Validation

In this post I talked about the new DataForm control in .Net RIA Services, showed how to add it to our application and how it works. Then we talked a little bit about annotating the entities with validation requirements and saw how the DataForm enforces them.

Enjoy!

This is the second post in a series of posts about building applications with Microsoft .Net RIA Services and Silverlight 3. In PART 1 of this series I created a new application, created a simple data model and used the Domain Service and Domain Context to retrieve data and bind it to a DataGrid.
In this post we take it from that step, and replace the manual work with the DomainDataSource that provides some more advanced scenarios.

Use a DomainDataSource to easily connect the data

Remove the code that retrieves the data from the server. Open Views\HomePage.xaml and comment out all the code in the Page_Loaded method.

Instead of loading the data manually, add a DomainDataSource control to the page. To do this, add a reference to System.Windows.Ria.Controls.dll, and add the Xml namespace that relates to its contents:

<navigation:Page x:Class=”BankApp.HomePage”


xmlns:ria=”clr-namespace:System.Windows.Controls;
assembly=System.Windows.Ria.Controls”

Loaded=”Page_Loaded”

Title=”HomePage Page”>

</navigation:Page>

Add the DomainDataSource to the page

<navigation:Page x:Class=”BankApp.HomePage”

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

<ria:DomainDataSource x:Name=”customersDataSource”

LoadMethodName=”GetCustomers”

AutoLoad=”True”

LoadSize=”5″>

</ria:DomainDataSource>

</Grid>

</navigation:Page>

Reminder: After creating the DomainService in the server side, Visual Studio has generated a client side DomainContext that does all the magic of connecting to the server for us.

Assign the DomainContext to the DomainDataSource so it can use it to pull data from the server. To do that, add a local xml namespace:

<navigation:Page x:Class=”BankApp.HomePage”

xmlns:local=”clr-namespace:BankApp.Web”

Title=”HomePage Page”>

</navigation:Page>

And do the assignment:

<ria:DomainDataSource x:Name=”customersDataSource”

LoadMethodName=”LoadCustomers”

AutoLoad=”True”

LoadSize=”5″>

<ria:DomainDataSource.DomainContext>

<local:BankDomainContext />

</ria:DomainDataSource.DomainContext>

</ria:DomainDataSource>

The last thing we need to do is to bind the DataGrid to the DomainDataSource.

<data:DataGrid MinHeight=”200″

x:Name=”dataGrid”

ItemsSource=”{Binding Data, ElementName=customersDataSource}”>

</data:DataGrid>

Run the application, and see how the data is being loaded in chunks according to the LoadSize property of the DomainDataSource.

Add a DataPager Control to enable paging through the data. To do that, add a reference to System.Windows.Controls.Data.DataForm.dll the following xml namespace:

xmlns:dataControls=
“clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm”

Add the DataPager control and bind it to the DomainDataSource:

<data:DataGrid MinHeight=”200″ …>

</data:DataGrid>

<dataControls:DataPager PageSize=”3″

Source=”{Binding Data, ElementName=customersDataSource}” />

If you now run the application, you should get a result similar to this:

DomainDataSource DataGrid DataPager

Data Grouping

Add Grouping support to the DataGrid. Add the following xml namespace:

xmlns:riaData=”clr-namespace:System.Windows.Data;assembly=System.Windows.Ria.Controls”

Then, add a group descriptor to group the results by the property City.

<ria:DomainDataSource …>

<ria:DomainDataSource.GroupDescriptors>

<riaData:GroupDescriptor PropertyPath=”City” />

</ria:DomainDataSource.GroupDescriptors>

</ria:DomainDataSource>

The result should look like this:

DataGrid DomainDataSource Grouping GroupDescriptor

Data Sorting

To add sorting support, add a SortDescriptor:

<ria:DomainDataSource …>

<ria:DomainDataSource.SortDescriptors>

<riaData:SortDescriptor PropertyPath=”CustomerID”

Direction=”Descending” />

</ria:DomainDataSource.SortDescriptors>

</ria:DomainDataSource>

Rich Data Filtering

Add a dynamic filtering support based on a selected value of a CheckBox. Add a Checkbox above the DataGrid.

<CheckBox x:Name=”chkBusiness”

Content=”Business Customer ?” />

<data:DataGrid x:Name=”dataGrid” …> </data:DataGrid>

Add a FilterDescriptor that filters data according to the IsBusiness property of each item. Bind the FilterDescriptor to the IsChecked property of the above checkbox.

<ria:DomainDataSource x:Name=”customersDataSource” … >

<ria:DomainDataSource.FilterDescriptors>

<riaData:FilterDescriptorCollection>

<riaData:FilterDescriptor PropertyPath=”IsBusiness”

Operator=”IsEqualTo”>

<riaData:ControlParameter ControlName=”chkBusiness”

PropertyName=”IsChecked”

RefreshEventName=”Click” />

</riaData:FilterDescriptor>

</riaData:FilterDescriptorCollection>

</ria:DomainDataSource.FilterDescriptors>

</ria:DomainDataSource>

Run the application, check and uncheck the checkbox, and see how the data is refreshed accordingly.

FilterDescriptors DomainDataSource

Conclusion

In this post we took on from where we left at PART 1, and replaced the manual work needed to get the data from the server with the DomainDataSource that provides some more advanced scenarios. In the next post we’ll introduce the DataForm control and talk more about data validation.

Enjoy!

This is the first post in a series of posts about building applications with Microsoft .Net RIA Services and Silverlight 3. In this post I will create a new application, create a simple data model and use the Domain Service and Domain Context to retrieve data and bind it to a DataGrid.

Before you start, make sure you have Silverlight 3 Beta and .NET RIA Services March 2009 Preview installed, and you have already installed and configured SQL Server.

Create a Silverlight Navigation Application

Create a new Silverlight Navigation Application.

Walkthrough .Net RIA Services Silverlight 3

After you click OK, the New Silverlight Application Dialog is shown. Click OK again to create an ASP.Net project that links to the new Silverlight Application.

Walkthrough .Net RIA Services Silverlight 3

A new solution is created. Notice the new assemblies that the Silverlight project is referencing, and notice the new assemblies among them.

Walkthrough .Net RIA Services Silverlight 3

Build a Domain Service

Add a new Data Model to your server side project (BankApp.Web). This data model can be a LINQ to SQL model, an Entity Data Model, or you can use any other business object representation.

In this sample I am using a LINQ to SQL data model.

Walkthrough .Net RIA Services Silverlight 3

Make sure to build the project so that Visual Studio will generate the data classes and data context before the next step.

Add a new Domain Service. Add a new Item to the server project, and select the Domain Service template in the Web category.

Walkthrough .Net RIA Services Silverlight 3

After you add this item, the New Domain Service Class Dialog is shown. Select the Data Context (BankDataContext in this sample), select the entities you want to expose and whether you want to allow editing and click OK.

Walkthrough .Net RIA Services Silverlight 3

Walkthrough .Net RIA Services Silverlight 3This adds the BankDomainService.cs that contains the code that exposes the data to the client, and BankDomainService.metadata.cs that contains additional metadata, mostly for presentation and validation.

A few references were also added: System.ComponentModel.DataAnnotations, System.Web.DomainServices, System.Web.DomainServices.Providers and System.Web.Ria.

In addition to that, a new Http Handler was added to the web.config file.

<httpHandlers>

<add path=”DataService.axd”

verb=”GET,POST”

type=”System.Web.Ria.DataServiceFactory, System.Web.Ria, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″

validate=”false” />

</httpHandlers>

Walkthrough .Net RIA Services Silverlight 3Build the solution. This executes a build target the generates the client side code that is required in order to consume the Domain Service. If you click the Show All Files button for the client application, you’ll notice the generated code.

Display Domain Data in the Application

Add a DataGrid Control to the client application. To do that, add a reference to System.Windows.Controls.Data.dll that contains the DataGrid Control.

Then, open the Views\AboutPage.xaml and add an xmlns prefix to the CLR namespace of the DatGrid.

<navigation:Page x:Class=”BankApp.HomePage”


xmlns:data=”clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data”

>

Add the Xaml markup for the DataGrid Control:

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

<StackPanel>

<StackPanel Style=”{StaticResource ContentTextPanelStyle}”>

</StackPanel>

<data:DataGrid MinHeight=”200″ x:Name=”dataGrid”>

</data:DataGrid>

</StackPanel>

</Grid>

Handle the Loaded Event of the Page. First, Add a method to handle the Loaded event of the page.

<navigation:Page x:Class=”BankApp.HomePage”


Loaded=”Page_Loaded”

Title=”HomePage Page”>

In the method that handles the event, use the client Domain Context to retrieve data from the service and bind to the DataGrid.

private void Page_Loaded(object sender, RoutedEventArgs e)

{

BankDomainContext context = new BankDomainContext();

this.dataGrid.ItemsSource = context.Customers;

context.LoadCustomers();

}

Now, run the application and let it go and retreive the data and present it in the DataGrid.

Walkthrough .Net RIA Services Silverlight 3

In this post I created a new application, created a simple data model and used the Domain Service and Domain Context to retrieve data and bind it to a DataGrid. In the next post I’ll explore more controls that ship with the .Net RIA Services.

Enjoy!

Hello All!

Posted: October 31, 2009 in Uncategorized

Hello All! This is Chandan Deo, an enthusiastic software professional working on leading patient health care software products. I will be back soon with more for you to share with…….