Annotations are simply a set of extensions to the raw XSD
file that is used by .NET to generate the typed DataSet. Even after using the
typed dataset, still the code is not that readable. For example, if your table
is named Customers, the DataTable class will be named CustomersDataTable, the
DataRowCollection will be named Customers, and the method to create a new
DataTableRow is called NewCustomersRow. You need to add codegen annotations to
change the typedPlural and typedName of the table element in XSD file to make
it more readable. We can also change the column name using these annotations.
We use annotation because it will retain the original names without modifying
it in the schema so that typed dataset schema and database table schema remain
the same- it is something similar to adding an alias.
To change “ProductsDatable” to BabulivesDatatble and
“NewProductsRow” to “BabuLivesProductsRow” we can modify the XSD to add
annotations.
Modified XSD:
Listing 11: XSD Code
<xs:element name="Products" codegen:typedName="BabuLivesProduct"
codegen:typedPlural="BabuLivesProducts">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductID" msdata:AutoIncrement="true"
msprop:Generator_UserColumnName="ProductID"
msprop:Generator_ColumnPropNameInRow="ProductID"
msprop:Generator_ColumnVarNameInTable="columnProductID"
msprop:Generator_ColumnPropNameInTable="ProductIDColumn"
type="xs:int" />
<xs:element name="ProductName"
msprop:Generator_UserColumnName="ProductName"
msprop:nullValue="_null"
msprop:Generator_ColumnPropNameInRow="ProductName"
msprop:Generator_ColumnPropNameInTable="ProductNameColumn"
msprop:Generator_ColumnVarNameInTable="columnProductName"
type="xs:string" minOccurs="0" />
<xs:element name="UnitPrice"
msprop:Generator_UserColumnName="UnitPrice"
msprop:Generator_ColumnPropNameInRow="UnitPrice"
msprop:Generator_ColumnVarNameInTable="columnUnitPrice"
msprop:Generator_ColumnPropNameInTable="UnitPriceColumn"
type="xs:decimal" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
To make this work we need to add a namespace in the XSD
file, locate the following element in XSD file and add the namespace that is
bolded here.
Listing 12: XSD code with modified namespace
<xs:schema id="Products" targetNamespace="http://tempuri.org/Products.xsd"
xmlns:mstns="http://tempuri.org/Products.xsd"
xmlns="http://tempuri.org/Products.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"
attributeFormDefault="qualified" elementFormDefault="qualified">
After compiling, the intellisense will show like:
Figure 15– Annotations intellisense
Handling Nulls using annotations
As we know, null value in a primitive datatype column
returns an exception; instead we can make to it return a default value:
By adding the above attribute to a column tag in XSD it will
prevent the exception by giving this default value.
The following table lists the annotations to change the
elements in typed dataset.
DataSet Element
|
Default Naming
|
Annotation to Modify
|
DataTable
|
TableNameDataTable
|
typedPlural
|
DataTable methods
|
NewTableNameRow
AddTableNameRow
DeleteTableNameRow
|
typedName
|
DataRowCollection
|
TableName
|
typedPlural
|
DataRow
|
TableNameRow
|
typedName
|
DataSet Events
|
TableNameRowChangeEvent
TableNameRowChangeEventHandler
|
typedName
|
What happens under hood?
When we drag a table into typed dataset, designer visual
studio normally generates the XML schema. There is a tool called XSD.EXE in
framework SDK which generates the typed dataset using xml file generated. This
is the tool that is generating the typed dataset from the xsd file created.
Common hurdle in Visual Studio
Sometimes when you work with dataset designer in visual
studio you will get the following error.
Configure TableAdapter Products Failed
Key not valid for use in specified state
Resolution
This error is because there may be some dump connection in
server explorer of visual studio like the one below that is disconnected.
Figure 16– Server explorer with dumb connections
Deleting those connections will solve the problem.