DOWNLOAD THE CODE:
Download the Code 97104.zip

Creating a Custom Dataset or SQLDataSource

In both Windows Forms and ASP.NET, once you’ve laid out your form with the previously mentioned controls, you’ll see the context menu for the grid in the grid’s upper right corner. On this menu, you’ll see a Choose Data Source dropdown list. For the Windows Forms application, you’ll find that when you open this list for a new project, an Add Project Data Source link will appear near the bottom. In ASP.NET, opening the same Choose Data Source option reveals a simple dropdown list with the option to create a new data source. In either case, you’ll enter a wizard screen that lets you specify the underlying source of your data—a database— then lets you to build a connection string for that data source. These are fairly common steps, so I won’t go into detail. In both cases, the final step in this process of defining your data objects is to automatically add the connection information to your associated .config file.

Having created your data source, you now need to define your data objects. Figure 1 shows the next Windows Forms screen. I’ve expanded the Stored Procedures section. The wizard can’t associate all the stored procedures into a single table adapter, so after the wizard finishes, I’ll need to complete this step for my Windows Forms solution. At this point, I can finish the wizard, which will create the XML Schema Definition (XSD) describing my data set, add this element to my project, and let me edit the definition of my table adapter.

The ASP.NET wizard is a bit more involved. Its first screen lets you either select one or more tables or choose the Specify a custom SQL statement or stored procedure option. Selecting the custom option, you’ll then press Next, at which point the wizard lets you specify either a custom SQL statement or a stored procedure for SELECT, UPDATE, INSERT, and DELETE.

After you fill in all four stored procedures and press Next, you’ll map the parameter for your Select statement to a field outside your data grid. On the assumption that this parameter needs to come from somewhere, the wizard lets you map either a query string value or, in this case, a form value. As I described earlier, there will be a text box on the form, so this control is mapped as the source of this parameter. (If the text box is empty, the system provides a default value of ALFKI—the customer ID for the first customer in the Northwind sample database.)

Once you’re satisfied with your settings on this screen, the final screen lets you execute a test run of your query statement. Unlike the Windows Forms solution, the ASP.NET solution updates your default. aspx page. The default.aspx page will use the data definition embedded in the page to make the calls to the database. Now that we’ve finished setting the data source for ASP.NET, let’s return to Windows Forms and set up a basic data-bound grid.

Windows Forms Data Binding

As I mentioned earlier, the Windows Forms wizard doesn’t really understand how to map additional stored procedures to your data adapter. As you’ll see, this shortcoming has implications beyond the wizard. First, a quick review: By now, you’ve created a new Windows Application project, similar to the WinForm DataBinding.zip sample available for download at www.sqlmag.com, InstantDoc ID 97104. Your application would have automatically generated a Form 1, and you’ll have used the design to lay out your form similar to what Figure 2 shows.

Notice that in addition to showing the form, the figure also shows the grid control’s context menu, from which you defined your data source. Also, note that the associated options for Adding, Editing, Deleting, and Sorting are selected. This control’s context menu is quite different from that of the ASP.NET GridView control (discussed later).

Finally, by clicking on the Edit Columns option, you can change the default width of the various columns, map in different display headers, make key columns Read Only, and even make certain columns such as Company ID invisible so that they won’t appear in your UI.

After you finish customizing the DataGridView control from the context menu, it’s time to review the capabilities of your custom data set. To do so, review your solution in the solution explorer and locate the .xsd file, which was added as part of the wizard process that created your data object. The sample code has one called dataset1.xsd, and double-clicking this file opens a graphical representation of the newly created typed dataset and its associated table adapter. Working with a table adapter is important because it’s the adapter that lets you map in your database actions as stored procedures. Right-click the table adapter, and select Configure from the context menu.

The Configure option opens the screen that Figure 3 shows. This wizard is designed to let you group related stored procedures onto a given table adapter. After you fill in your stored procedures in the wizard, you’ll proceed to Figure 4, which shows the default methods to be created for your table adapter.

Prev. page     1 [2] 3     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.

 
 

ADS BY GOOGLE