Export (0) Print
Expand All

Data Form Wizard Generated Code

Visual Studio .NET 2003

The Data Form Wizard helps you create data-bound Web Forms pages or Windows Forms. The sections below provide some detail on the code generated by the wizard, including why the code is designed the way it is and how you might modify it after running the wizard.

The wizard is used to generate different configurations of Web Forms pages and Windows Forms. As a consequence, the code that it generates is designed to handle a variety of situations, including various combinations of controls on the form and various data sources. For more information, see Data Form Wizard.

The wizard-generated code is therefore not designed for any specific form configuration. One of the purposes of the wizard is to generate code that you can study to learn how to create your own data-bound forms. However, the wizard-generated code can be optimized through modification based on your particular application.

Using the .NET Data Provider for OLE DB

The Data Form Wizard generates all data access code using the .NET Data Provider for OLE DB (OleDbConnection, OleDbDataAdapter, and so on), even if you are accessing a SQL Server database that is version 7.0 or later. This is a safe strategy for the wizard, because the OLE DB provider works with all available data sources, including SQL Server. This approach also gives you the option to change data sources later without having to make extensive changes to the data-access code in the form.

Note   When writing your own code, you can choose between the .NET Data Provider for OLE DB and the .NET Data Provider for SQL Server. As a rule, you should use the .NET Data Provider for SQL Server if you are accessing SQL Server version 7.0 or later, and if you know that the data source for the form will not change.

It is possible to modify the data provider in the wizard-generated code to use the data provider for SQL Server, but this can be difficult. You must change references to any elements in the System.Data.OleDb namespace to System.Data.SqlClient. In addition, you must change the way parameters are handled. The SQL provider requires named parameters, which would require changes — both in the SQL statements in the adapter's commands, as well as in their Parameters collection. If you want to switch to the SQL provider, you might find it easier to manually add SqlConnection and SqlDataAdapter objects and configure them, allowing Visual Studio to configure their parameters.

Merging Datasets During Load

The wizard actually generates two datasets: one that is the data source for controls and a second one that is filled by the data adapter with information from the data source. In its LoadDataSet method, the wizard merges these two datasets. The code looks like this for a dataset named dsCustomers:

' Visual Basic
    End Sub
        Public Sub LoadDataSet()
        ' Creates a new dataset to hold the records returned from the call to FillDataSet.
        ' A temporary dataset is used because filling the existing dataset would
        ' require the databindings to be rebound.
        Dim objDataSetTemp As WindowsApplication1.dsCustomers
        objDataSetTemp = New WindowsApplication1.dsCustomers()
        Try
            ' Attempts to fill the temporary dataset.
            Me.FillDataSet(objDataSetTemp)
        Catch eFillDataSet As System.Exception
            ' Insert code to handle errors.
            Throw eFillDataSet
        End Try
        Try
            ' Empties the old records from the dataset.
            objdsCustomers.Clear()
            ' Merges the records into the main dataset.
            objdsCustomers.Merge(objDataSetTemp)
        Catch eLoadMerge As System.Exception
            ' Insert code to handle errors.
            Throw eLoadMerge
        End Try

    End Sub
// C#
public void LoadDataSet()
{
   // Creates a new dataset to hold the records returned from the call to FillDataSet.
   // A temporary dataset is used because filling the existing dataset would
   // require the databindings to be rebound.
   WindowsApplication1.dsCustomers objDataSetTemp;
   objDataSetTemp = new WindowsApplication1.dsCustomers();
   try 
   {
      // Attempts to fill the temporary dataset.
      this.FillDataSet(objDataSetTemp);
   }
   catch (System.Exception eFillDataSet) 
   {
      // Insert code to handle errors.
      throw eFillDataSet;
   }
   try 
   {
      // Empties the old records from the dataset.
      objdsCustomers.Clear();
      // Merges the records into the main dataset.
      objdsCustomers.Merge(objDataSetTemp);
   }
   catch (System.Exception eLoadMerge) 
   {
      // Insert code to handle errors.
      throw eLoadMerge;
   }
}

It is not necessary to create the two datasets if you are working directly with the data source. However, if you are getting the dataset from another component — often, an XML Web service — you cannot bind controls to a dataset returned by the component. In that case, you do need two datasets: the local version for data binding, and the version returned by the component as the actual data source.

Because the wizard cannot determine whether the dataset you want to use is being provided by a component, it always assumes that it is and generates code to create the two datasets and reconcile them with the Merge function. The code works for all cases, even for datasets directly filled from the data source.

Nonetheless, using a second dataset and the Merge function introduces some overhead, and is therefore not recommended for cases where you know that the dataset is being filled directly from the data source. You can modify the wizard-generated code to remove the second dataset and the Merge function. For example, you might modify the code above to read like the following:

' Visual Basic
Public Sub LoadDataSet()
   Try
      ' Executes the SelectCommand on the DatasetCommmand and fill the dataset.
      Me.FillDataSet(myproject.objdsCustomers)
   Catch eFillDataSet As System.Exception
      ' Insert code to handle exceptions.
      Throw eFillDataSet
   End Try
End Sub

// C#
public void LoadDataSet()
{
   try 
   {
      // Executes the SelectCommand on the DatasetCommmand and fill the dataset.
      this.FillDataSet(objdsCustomers);
   }
   catch (System.Exception eFillDataSet) 
   {
      // Insert code to handle exceptions.
      throw eFillDataSet;
   }
}

Updating Data in a Web Forms Page

The Data Form Wizard does not create update code for Web Forms pages. Data binding in Web Forms pages is one-way — that is, read-only — and the issues associated with updating data from Web Forms pages are more complex than they are in Windows Forms. For more information, see Introduction to Data Binding in Web Forms Pages.

Updating data on a Web Forms page created by the Data Form Wizard consists of these steps:

  1. Allowing users to modify data in the grid.
  2. Collecting the modified data.
  3. Executing a SQL Update statement or stored procedure, passing the modified values as parameters.

For a step-by-step example of how to update a database from a Web Forms page, see Walkthrough: Using a DataGrid Web Control to Read and Write Data.

See Also

Data Form Wizard | Dataset Updates in Visual Studio .NET | Web Forms Data Binding | Introduction to Data Adapters | Parameters in Data-Adapter Commands | Introduction to Data Access in Web Forms Pages

Show:
© 2015 Microsoft