简体   繁体   中英

datetime issue with 01/01/1900

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;          
}

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'

Assuming you have:

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

in case when date == null you want to insert SQL NULL ie 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;

If you are sending query as parameter (of string type) to another method which executes the query like the one below:

        int userno = 123;
        string date_variable = null;

        string query = string.Format(
                    @"INSERT INTO system_log (userno,ref_date) values ({0},'{1}');",userno,date_variable);

        obj.executeInsert(query,conn);

This might again save the default date when executed using ExecuteNonQuery() or something else.

Even passing (object)date_variable ?? DBNull.Value; wont work in this scenario

Then you can simply set date_variable as "null"

        if (string.IsNullOrEmpty(date_variable))
            date_variable= "null";
        else
            date_variable= "'" + date_variable+ "'";
        string query = string.Format(
                @"INSERT INTO system_log (userno,ref_date) values ({0},{1});",123,date_variable);
         obj.executeInsert(query,conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM