Setting NullValue property for typed dataset

 In Whidbey DataSet designer, you are able to set Typed DataColumn's NullValue property to the following values if the DataType is string

1 (Empty)                    

2 (Nothing) (null for C#)

3 (Throw exception)

4 any string you can type in.

The first three options are noticeable in the designer's property browser dropdown.

(Select a DataColumn and press F4 to show the property browser, select the NullValue property)

Apart from the dropdown options, you are allowed to type in anything directly to NullValue's combobox.

 

For example, you can type in NoValue for the NullValue property of Column1 and you will get this code generated in (code behind file XXX.designer.vb)

 

 Public Property Column1() As String

Get

If Me.IsColumn1Null Then

Return "NoValue"

Else

Return CType(Me(Me.tableDataTable1.Column1Column),String)

End If

End Get

Set

Me(Me.tableDataTable1.Column1Column) = value

End Set

End Property

 

 

Nice hum!

 

Unfortunately, users are not able to set values freely for other data types with the dataset designer. Therefore I wrote this FAQ as a workaround for the advanced users to set the NullValues as desired. We will look into this feature again in next version. 

 

Example 1:  If you have a Column, whose DataType is System.Data.DataSet. Now you want to set its NullValue to Nothing.

You can open the dataset file with a text editor (e.g. XmlEditor), find the column's msprop:nullValue and replace the value to msprop:nullValue="_null". If there is no msprop:nullValue attribute for the column, then add msprop:nullValue="_null".

 

The whole column would looks like this:

<xs:element name="Column1" type="..." msprop:nullValue="_null"   .../>

 

Save the DataSet file and verify that there is no build error and the code behind file is generated correctly.

 

Example 2: If you have a Column, whose DataType is Int32 and you want to set its NullValue to -1.

You can open the dataset file with text editor and replace or add msprop:nullValue with  msprop:nullValue="-1"

 

Warning: after you save the dataset file, if you get this error:

Custom tool error: Failed to generate code. Failed to generate code. Exception has been thrown by the target of an invocation.

That means your NullValue is not valid (For example, you set a NullValue to be "ABC" for a Integer type). You should modify the NullValue again to make it a valid value.

 

 

Note that these are the reserved values an you should use them appropriately when editing the dataset file through text editor.

 

msprop:nullValue="_empty"  -- (Empty)

msprop:nullValue="_null" -- (Nothing)

msprop:nullValue="_throw" -- (Throw exception)

 

msprop:nullValue="_throw" the default value and it is not persisted by default.

 

Hope this would help you in some advanced usages.

 

JohnChen (Smart Client Data Dev).