views:

226

answers:

2

Hi all,

(LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0);

this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception?

I am manually doing some if condition here. I don't want to write a manual condition to check all my variables..

I am doing something like this now..

string abc =  (Idatareader)datareader.GetValue(0);
if(abc = null)
    //assiging null
else
    assiging abc value

is there something like can we write extension method for this? thanks

+1  A: 

I'd use something like this:

string abc = (IDataReader)datareader.GetValue(0) ?? "Default";
Jaxidian
what its going to do? can you explain me thanks
kumar
"The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand." http://msdn.microsoft.com/en-us/library/ms173224.aspx
R. Bemrose
have you tested this because `DbNull != null`
Sky Sanders
@Sky: There was no requirement to check for DbNull - the code this is replacing only checks for null. If I wrote it to check for DbNull, then I would be changing the behavior of the code and I did not want to do such a thing.
Jaxidian
Do you expect that an IDataReader is going to contain a `null`? And the requirement was for an Extension Method. But my question was: Have you tested the code you provided? It seems wonky to me.
Sky Sanders
@Sky: No. It does not even compile simply because the code that @kumar posted explaining what he is currently doing does not compile. Both he (and I) are effectively saying `string foo = (object)bar;` which will not compile. Furthermore, I did not even claim that it DID compile. The idea here is for me to convey an idea, not for me to do all of his work for him. I attempted to convey the idea using the same means of communication that he used to convey his question - I thought it would be easier to apply. And the way I did it does not change the behavior of code in any way. Why the hate???
Jaxidian
no hate. I am just pointing out that your answer represents neither an understanding of the underlying issue nor a working solution. Aside from that, testing GetValue against a null is just silly. If presenting a valid answer that demonstrates an understanding of the problem, whether explicitly stated or not, is more work than you want to do then perhaps not answering is the better choice. OP does not have a clear understanding of the problem domain, it is the job of the responder to remedy that, not compound it with similar junk code. peace.
Sky Sanders
+1  A: 

Here is a couple extension methods that will nicely wrap up all of your concerns around retrieving strongly typed values from a data reader. If the value is DbNull the default of the type will be returned. In the case of string which is a class, a null will be returned. If the field was int, then 0 would be returned. Additionally, if you are expecting an int?, say from an nullable int field, null would be returned.

Specific Usage for Kumar's case:

string abc = datareader.GetValueOrDefault<string>(0);

General Usage

var name = GetValueOrDefault<string>(reader, "Name");

or

var name = reader.GetValueOrDefault<string>("Name");

or

var name = reader.GetValueOrDefault<string>(0);

Extension

public static class NullSafeGetter
{
   public static T GetValueOrDefault<T>(this IDataRecord row, string fieldName)
   {
       int ordinal = row.GetOrdinal(fieldName);
       return row.GetValueOrDefault<T>(ordinal);
   }

   public static T GetValueOrDefault<T>(this IDataRecord row, int ordinal)
   {
       return (T)(row.IsDBNull(ordinal) ? default(T) : row.GetValue(ordinal));
   }
}

from http://skysanders.net/subtext/archive/2010/03/02/generic-nullsafe-idatarecord-field-getter.aspx

Sky Sanders