Typed Dataset and its Usages
page 8 of 10
by Satheesh Babu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 44761/ 65

Annotations

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:

codegen:nullValue="0"

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.


View Entire Article

User Comments

Title: Why good for only very small projects?   
Name: Mark
Date: 2010-11-14 5:14:00 PM
Comment:
Why are Typed DataSets viable only if our project sizes are really small? How about a hybrid: BOs in the middle and UI tiers; Typed DataSets at the DataLayer side--Resolving data-changes from BOs to DataSets?
Title: I need urgent help plz   
Name: Fadi
Date: 2010-06-13 2:10:47 PM
Comment:
hi there, i am using typed dataset for my rdlc reports in my winforms app with C#. i m inserting data in database form different forms. the problem is, on run time when i insert data in the database its not updated data in rdlc reports, to get new data in my reports i need to close my app and debug it again to see new data in the reports. Is there any way to forcedly refresh data in my typed dataset on runtime???? Looking forward from your positive response.

Fadi
Title: developer   
Name: John Jones
Date: 2009-08-27 5:19:58 PM
Comment:
One point I forgot to mention:
Our main goal is to use the same SQL queries (basic inserts, updates, deletes) in both MS Access and SQL Server. Our app was originally written for Access using the VS designer to generate the queries. After testing these queries, we found that if the ` char was removed and replaced by [ ] brackets, then all the queries would work in both db's.
Title: developer   
Name: john
Date: 2009-08-26 12:46:38 PM
Comment:
We have a DataSet with Table Adapters built for MSAccess using OLEDB. We are using Visual Studio 2005 and .Net 2.0.

When the designer creates insert, update and delete queries it encloses the table name and field names with a ` (ASCII char 96).
For example:

INSERT INTO `table` (`col1`, `col2`, `col3`) values (....)

Is there a reason for this character? Assuming it's for escape purposes, is there a setting to make Visual Studio use another character, eg "[" or "]"?
Title: sir   
Name: jim
Date: 2008-12-15 8:18:50 PM
Comment:
where is the method SetUnitPriceNull found - not in this sample

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 2:20:47 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search