An unhandled exception of type ‘System.Data.StrongTypingException’ occurred in exe

This is an error given in the dataset (xsd file) when trying to return a null value for a column. By default, in DataSets and typed DataSets, when you try and access a value in a row that is null in the database, following exception is thrown.  The error message appears as:

An unhandled exception of type ‘System.Data.StrongTypingException’ occurred in .exe

Additional information: Cannot get value because it is DBNull.

Fix:

To fix this exception, within each field in a typed DataSet, you can specify a nullValue annotation to tell the typed DataSet how to react when an underlying field is DbNull. To do this follow given steps:

Steps

  1. In the DataSet1.xsd, add the namespace msprop in the namespace declaration. Paste the following line of code for the msprop namespace

xmlns:msprop=”urn:schemas-microsoft-com:xml-msprop”

immediately after thetargetNamespace value as shown below:

<xs:schema id="DataSet1" 
targetNamespace="http://tempuri.org/DataSet1.xsd" 
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" 
xmlns:mstns="http://tempuri.org/DataSet1.xsd" 
xmlns="http://tempuri.org/DataSet1.xsd" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" 
attributeFormDefault="qualified" elementFormDefault="qualified">
  1. Modify the element  to reflect the following code sample:
<xs:element name="TelephoneCode" msprop:nullValue="0" 
 type="xs:int" minOccurs="0" />
  1. Save the DataSet1.xsd file. If you are prompted to reload the file, click Yes.

The possible values for msprop:nullValue are:

Value Behavior
_throw Throw an exception. (This is what happens when you do not specify an annotation.)
_null Returns a null reference if the field type is a reference type, or throws an exception if the field is a value type (e.g. strings return null, ints throw an exception.)
_empty Returns String.Empty for strings, returns an object from an empty constructor from all other reference types. Still throws an exception if the field is a value type.
Replacement Value Specifies a default value to be returned when the type is null. The replacement must be compatible with type (e.g. nullValue=”0″ for an int, butnullValue=”Hi There” for a string.)

Depending on field’s data type you can use these annotations as shown below:

string
1.  <xs:element name="EmployeeName" msprop:nullValue="" 
     type="xs:string" minOccurs="0">
2.  <xs:element name="Address" msprop:nullValue="_empty" 
     type="xs:string" minOccurs="0">
 
int / decimal 
3.  <xs:element name="TelephoneCode" msprop:nullValue="0"
     type="xs:int" minOccurs="0" />
4.  <xs:element name="BasicSalary" msprop:nullValue="0" 
     type="xs:decimal" />
boolean
5.  <xs:element name="IsPermanent" msprop:nullValue="false" 
     type="xs:boolean" minOccurs="0" />
 
datetime
6.  <xs:element name="JoinDate" msprop:nullValue="1980-01-01T00:00:00" 
     type="xs:dateTime" minOccurs="0" />

In above examples ,

a) The first two fields EmplyeeName and Address will return empty strings when a DbNull is encountered.

b) The next two fields TelephoneCode and BasicSalary defaults the field to zero

c) The fifth field IsPermanent defaults to false

d) The last field JoinDate defaults to January 1st, 1980.

Using msprop annotation we can control the way the nulls are handled by the dataset.

 

– Mehul Kothari

www.radical-it-solutions.com

Leave a comment