ADO.NET convert NULL to default value?

  • Thread starter Thread starter Chris Morris
  • Start date Start date
C

Chris Morris

I'm working on a J++ conversion, and with the older ADO and the Java
WFC, we have methods like GetInt and GetString on Field classes that, by
default, convert NULLs to a default value.

J# -> .NET forces us into ADO.NET, and suddenly all these calls to Field
classes explode with a ClassCastException if the value is NULL -- no
conversion to a default value occurs.

I've mentioned this to a couple of people on weird occasions, and both
have mumbled something about being able to set something to change this
behavior. Is there such an option in ADO.NET?
 
Miha said:
There is no recordset anymore in ado.net....

Well, then I don't know. Using J#, there are .NET assembly versions of
the WFC stuff that talks to ADO. At some point internally J# hooks up
these Javaish WFC ADO classes to ADO.NET, and I don't know what it's using.

Regardless, bigger question, is there a way to 'turn on' behavior that
auto defaults NULL values to 0, "", false, etc... depending on the data
type? If so, just point me to that and I can work it backwards into J#.
 
Hi

There is a way to do what you want if you use a strongly typed dataset. Then
you access the fields through properties on the datarow object.
These properties throws an exception by default if they encounter a DbNull
value in a field. If you modify these properties to instead return 0, empty
string or whatever you want for a null value this will do the trick. (You
can find the properties in the DatasetName.cs file)
Its not the most elegant of ways but it will do what i think you want.

Tor Arne Gjelhus
 
Tor said:
Hi

There is a way to do what you want if you use a strongly typed dataset. Then
you access the fields through properties on the datarow object.
These properties throws an exception by default if they encounter a DbNull
value in a field. If you modify these properties to instead return 0, empty
string or whatever you want for a null value this will do the trick. (You
can find the properties in the DatasetName.cs file)
Its not the most elegant of ways but it will do what i think you want.

So you're saying build a wrapper (delegate) object around the dataset to
intervene and handle this stuff. I've already got this as an option in
my existing codebase, I was just wondering if there was a one-time
switch I could throw to alter this behavior as-is in ADO.NET -- I'd
heard some other people mention that they thought this was an option,
but I guess not.

Thx for the feedback, though.
 
Back
Top