Change Default value to return nulls

Hi

I have this code, the issue I have is when I get the data from SQL and if the fields are NULL it goes into this

return default(T);

so for a *date* I get

01/01/0001 00:00:00

for a *int* I get

0

Is it possible to change this so that when it does go to

if (reader[columnName] == DBNull.Value)

it returns a null?

thanks

public static T GetValue<T>(this IDataReader reader, string columnName)
        {
			int colNumber = reader.GetOrdinal(columnName);
			if (reader[columnName] == DBNull.Value)
			{
				// Returns the default of this type if dbnull
				return default(T); 
			}
			else
			{
				return (T)reader.GetValue(colNumber);
			}
        }

Open in new window

mousemat24Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
may be I didn't get the question fully. why can't you return a null there? are you getting any error
eg.
public static T GetValue<T>(this IDataReader reader, string columnName)
        {
                        int colNumber = reader.GetOrdinal(columnName);
                        if (reader[columnName] == DBNull.Value)
                        {
                                // Returns the default of this type if dbnull
                                return null; 
                        }
                        else
                        {
                                return (T)reader.GetValue(colNumber);
                        }
        }

Open in new window

0
mousemat24Author Commented:
i get this error

Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)'

when I put null.
0
Roshan DavisCommented:
can you check this link?
http://stackoverflow.com/questions/302096/how-can-i-return-null-from-a-generic-method-in-c
specially the part

where T : class, IDisposable

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Roshan DavisCommented:
or try this also
public static Nullable<T> GetValue<T>(this IDataReader reader, string columnName)
        {
                        int colNumber = reader.GetOrdinal(columnName);
                        if (reader[columnName] == DBNull.Value)
                        {
                                // Returns the default of this type if dbnull
                                return null; 
                        }
                        else
                        {
                                return (T)reader.GetValue(colNumber);
                        }
        }

Open in new window

0
mousemat24Author Commented:
when I try this:

public static Nullable<T> GetValue<T>(this IDataReader reader, string columnName)
{
}

I get

The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

When I try this


public static T GetValue<T>(this IDataReader reader, string columnName) where T : class, IDisposable
{
}

... where I'm using it i.e.

int ID = reader.GetValue<int>("ID");

I get errors there

The type 'int' cannot be used as type parameter 'T' in the generic type or method 'Test.DataReaderExtensions.GetValue<T>(System.Data.IDataReader, string)'. There is no boxing conversion from 'int' to 'System.IDisposable'.


thanks
0
mousemat24Author Commented:
I get the same error for a DateTime as well:

DateTime dates = reader.GetValue<DateTime>("dates");
0
Roshan DavisCommented:
oh.

can you try this

public static T GetValue<T>(this IDataReader reader, string columnName) where T ; struct

Open in new window

0
Roshan DavisCommented:
one workaround is, can you return a struct that will have 2 members (one is T and other a bool saying null or not null).

generic and nullable can lead more in to confusion/complications
http://blogs.msdn.com/abhinaba/archive/2005/12/11/501544.aspx
0
Roshan DavisCommented:
or return T as out parameter and return bool-false for null.
0
mousemat24Author Commented:
when I do

public static T GetValue<T>(this IDataReader reader, string columnName) where T ; struct

I get loots of errors in the method

I get an error on the 1st brace ({) Identifier expected
I get an error in the if statement - Invalid token 'if' in class, struct, or interface member declaration      

and the list goes on
0
mousemat24Author Commented:
roshmon, do you think you can help me with creating a method that meets this? or return T as out parameter and return bool-false for null.

and also how to run it i.e.

DateTime dates = reader.GetValue<DateTime>("dates"); or

Int number = reader.GetValue<Int>("number");

String name = reader.GetValue<String>("name");
thankyou
0
mousemat24Author Commented:
I just tried this

myDate is set to null in the IF statement, but when I assign it i.e.

book.Date = myDate;

I get this error:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

when I try to phase it it puts in the default date

i.e. 01/01/0001 00:00:00
DateTime? myDate;
if (reader.IsDBNull(reader.GetOrdinal("DATE")))
  myDate = null;
else
  myDate = Convert.ToDateTime(reader["DATE"]);

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.