Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is standard way to handle Null dates in .NET

I have asp.net form with C#, where is I am taking user information to insert in the database as usual by using Linq. well. Where as I am taking Date of birth also from the user, but if user skip to fill date text box from ui, then I am getting date like '01/01/0001' something like this, which certainly database security would not allow to store it.

So I need to check somewhere in my code that it is null or in this (above given) format. If it is null or in format '01/01/0001' then what exactly I have to do? I don't have any default value for dates.

So what is the standard way to handle if date is null (but not mandatory).Please guide me. So many times I found myself in trap while handling null for various types.

Edited see what i did seems it working here. but i don't think so this is standard way:

DateTime? otxtDOB = new DateTime();
                    if (!string.IsNullOrEmpty(DOB))
                    {
                    if (Convert.ToDateTime(DOB) != DateTime.MinValue)
                        {
                        otxtDateOfPurchese = Convert.ToDateTime(Convert.ToDateTime(DOB).ToString("dd-MMM-yyyy"));
                        }
                    else
                    {
                     otxtDOB = null;
                        }

                    }

Please confirm me is this right way ?

like image 644
Red Swan Avatar asked Mar 15 '11 09:03

Red Swan


People also ask

How do you handle null values in a date?

One is 1/1/1900 this one looks to cover NULL values. Then they have 1/1/9999, this is used to fill in an "Expired" column. The 1/1/9999 appears to cover the "Not Expired" date. The current "valid" dates run from 1/1/2000 through 12/31/2030.

How do you handle a null check in C#?

IsNullOrEmpty() Method of C# This method is used when you want to check whether the given string is Empty or have a null value or not? If any string is not assigned any value, then it will have Null value. The symbol of assigning Null value is “ “or String. Empty(A constant for empty strings).

How do I pass a date as null in C#?

C# Nullable Type By default DateTime is not nullable because it is a Value Type, using the nullable operator introduced in C# 2, you can achieve this. Using a question mark (?) after the type or using the generic style Nullable.

What is the null value for DateTime in C#?

Assigning null Value to DateTime in C# The DateTime is not nullable because, by default, it is a value type. The value type is a form of data stored in its memory allocation.


1 Answers

Making the date property Nullable (i.e. a "DateTime?") should allow it to actually be null if the user hasn't set it. (And provided your database column will allow nulls, it can be stored as null in the database)

Otherwise it's going to default to DateTime.MinValue which is what you're seeing here. And you'll have to explicity test for DateTime.MinValue when adding to the database.

like image 123
Massif Avatar answered Nov 14 '22 22:11

Massif