View previous topic :: View next topic |
Author |
Message |
gregoryagu
Joined: 12 Dec 2007 Posts: 78
|
Posted: Wed Feb 04, 2009 9:28 pm Post subject: SQLDBTypes |
|
|
I am loading a tree by using ADO.net, and returning a DataSet. I open the table and loop through the rows. In each row, I use code that looks like this:
String description = (string) row[0]["description'];
The issue is when the description is null. In looking into the issue on MSDN, it says to use the SqlString structure as it can handle null values.
So I changed the code to this:
SqlString description = (SqlString) row[0]["description'];
The new issue is that the field is of type string, not of type SqlString in the dataset. I know this by looking in the debugger under the column properties.
It seems that the data types of the DataTable would be of the Sql types, but this does not appear to be the case. All of the columns are using the CLR types, not the SQL data types. Do you have any idea what is up with that? I have searched and not been able to find anything on this.
My workaround is to skip using the SqlString type, and simply check for a null value before doing the conversion. Works, but its a bit kludgy. Do you know of a better way?
Thanks,
Greg |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Thu Feb 05, 2009 5:46 am Post subject: |
|
|
Is this a Virtual Tree issue - or an issue with Datasets? My understanding of datasets is that for each field the DataRow stores an object. If the database entry is not null then the objects is the typed value (eg int, string) however if there is a null database entry for the row then the field value stored is actually DbNull. This can cause the type of problem you are experiencing. To avoid this you can create a function to handle the issue like:
Code: |
void GetDbString(object value)
{
return (value is DbNull) ? null : (string) value;
}
// Then call it like
string description = GetDbString(row[0]["description"]);
|
If you are using typed datasets then you can set the NullValue for string fields to be (Null) and then the DataSet designer will automatically generate nice wrapper code like this:
Code: | public string ShipAddress {
get {
if (this.IsShipAddressNull()) {
return null;
}
else {
return ((string)(this[this.tableOrders.ShipAddressColumn]));
}
}
set {
this[this.tableOrders.ShipAddressColumn] = value;
}
} |
_________________ Infralution Support |
|
Back to top |
|
|
gregoryagu
Joined: 12 Dec 2007 Posts: 78
|
Posted: Wed Feb 11, 2009 5:25 pm Post subject: |
|
|
Thanks very much. This was a Dataset issue, not a Virtual Tree issue. (I knew you would have an intelligent answer!) |
|
Back to top |
|
|
|