I just had to find a work-around for this. I needed to change some old code for a web page, written for ASP.NET 2.0. The page uses Telerik RadMenu, a menu control. This component requires the root elements to have proper DBNull-values (for parentIDs).
So, when I compiled the old code, the RadMenu component gave me lots of problems. First exceptions regarding Constraints, then it did not understand which elements were root elements and the whole thing looked horrible.
But I solved it and this is what worked for me.
In the Properties page of the ParentID column in the Table Adapter designer, I used:
- AllowDBNull: true
- DefaultValue: -1 (-1 is a value that does not occur normally for that column)
THe NullValue-property stayed at "Throw Exception" as it was impossible to change for me.
And in the code using the values from the table adapter, I used this construct (VB.NET code, not C#, as this question is tagged):
Dim MenuBarTable As DAL.Page.MenuBarDataTable 'The Table Adapter
MenuBarTable = PageObj.GetMenuBar() 'The generated Get function
MenuBarTable.ParentIDColumn.AllowDBNull = True
For Each row As Page.MenuBarRow In MenuBarTable.Rows
If row.IsParentIDNull() Then
row.SetParentIDNull()
End If
Next
The code generated for the table adapters generates two functions for each column that should allow DBNULLs. They are meant be used when dealing with NULLs, but it is a clumsy solution by Microsoft. What happens behind the scenes is that the the table adapter will output the column's DefaultValue instead of NULL from the Get-function. I call that a "simulated NULL" or a "fake NULL".
The function IsParentIDNull() will actually check if the row contains this "fake NULL", e.g. the column's DefaultValue, and when it does, I'm inserting a proper DBNull using the SetParentIDNull() function.
This solution works for me, but is not very elegant, nor is it very efficient, but it could be of help to someone else, I hope.