(LocalVariable)ABC.string(Name) = (IDataReader)dataReader.GetString(0);
This name value is coming from database.
What happening here is if this name 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 = dataReader.GetValue(0);
if (abc == null)
//assigning null
else
//assigning abc value
Is there something like can we write extension method for this?
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
stringwhich is a class, anullwill be returned. If the field wasint, then0would be returned. Additionally, if you are expecting anint?, say from an nullable int field,nullwould be returned.Specific Usage for Kumar’s case:
General Usage
or
or
Extension
from http://skysanders.net/subtext/archive/2010/03/02/generic-nullsafe-idatarecord-field-getter.aspx