views:

767

answers:

13

Is there a better/cleaner way to do this?

int stockvalue = 0;
if (!Convert.IsDBNull(reader["StockValue"]))
    stockvalue = (int)reader["StockValue"];
+4  A: 

Yes you can use int? This way you can have a default value of null instead of 0. Since the result of stockvalue could potentially be 0 there isn't confusion as to whether the database was 0 or null. For instance like this (pre nullable) we had a default initialization of -1 to represent no value was assigned. Personally, I thought this was a little dangerous because if you forget to set it to -1, there is a data corruption issue that can be really difficult to track down.

http://msdn.microsoft.com/en-us/library/2cf62fcy(VS.80).aspx

int? stockvalue = null;

if (!Convert.IsDBNull(reader["StockValue"]))
    stockvalue = (int)reader["StockValue"];

//Then you can check 

if(stockValue.HasValue)
{
  // do something here.
}
Kevin
+1 for the correct data type to use. However, IIRC, `DBNull` still has to be converted to `null` with a bit of checking.
Jon Seigel
use int? how? Simply casting reader["StockValue"] to (int?) will throw an exception when reader["StockValue"] is DBNull
Philippe Leybaert
you can use int? so that you don't have to use a default initialization to 0.
Kevin
A: 

use the Nullable<int> type...int? for short

Rich
A: 

Here's one way.

int stockvalue = Convert.IsDbNull(reader["StockValue"]) ? 0 : (int)reader["StockValue"];

You could also use TryParse

int stockvalue = 0
Int32.TryParse(reader["StockValue"].ToString(), out stockvalue);

Let us know which way works for you

masenkablast
I don't mind the down votes, just please leave a comment saying how I could improve this answer.
masenkablast
There is definitely no reason for a DV here as your answer is a correct solution.
Chris Marisic
Yeah, nothing wrong with it. I've up voted you to balance it out. :-)
Brian Scott
I didn't downvote, but it's a very inefficient piece of code. Your first line calls the reader[] indexer twice. The second one uses very inefficient string conversions, which is totally unnecessary.
Philippe Leybaert
Good point, I usually don't use any type of datareader in my applications. Instead opting for an object-based representation of back-end data.
masenkablast
+1  A: 

You could do this conversion directly in your DB-query, thus avoiding the special case alltogether.

But I wouldn't call that 'cleaner', unless you can consistently use that form in your code, since you would lose information by returning '0' instead of NULL from the DB.

lexu
The argument about losing information does make sense. In an database application, the value of "0" is different than NULL. One denotes having no value at all, and one is a value that just happens to be zero.
masenkablast
A: 

Not really. You could encapsulate it in a method:

