Hi Jon (and Cor),
This problem may only exist with strongly typed datasets but here's the
demo. I've included both the dataset and demo code. You should find that
the first call to SafeDbText works fine but that you can't even step into
the second call of SafeDbText - the debugger goes straight to its error
page. (Not very helpful!) This is despite the Try block I put in
specifically to try and intercept the cast error.
Julian
------ Code -----------------------------------------
Module Module1
Public Sub DemoSafeDbText()
Dim myDs As New dsDemo
Dim nr As dsDemo.Table1Row = myDs.Table1.NewRow
nr.Field1 = "Some text"
nr.SetField2Null()
Dim SafeText As String = SafeDbText(nr.Field1)
SafeText = SafeDbText(nr.Field2)
End Sub
Public Function SafeDbText(ByVal Value) As String
Try
If IsDBNull(Value) Then
Return ""
Else
Return CStr(Value)
End If
Catch ex As Exception
Return ""
End Try
End Function
End Module
------ Dataset ---------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="dsDemo" targetNamespace="
http://tempuri.org/dsDemo.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="
http://tempuri.org/dsDemo.xsd"
xmlns:mstns="
http://tempuri.org/dsDemo.xsd"
xmlns:xs="
http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="dsDemo" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="Field1" type="xs:string" minOccurs="0" />
<xs:element name="Field2" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>