This documentation is archived and is not being maintained.

Adding Untyped Datasets to a Form or Component

Visual Studio .NET 2003

An untyped dataset is an instance of the DataSet class that you create in a form or component. Unlike a typed dataset, an untyped dataset is not an instance of a class generated using an XML Schema (.xsd) file. As a consequence, an untyped dataset has no inherent structure. You create the tables, columns, constraints, and DataRelation objects yourself at design time in the Properties window, at run time in code, or by allowing a data adapter's mappings to do so when you use the data adapter to fill the dataset.

Note   Although an untyped dataset is not based on a class that incorporates a schema, after building the untyped dataset, you can export its structure to a schema file. For more information, see the DataSet.WriteXmlSchema method.

Creating the Dataset

The instructions below describe how to create an untyped dataset using the Component Designer while editing a form or component. For more information about how to create an untyped dataset at run time in code, see Creating a DataSet and Adding a DataTable to a DataSet.

Note   When you deploy an application that includes Visual Studio data-access components, you must make sure that the user installing the application has version 2.7 or later of the Microsoft Data Access Components (MDAC). For more information, see Adding a Launch Condition for Microsoft Data Access Components.

To create an untyped dataset at design time

  1. From the Data tab of the Toolbox, drag a DataSet element onto the form or component you are working with.

    The Choose a Dataset dialog box is displayed.

  2. Select Untyped Dataset and click OK.

    A blank dataset is added to the form or component.

  3. If you want to give the dataset a new name, select it, open the Properties window, and edit the following two properties.
    • (Name)   This property sets the object name by which you refer to the dataset in code.
    • DataSetName   If you export the dataset to an XML document, this property sets the name of the dataset in the document (that is, the name of the topmost element). Setting this property can help avoid problems with name duplication or case sensitivity.

Adding Tables and Columns

You can use the Properties window to create the structure of the untyped dataset. The dataset includes a Tables property that is a collection of the DataTable objects defined for the dataset. Each table in turn supports a Columns property that is a collection of DataColumn objects. To create tables and columns, therefore, you use the Collection Editor feature of the Properties window.

Note   You cannot create records in an untyped dataset at design time; you can populate a dataset only at run time. The Rows collection of a dataset is therefore not available in the Properties window.

To add tables and columns to the dataset

  1. In the form or component, select the dataset and open the Properties window.
  2. In the Tables property, click the button to open the DataTable Collection Editor dialog box.
  3. Click Add to create a new table. If you want to rename the table, select it and in the right pane of the properties grid, change the value of the (Name) property.
    Note   The names of all tables in all datasets in the form or component share the same namespace. Therefore, you cannot use a table name in one dataset if that name is in use in another dataset.
  4. To add columns to the table, do the following:
    1. Make sure the table you are working with is selected in the Members list, and then in the Properties grid on the right, click the button in the Columns property to display the DataColumn Collection Editor dialog box.
    2. Click Add to create a new column, and then set the new column's properties in the grid on the right. Some of the properties allow you to set column constraints, such as a unique constraint or a non-null constraint. Typical properties to set are listed in the following table.
      PropertyDescription
      AllowDBNullA Boolean value indicating whether the column can be left blank in a given record.
      ColumnNameThe name of the column in the table's Tables collection, which is how the column can be referenced in code.
      DataTypeA .NET Framework data type to be enforced for the column. If you import or export an XML document to or from this dataset, data types are automatically converted to schema types (for example, System.String is converted to xs:string and vice versa). Select a data type from the drop-down list.
      ReadOnlyA Boolean value indicating whether a column can be changed.
      UniqueA Boolean value indicating a unique constraint on the column. You always set this to true for a primary key column, but it can be set for any column.
      (Name)An instance name for the column used only at design time.
    3. Repeat Step b for each column you want to add.
    4. When you are finished adding columns for the current table, click OK to close the DataColumn Collection Editor dialog box.
  5. To set a primary key for the table, use the drop-down list in the PrimaryKey property.
    1. In the drop-down list, select the check box next to each part of the primary key.
    2. After selecting one or more columns for the primary key, you can expand the PrimaryKey property and set properties for each primary key column. The Unique property is automatically set for all the columns that are part of the primary key.
  6. Repeat Steps 3 through 5 for each table and set of columns that you want to add to the dataset.

Adding Constraints

You can define two types of constraints in a dataset:

  • A unique constraint that checks that the new values in a column are unique in the table.
  • A foreign-key constraint that defines rules for how related child records should be updated when a record in a master table is updated or deleted.

To add constraints to an untyped dataset at design time

  1. In the form or component, select the dataset and open the Properties window.
  2. Click the button in the Tables property to open the Tables Collection Editor.
  3. Select the table to add a constraint to, and then click the button in the Constraints property to open the Constraints Collection Editor.
  4. Click Add and then select Unique Constraint or Foreign Key Constraint from the drop-down list.

    The Unique Constraint or Foreign Key Constraint dialog box id displayed. For help on filling in these dialog boxes, press F1.

Adding Relations

You can also create relationships (DataRelation objects) between tables in the dataset at design time. For more information about DataRelation objects, see Introduction to DataRelation Objects.

To add a DataRelation object to an untyped dataset at design time

  1. In the form or component, select the dataset and open the Properties window.
  2. In the Relations property, click the button to open the DataRelation Collection Editor dialog box.
  3. Click Add to create a new data relation.

    The Data Relation Editor dialog box opens with default values for the new relationship.

  4. In the dialog box, set values according to the following table.
    ValueDescription
    NameThe name by which you can refer to this DataRelation object in code. A typical name includes the names of both tables being related (for example, CustomersOrders).
    Parent tableThe table containing the parent records. Select a table from the drop-down list.
    Child tableThe table containing child records. Select a table from the drop-down list.
    Parent columnThe names of the columns that make up the primary keys in the parent table. You can select column names from the drop-down list or type in a column name (for example, if you know the name of a column that will be defined at run time).
    Child columnThe names of the columns that make up the foreign keys in the child table.
    Enforce constraintsA Boolean value indicating whether the DataRelation object will be used to cascade changes from the parent table to the child table and vice versa.
    Note   If you choose to enforce constraints, a foreign key constraint is added to the parent table.
    Update rule
    Delete rule
    Accept/reject rule
    The nature of the cascaded change that should be made in the child table when changes occur in the parent table. For more information about the settings for these values, see Introduction to DataRelation Objects.
  5. Click OK to save the changes to this relationship.
  6. Repeat Steps 3 through 5 for each relationship you want to create.

See Also

Introduction to DataRelation Objects | Adding Constraints to a Table | DataSet.WriteXmlSchema Method

Show: