1

How do we read integer null values from SqlDataReader?

CREATE TABLE [dbo].[Mem_Basic] (
  [Id]          INT           IDENTITY (1, 1) NOT NULL,
  [Mem_Email]   VARCHAR (50)  NULL,
  [Mem_DOB]     DATE          NULL,
  [Mem_ResPin]  INT           NULL,
  PRIMARY KEY CLUSTERED ([Id] ASC)
);

For string and integer I use

if (reader.Read() == true)
{
  mb.Mem_Email = reader["Mem_Email"] == System.DBNull.Value ? null : (string)reader["Mem_Email"];
  mb.Mem_ResPin = reader["Mem_ResPin"] as int? ?? default(int);
 }

But how do I read the date in same manner from reader?

//mb.Mem_DOB = (Convert.ToDateTime(reader["Mem_DOB"]));
2
  • i cant't read date value from database of there is know value is there (null value). If there is null i want return null value Commented Sep 2, 2013 at 4:52
  • 3
    If it's a nullable DateTime in the DB, it should be a nullable DateTime (DateTime?) in the data access layer. Commented Sep 2, 2013 at 4:52

4 Answers 4

4
mb.Mem_DOB = reader["Mem_DOB"] == DBNull.Value
                 ? (DateTime?) null
                 : (DateTime) reader["Mem_DOB"];

The mb.Mem_DOB should be declared as a DateTime?

Don't use Convert.ToDateTime. The value is already a DateTime, so just cast it.

Sign up to request clarification or add additional context in comments.

1 Comment

@Matt-shows the error there is no impicit convertion between <null> and System. DateTime
2
DateTime? Mem_DOB = null;

mb.Mem_DOB = reader["Mem_DOB"] == System.DBNull.Value ? null : Convert.ToDateTime(reader["Mem_DOB"]);

8 Comments

Does that compile, or do you need (DateTime?)null?
@Parvathiiiii - what is the type of Mem_DOB?
Mem_DOB is a nullable DateTime member of your class mb and its also the name of a column in your database table.
public DateTime? Mem_DOB { get; set; } - nullable with the question mark:)
@Parvathiiiii DateTime can not hold a null value. DateTime? can. Notice the question mark at the end. But changing that means, everywhere you use it, you need to check for HasValue and then use the Value.
|
1
mb.Mem_DOB = reader["Mem_DOB"] == System.DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["Mem_DOB"]);

2 Comments

SQLDateTime for 1753, not DateTime.MinValue as 1/1/0001 as dates before 1753 wont work being reinserted into SQL.
I asume that Mem_DOB is DateTime, not DateTime?. In this case we should read it this way and write to database Null if Mem_DOB == DataTime.MinValue
0

You can check if it is null or not.

int i = reader.GetOrdinal("Mem_DOB")
if( reader.IsDBNull(i))
   mb.Mem_DOB = someDefaultValueforyourdatetime; //assign null of Mem_DOB is nullable
else
   mb.Mem_DOB = Convert.ToDateTime(reader(i));

Though instead of using Convert.ToDateTime directly, i would recommend using DateTime.TryParse or DateTime.TryParseExact.

1 Comment

That will only work if Mem_DOB is a nullable DateTime? field of your class....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.