public int getDBIntValue(object value, int defaultValue) {
  if (!Convert.IsDBNull(value)) {
    return (int)value;
  }
  else {
    return defaultValue;
  }

And call it like this:

stockVaue = getDBIntVaue(reader["StockValue"], 0);

Or you could use coalesce in your query to force the returned value to be non-null.

Edit - corrected dumb code errors based on comments received.

Ray
And the downvotes are because... ?
Ray
@Ray: look at your code, and you'll downvote yourself.
John Saunders
(I didn't downvote you, but) For one, you passed in reader["StockValue"] in the "value" parameter, but then ignored the "value" parameter inside the function and used "reader["StockValue"]" instead, so it won't compile.
Adam V
@Ray: Your `getDBIntValue` will always attempt to extract an `int` from `reader["StockValue"]`, regardless of what's passed in. You also have the keyword `default` as one of your parameters, which won't compile (you'd have to make it `@default`, or -- better -- just change the name).
Dan Tao
@John - I corrected a copy-and-paste error which I made. However, the "if you don't know I'm not going to tell you" thing doesn't help me (or anyone else reading these comments) to learn.
Ray
(I didn't downvote you either, though.)
Dan Tao
@Ray just make sure to send us your resume so we can not hire you for any future projects
masenkablast
@masenkablast - I didn't downvote you - what makes you think I did? I don't think I have ever downvoted any answer on SO.
Ray
@Dan: presumably he realizes it'll always get an int, hence the name getDB*Int*Vaue (btw, Ray, please add the missing 'l'. Misspellings bother me).
Adam V
This will teach me to just type and post without checking my code- sorry for the dumb errors - I think it is fixed now.
Ray
@adam - the spelling is fixed - my 'L' key is sticky today. And yes, you have to know you are getting an int. If one were to use these types of utility methods, you would have getDBString, getDBByte, etc.
Ray
@Adam: Originally (before he fixed the code) the method called `Convert.IsDBNull(reader["StockValue"])` regardless of the parameter passed. So it would *always* look at `reader["StockValue"]`, not any other column in any other table. That was what I was pointing out--not that it returns an `int` (which you're correct in pointing out should be obvious).
Dan Tao
+6  A: 
int? stockvalue = (int?)(!Convert.IsDBNull(result) ? result : null);

One possible solution so that you ensure that the DBNull carries across to your code. For our group, as a best practice, we try and not allow NULL columns in the database unless its really needed. There is more overhead in coding to handle it, and sometimes just rethinking the problem makes it so its not required.

Digicoder
+7  A: 

The way I handle this is

int? stockvalue = reader["StockValue"] as int?;

Very simple, clean and one line. If for some reason I absolutely can't have a null value (which I find poor reasoning for usually since I'd rather know if a value has meaning or if it was unitialized for a primitive type) I would do:

int stockvalue = (reader["StockValue"] as int?).GetValueOrDefault(-1);
Chris Marisic
@Will thanks I haven't had my coffee yet this morning!
Chris Marisic
A: 

While it's convenient to reference reader["StockValue"], it's not very efficient. It's also not strongly-typed, as it returns type object.

Instead, within your code, do something like this:

int stockValueOrdinal = reader.GetOrdinal("StockValue");
int? stockValue = reader.IsDbNull(stockValueOrdinal) ?
    null : 
    reader.GetInt32(stockValueOrdinal);

Of course, it's best to get all of the ordinals at one time, then use them throughout the code.

John Saunders
@John: in your comment on my answer you asked me "what if the column changed to double?". The code you provided here will throw an exception if the column holds a value which is not of type int. (also, minor detail: there's not .GetInt() method on a data reader. It should be .GetInt32())
Philippe Leybaert
@Philippe: throwing an exception is _precisely_ what the code should do if the column type changes and the code does not. Returning zero when something serious is wrong is a very bad idea.
John Saunders
@Philippe: thanks for catching the typo.
John Saunders
+4  A: 

The shortest (IMHO) is:

int stockvalue = (reader["StockValue"] as int?) ?? 0;

Explanation:

  • If reader["StockValue"] is of type int, the value will be returned, and the "??" operator will return the result
  • If reader["StockValue"] is NOT of type int (e.g. DBNull), null will be returned, and the "??" operator will return the value 0 (zero).
Philippe Leybaert
@Philippe: what happens if the type of the column is changed to Double or something?
John Saunders
+1 - This is the way I'd write the code, although if a null value is valid there is no need for the default; `int? stockvalue = reader["StockValue"] as int?`
Stevo3000
@John: it would return 0. But no matter what solution you pick, if a column changes data type, you're screwed, unless you throw in a call to Convert.ToInt32() or something.
Philippe Leybaert
A: 

I have two following extension methods in my project:

    public static T GetValueSafe<T>(this IDataReader dataReader, string columnName, Func<IDataReader, int, T> valueExtractor)
        where T : class 
    {
        T value;
        if (dataReader.TryGetValueSafe(columnName, valueExtractor, out value))
        {
            return value;
        }

        return null;
    }

    public static bool TryGetValueSafe<T>(this IDataReader dataReader, string columnName, Func<IDataReader, int, T> valueExtractor, out T value)
    {
        int ordinal = dataReader.GetOrdinal(columnName);

        if (!dataReader.IsDBNull(ordinal))
        {
            // Get value.
            value = valueExtractor.Invoke(dataReader, ordinal);

            return true;
        }

        value = default(T);
        return false;
    }

The usage can be like this:

string companyName = dataReader.GetValueSafe("CompanyName", (reader, ordinal) => reader.GetString(ordinal));
Andrew Bezzub
Interesting but very wordy, I created a class similar to this when I worked with DataReaders extensively but made it much more concise that it operated as `reader.Get<int>("ReaderField")` and worked using the `ChangeType` method
Chris Marisic
Good idea +1, but I think my implementation would be a bit faster.
Andrew Bezzub
+1  A: 

I wrote an extension method several days ago. By using it you could just do:

int? stockvalue = reader.GetValue<int?>("StockValue");

Here's the extension method (modify to fit your needs):

public static class ReaderHelper
{
    public static bool IsNullableType(Type valueType)
    {
        return (valueType.IsGenericType &&
            valueType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
    }

    public static T GetValue<T>(this IDataReader reader, string columnName)
    {
        object value = reader[columnName];
        Type valueType = typeof(T);
        if (value != DBNull.Value)
        {
            if (!IsNullableType(valueType))
            {
                return (T)Convert.ChangeType(value, valueType);
            }
            else
            {
                NullableConverter nc = new NullableConverter(valueType);
                return (T)Convert.ChangeType(value, nc.UnderlyingType);
            }
        }
        return default(T);
    }
}
Vivek
A: 
int? stockValue = reader["StockValue"] == null || reader["StockValue"] == DBNull.Value ? null : (int?)reader["StockValue"];
Will Marcouiller
A: 

int stockvalue = reader["StockValue"] != DbNull.Value ? Convert.ToInt32(reader["StockValue"]) : 0;

amin10043