1

I have a table with a few fields, one of them is a Double type field which can contains null values...

Using ADO and SQLDATAReader I recover that field in a variable. I defined this variable as a: Double, Double?, double, double?... and I got the value (coming from de SQLDataReader) using GetValue (and doing a cast) or using a GetDouble... each one is crashing when the value is null.

The only this is working is defining this variable as a object, but I dont want it. Thinking in advance could be hard times handle this type in my project...

Quote: I have to differentiate the case when this value is 0 or null...

Any idea guys?

Edited:

Object.DoubleValue= (Double?)Datos.GetDouble(1);
Object.doubleValue= (double?)Datos.GetDouble(1);

Not working.

Object.ObjectValue= Datos.GetValue(1);

Working.

Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60

1 Answers1

3

Unfortunately there's no out of the box method. But you could tweak it with an extension method like this:

(be aware its just a rough prototype that works in your case, but probably needs some checks and constraints etc)

public static class Helpers
{
    public static T GetSmartValue<T>(this SqlDataReader r, int ordinal)
    {
        dynamic value = r.GetValue(ordinal);
        if (value == DBNull.Value)
        {
            value = null;
            return value;
        }

        return (T) value;
    }
}

then in your code

var x = yourReader.GetSmartValue<double?>(1);
gsharp
  • 27,557
  • 22
  • 88
  • 134
  • Finally I desist... I will use your method gsharp. Quite elegant... Thanks Mate... – Kenzo_Gilead Jul 07 '16 at 13:18
  • welcome. however i don't know what kind of application you are writing, but i use plain ado.net mostly only for quick n dirty stuff, for other i rely on dapper or EF. makes life easier :) – gsharp Jul 07 '16 at 13:34
  • Hard to explain... but I need to use it... Thanks again gsharp... :) – Kenzo_Gilead Jul 07 '16 at 13:37
  • yeah sometimes we can't pick our favorite tools .. been there too ;-) have a good weekend @EliasMP – gsharp Jul 08 '16 at 12:38
  • You as well... Cheers @gsharp :) – Kenzo_Gilead Jul 08 '16 at 16:34
  • What do you think about my first approach @gsharp? Defining a class with object attributes, and recovering the values from the DB as an object. I can paint them (in the view) directly without any cast... – Kenzo_Gilead Jul 09 '16 at 07:20
  • 1
    @EliasMP well better to cast than having object in my opinion. however that always depends on what you want to do. as i don't have the full picture of your project it's difficult to say. – gsharp Jul 11 '16 at 06:26
  • I haven´t all picture as well... LOL. Kind of using Devexpress, ADO, painting data, MVC, Entity,... Just asking, finally I used your method... Elegant... Cool!! Cheers mate... – Kenzo_Gilead Jul 11 '16 at 07:12
  • nice solution, one optimization: return null in case of DBNull instead of return DBNull.Value – swe Sep 14 '16 at 09:16
  • @gsharp, sry, yes... value=null;return value; ok, i got that... one line less: return null; :) – swe Sep 14 '16 at 11:48
  • 1
    @swe return null doesn't work as well since it's not defined that T must be a class ;-) I'm assigning null to value because it's dynamic. – gsharp Sep 14 '16 at 13:31