views:

28

answers:

2

I have a string which can be empty if its not empty it is containing a xml document. The problem is null values are allowed in this dataset column of DataType System.String.

Error Message:this.MetaData' threw an exception of type 'System.Data.StrongTypingException'

base {System.SystemException} = {"The value for column 'MyData' in table 'GMyTAbleName' is DBNull."}

UPDATE

here is a screenshot of the cause:

http://666kb.com/i/bld3eelnaicsgb9tv.png

you see how it tries to convert NULL to a string which should be returned.

That code is from the DataSet.Designer.cs file, how could I change that behaviour? :S

A: 

Consider testing the column first using the IsNull method on the DataRow.

I've never did like DBNull. :)

kbrimington
even if I would test that, there is alreay such a test in the dataset, but when its Null and its allowed to be null, the problem is the dataset can not convert the null to empty string, thats what the error said.
msfanboy
@msfanboy: I take your point. The idea is that you protect yourself from accessing the strongly-typed column until you are certain it is not null, because you know you'll get a StrongTypingException if you ever access the getter when its null. It's tedious, I know; avoiding DBNull is part of why I adopted LINQ so darn fast.
kbrimington
@kbrimingtonthx for your cheers up I like Linq too, but can`t use it here :/I updated the first post with a screenshot of that problem!
msfanboy
A: 

ok I made it working. I will list the steps here:

1.) http://msdn.microsoft.com/en-us/library/ya91ataz(vs.71).aspx

choose this: (Empty) To have null values return as String.Empty.

2.) As we now a string.empty is not a valid xml-string I checked this in the getter of my appropriate property.

private XmlDocument XMLMyData
 {
    get
    {
        XmlDocument doc = new XmlDocument();    

        if (this.MyData.Trim().Length > 0) 
            doc.LoadXml(this.MyData); // return xml-string in the xml document
        else if (String.IsNullOrEmpty(this.MyData)) 
            return doc;             // return empty xml document

        return doc;                                     
    }
}
msfanboy