October 07, 2003
Designing Typed DataSets in Visual C#'s XML Designer

These are my personal notes for designing typed datasets in Visual C#.NET's XML Designer.

Naming Conventions


  • Name your dataset file and your dataSetName with the "Set" postfix, e.g. CustomerSet.xsd
  • Pluralize the name of your Table elements, e.g. Customers
  • Capitalize the names of your elements, which corresponds to DB Schema conventions although XML elements tend to be lower-cased.

Inserting New Rows Into a Typed DataSet


The Visual Studio documentation is incorrect where it explains how to insert new rows into a typed dataset.
It gives the example:
// C#
DataRow anyRow = DatasetName.ExistingTable.NewRow();
anyRow.FirstName = "Jay";
anyRow.LastName = "Stevens";
ExistingTable.Rows.Add(anyRow);
But this is incorrect as the DataRow type is generic and does not contain the FirstName or LastName fields. A more specific type and a cast are necessary, as in:
// C#
ExistingDataSet.ExistingTableRow anyRow =
(ExistingDataSet.ExistingTableRow) DatasetName.ExistingTable.NewRow();
anyRow.FirstName = "Jay";
anyRow.LastName = "Stevens";
ExistingTable.Rows.Add(anyRow);
But if it's going to be this complicated, you might as well follow the alternative instructions that work for both untyped and typed datasets. See the documentation.

Inserting Related Rows Into Related Tables


If you create an XML Schema that has 2 tables in a parent-child relationship, then a DataRelation will be automatically created for you by Designer. You won't see it, but a column in the child table will automatically be created for a foreign key to match up with the primary key in the parent table. The question is: how do you insert a new row into both tables so that the relationship is maintained? Do the following in the specific order:
  1. Create the parent row, set the column values, and add it to the parent table
  2. Create the child row, set the column values
  3. Call SetParent() on the child row with the parent as the argument, e.g. childRow.SetParent(parentRow)
  4. Add the child row

I could not find how to do this in the documentation either.

Assigning a DataSource to a DataView at run-time


If a DataSource is created at run-time in the Text Editor as opposed to design time in the Designer, and you want to have a DataView associated with it, this DataView should also be initialized at run-time. The problem is if you assign the source to the DataView after its EndInit() method is called, then RowFilter won't work.

Posted by juliob at October 07, 2003 01:54 AM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?

Please enter the Security Code as shown below:




License:
Creative Commons License