views:

90

answers:

3

i have a datetime column in sql server and its optional field and if the user decided not to enter then i want to insert the value as NULL in the table and i define something like this:

@deadlineDate datetime = null

when i am inserting into sql server i have this code in asp.net

private DateTime? GetDeadlineDate()
{
    DateTime? getDeadlineDate = null;
    if (!string.IsNullOrEmpty(DeadlineDate.SelectedDate))
    {
       getDeadlineDate = DateTime.Parse(DeadlineDate.SelectedDate).Date;
    }
    if (!getDeadlineDate.HasValue)
    {
        return null;
    }
    return getDeadlineDate.Value;

}

but the problem is: its inserting

1900-01-01 00:00:00.000

in the sql table instead of NULL

what i am doing wrong here?

UPDATE:

private DateTime? GetDeadlineDate()
{
    DateTime? getDeadlineDate = null;
    if (!string.IsNullOrEmpty(DeadlineDate.SelectedDate))
    {
       getDeadlineDate = DateTime.Parse(DeadlineDate.SelectedDate).Date;
    }
    if (!getDeadlineDate.HasValue)
    {
        return DBNull.Value; //throws error....
    }
    return getDeadlineDate.Value;          
}
+5  A: 

You need DBNull.Value rather than null when inserting into SQL server.

When you set DateTime = null in .NET it takes the minimum value of DateTime which is 01-01-0001.

I'd assume you are using a SMALLDATETIME in SQL Server where the minimum value is '01/01/1900'

Chris Diver
Cannot implicitly convert type 'System.DBNull' to 'System.DateTime?'
Abu Hamzah
You will have to post the code performs the `INSERT`, this is where you should use `DBNULL.Value`
Chris Diver
Chris: i have posted the code GetDeadlineDate() is method that grabs the date from textbox.
Abu Hamzah
i have datetime in sql server and i am using sql server 2008
Abu Hamzah
* default value of `DateTime` = `default(DateTime)` = `DateTime.MinValue` = `1900-00-00 00:00:00`
abatishchev
`DateTime.MinValue` = `01-01-0001` http://msdn.microsoft.com/en-us/library/system.datetime.minvalue%28v=VS.100%29.aspx. He is using SQL `DATETIME` as can been seen in the precision of his post which has a minimum value of `01-01-1753` i'm interested where the `01-01-1900` is coming from now, what am I missing? Nisar, please see abatishchev's post on how to use `DBNull.Value` correctly, this however depends how you interact with the database, you cannot return `DBNull.Value` from that function without changing the return type.
Chris Diver
@Chris: Yes, you're right, `default(DateTime)` is 0001 not 1900. Very strange. I'll research more
abatishchev
In fact, `1900-01-01` is a min value for `SMALLDATETIME` http://msdn.microsoft.com/en-us/library/ms182418.aspx probably your column is smalldatetime so when you try to insert a value lower that it's min value, the value becomes equal to the min value
abatishchev
@abatishchev That's what I thought, but he says it is `DATETIME` and in his post it would be `1900-01-01 00:00:00` not `1900-01-01 00:00:00.000`. I've tried with DateTime and I get an SQL overflow error.
Chris Diver
@Chris: Your answer talks about `DateTime` being null, but it's a value type. What do you actually mean?
Steven Sudit
@Steven: I think @Chris is about SQL DATETIME not .NET System.DateTime
abatishchev
Sorry - I've made it a bit clearer - the first line will clear up the original question, but the part about the minimum value of `DateTime` is probably irrelevant as he is returning a 'DateTime?`. Can't really say what is going on to get that value (1900-01-01) without seeing what he is doing with the result.
Chris Diver
+2  A: 

Assuming you have:

DateTime? date = GetDate();
command.Parameters.Add("@date").Value = date;

in case when date == null you want to insert SQL NULL i.e. DBNull.Value so you should do next:

DateTime? date = GetDate();
command.Parameters.Add("@date").Value = (object)date ?? DBNull.Value;

which means the same as:

if(date != null)
     // use date
else
     // use DBNull.Value

if you want to take care about nullable datetime in your function you should declare it next way:

private object GetDate()
{
    DateTime date;
    return DateTime.TryParse(selectedDate, out date) ? date : DBNull.Value;
}

command.Parameters.Add("@date").Value = GetDate();

but I don't recommend to do that and use next:

command.Parameters.Add("@date").Value = (object)GetDate() ?? DBNull.Value;
abatishchev
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and 'System.DBNull' get this error in GetDate() method.
Abu Hamzah
You'll need to explicitly set the `DbType` of the parameter, then.
Steven Sudit
@Nisar: Cast `(object)` on `DateTime` when use operators `?` or `??` with `DBBull.Value` which is `object`
abatishchev
@Steven: I used dummy parameter declaration - just as an example. Sure in real application the type should be indicated explicitly
abatishchev
A: 

Are you sure the field allows nulls?

HLGEM
If it doesn't, then it should fail on update or insert, rather than silently "cast" to a minimum.
Steven Sudit
@Steven: Agree. I think OP don't describe enough the real environment
abatishchev
not if it has a default value set
HLGEM