Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Typed Datasets: A First Step to .NET Flexibility : Page 2

One of your first steps to serious .NET development should be creating and implementing flexible typed datasets to maintain control and protect the validity of your data.




Application Security Testing: An Integral Part of DevOps

Creating the Typed Dataset

Many solutions contain multiple projects. The following example contains two projects: one contains a simple user interface and a second contains the appropriate data structures that the user interface references. Often, it doesn't matter which project you add first. However, the namespace in this example will use the name given to the first project. For that reason, you will create the user interface project, a Windows Forms application, first:
Figure 1. Create New Project: Open a new project.
  1. Launch Visual Basic Express and create a new solution by adding a Windows Forms project. Choose New Project from the File menu, and then select Windows Forms Application. Name the project NorthwindTypedDatasetExample, as shown in Figure 1, and click OK.
  2. To establish a connection to Northwind, the database that comes with MS Access, choose Connect to Database from the Tools menu (Or right-click Data Connections in the Database Explorer, and choose Add Connection). Use the Browse button in the resulting Add Connection dialog box to locate Northwind.mdb and then click OK.
  3. If you don't see the solution file in the Solution Explorer as shown in Figure 2, choose Options from the Tools menu. Expand the Projects and Solutions node and click General. To the right, check the Always Show Solution option, as shown in Figure 3, and click OK. If the solution file is hidden, VB Express works with only one project at a time. As stated previously, this solution requires two projects.

    Figure 2. VB Express Solution File: Make sure the solution file is visible in the Solution Explorer.
    Figure 3. Show Solution Option: To work with two projects in one solution, you must check the Always Show Solution option.
  4. Figure 4. Startup Project: Identify the Windows Forms Application project as the solution's startup project.
  5. Right-click the new project (not the solution) and choose Set As Startup Project, as shown in Figure 4. Doing so allows you to run this project when you start the application.

At this point, you have a solution that's connected to the Northwind database and a Windows Forms application project. You have just started the user interface project, which needs a bit more work. First, however, add a second project: a class library containing the data structures that the form project you just built will reference.

To add the class library, do the following:

  1. To add the second project (making sure the solution container is visible and selected), right click on the solution, select Add from the resulting menu, and then choose New Project as shown in Figure 5. If prompted to save the current project, click Save.
    Figure 5. Add New Project: Add a new project to the solutions file.
    (You can add a class library from the File menu, but it won't work as expected unless you are in the solution namespace. To add a second project to the solution, you must work from the solution file in the Solution Explorer.)
  2. In the resulting Add New Project dialog shown in Figure 6, select Class Library, name the new library NorthwindDataAccess, and click OK. At this point, you have two projects in the Solution Explorer, as shown in Figure 7.

    Figure 6. Select Class Library: Add a class library to the current solution.
    Figure 7. Two Projects in the Solution Explorer: The current solution comprises two projects, a Windows Forms application and a class library.

The data structure you'll add to the Class Library project is a typed dataset. To do so, complete the following steps:

  1. To add a new typed dataset to the Class Library project, right-click NorthwindDataAccess (in Solution Explorer). Choose Add from the resulting submenu, and then select choose New Item.
  2. In the resulting Add New Item dialog box, select DataSet. Name the new dataset CustomersDataset, as shown in Figure 8, and then click Add.
  3. In the Database Explorer, expand the Northwind.mdb connection and drag the Customers table to the Dataset Designer, as shown in Figure 9. You just added a typed dataset to the Class Library project. If the Database Explorer isn't visible, press Ctrl+Alt+S or choose Database Explorer from the View menu.

  4. Figure 8. New Dataset: Create a new dataset named CustomersDataset.
    Figure 9. New Typed Dataset: VB Express adds a reference to the new dataset to the Solution Explorer.

At this point, you have the two projects your solution needs. To work with both projects, you need a reference between them. The reference should be in only one direction. If you want the Windows Forms Application project to use data from the Class Library project's NorthwindDataset, you must set the reference in the Windows Forms Application as follows:

Figure 10. Add Reference: Connect the two projects by adding a reference from NorthwindTypedDatasetExample to NorthwindDataAccess.
  1. Right-click the NorthwindTypedDatasetExample project (not the solution) in the Solution Explorer, and choose Add Reference. In the resulting Add Reference dialog box, click the Projects tab.
  2. Select NorthwindDataAccess, as shown in Figure 10, and click OK.

This reference is necessary for the code to recognize the existence of the objects it's importing, but it won't always be necessary to add such a reference at this point (only for code that needs to recognize the existence of your objects). However, it won't hurt to add it, so you might as well. If you forget, it'll be harder to troubleshoot its absence later.

Now, you're ready to use the Customers typed dataset as follows:

  1. With Form1.vb in Design view, drag a DataGridView from the Data section of the Toolbox to the form.
  2. Figure 11. Find the Dataset: Expand Form1ListInstances to display CustomersDataset1.
  3. From the NorthwindDataAccess Components section of the Toolbox (newly added), drag CustomersDataset to the form. Just dragging the dataset to the form won't create a DataGridView the way dragging a table does.
  4. VB Express will display the DataGridView Tasks dialog box. Open the Choose Data Source control's dropdown list (currently displays none) and expand Other Data Sources. Expand Form1ListInstances to display CustomersDataset1, as shown in Figure 11. CustomersDataset1 is an alias for CustomersDataset.
  5. With the DataGridView still selected, set the DataSource property to CustomersDataset1 and the DataMember property to Customers. Doing so will update the control's data structure. Expand the size of the form and the DataGridView to display as many columns as possible.
  6. Return to the form's code module and update Class Form1 by entering the following code, as shown in Figure 12:

    Imports NorthwindDataAccess.CustomersDataset Imports NorthwindDataAccess.CustomersDatasetTableAdapters Public Class Form1 Dim _adapter As New CustomersTableAdapter Dim dataset As New NorthwindDataAccess.CustomersDataset Private Sub Form1Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load adapter.Fill(dataset.Customers) Me.DataGridView1.DataSource = _dataset End Sub End Class

  7. Press F5 to run the form. Figure 13 shows the typed dataset displaying customer data from the Customers table in Northwind.mdb.

    Figure 12. Update Class Form1: Update Class Form1 by entering the following the code that populates the DataGridView.
    Figure 13. Typed Dataset Displaying Customer Data: The main project's form displays data from the Customers table.

When you execute the solution, VB Express runs the form. The form's code uses the data from the Customers table (via the connection to Northwind) to fill the form's DataGridView. The code itself is brief—you don't have to hard code a connection string or a single control. That's the beauty of .NET.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date