开发者

Can a strongly-typed DataColumn that is set to AllowDBNulls=False ever have a DBNull for its value?

开发者 https://www.devze.com 2023-01-25 09:07 出处:网络
Background information first: We are using a strongly-typed DataSet to manage transaction files. That is to say, the DataSet does not use a database of any kind as its backing store; instead the Data

Background information first:

We are using a strongly-typed DataSet to manage transaction files. That is to say, the DataSet does not use a database of any kind as its backing store; instead the DataSet is used to read and write XML files to disk. The WriteXml(fileName As String) is used to output specific rows of data, and all dependant rows from other tables are automatically embedded in the output.

The ReadXml method is used to load these files at startup time.

Originally, the classes for this were generated with a code-gen tool in VB.NET. However, since this system was put in place, many manual modifications have been made. i.e. regenerating the classes is not really an option at this point (Boo and hiss all you like; I didn't do it).

A customer who uses our system in the field has run accross an odd exception; one that we can't readily reproduce. Basically, a case occurred where the system was trying to read the value of a transaction already in the DataSet, and it resulted in an "InvalidCastException".

开发者_开发百科(BTW: Our system runs on a streamlined WinCE system, using the Compact Framework v3.5; since it's bare-bones, the CAB file that gives exception message is not installed, but given its context, I can make a good guess...)

If I understand correctly, this should happen when a strongly-typed property is accessed from the DataSet that has a DBNull value.

If this was all there was to it, I wouldn't be writing this.

However, the column is question has its "AllowDBNulls" property set to False. I've checked and re-checked this, and confirmed that it is being set to False.

So... shouldn't this be impossible? How can an InvalidCastException occur when this property is set as False?

Here's some code-snippets do show what we're doing to setup the column:

Private Sub InitClass()
    'Snip...'
    Me.columnTransactionMode = DatasetUtilities.addColumn(Me.Columns, "transactionMode", GetType(System.Int32), False, False)
    'Snip...'
End Sub

The DatasetUtilities.addColumn method:

Friend Shared Function addColumn(ByRef columns As Data.DataColumnCollection, _
                                    ByRef columnName As String, ByRef type As Type, _
                                    ByRef allowDBNull As Boolean, ByRef isUnique As Boolean) As Data.DataColumn
    Dim column As Data.DataColumn

    column = New Data.DataColumn(columnName, type, Nothing, System.Data.MappingType.Attribute)
    columns.Add(column)
    column.AllowDBNull = allowDBNull
    column.Unique = isUnique

    Return column
End Function

The transactionMode property on the CustomerTransactionRow class:

Public Property transactionMode() As Integer
    Get
        Return CType(Me(Me.tableCustomerTransaction.transactionModeColumn), Integer)
    End Get
    Set(ByVal Value As Integer)
        Me(Me.tableCustomerTransaction.transactionModeColumn) = Value
    End Set
End Property

This is the first and only report of this issue that we've had to-date. I'm going to protect against this happening again as best as I can with exception handling. However, I'd feel a lot better if I knew exactly why the issue occurred.

Thanks for your time, all!


Try setting a default value for any columns that don't allow NULLs. I recently had a similar situation where AllowDBNull was set to false but columns in new rows still contained NULL if no default was specified. AFAIK this is normal behavior. In my case setting the default value or explicitly setting the value in any new rows solved the problem.

Cheers! JE

0

精彩评论

暂无评论...
验证码 换一张
取 消