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?
 
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
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.