Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:36:02+00:00 2026-05-16T10:36:02+00:00

I couldn’t find best solution when DateTime value is NULL. I use this techinque

  • 0

I couldn’t find best solution when DateTime value is NULL.

I use this techinque When binding;

_ACTIVATION_DATE = dt.Rows[0]["ACTIVATION_DATE"] == DBNull.Value ? new DateTime(1970, 12, 30) : (DateTime?)dt.Rows[0]["ACTIVATION_DATE"];

When Inserting;

public void Insert()
{
    string ad="";
    string dd="";

    if (ACTIVATION_DATE == null)
        ad = "null";
    else
        ad = "'" + ACTIVATION_DATE + "'";
    if (DEACTIVATION_DATE == null)
        dd = "null";
    else
        dd = "'" +DEACTIVATION_DATE +"'";

    string sSQL = "INSERT INTO LINE_INFO (ACTIVATION_DATE,DEACTIVATION_DATE,STATUS,PO,NOTES) VALUES (" + ad + "," + dd + "," + _STATUS.ToString() + "," + _PO.ToString() + ",'" + _NOTES.ToString() + "');SELECT @@IDENTITY AS LASTID";
    }

Variables;

DateTime? ACTIVATION_DATE;
DateTime? DEACTIVATION_DATE;

What is the smart way to handle Null DateTime values?

When I find the solution I will write an article about this topic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:36:03+00:00Added an answer on May 16, 2026 at 10:36 am

    Why are you using new DateTime(1970, 12, 30) when you’re already using a nullable DateTime? The whole point of nullable value types is that you don’t need magic values like that.

    I would possibly use:

    _ACTIVATION_DATE = dt.Rows[0]["ACTIVATION_DATE"] as DateTime?;
    

    That will automatically use the null value for any non-DateTime value. Of course, that means you’ll get a null value instead of an exception if you accidentally have an integer or something like that. Alternatively:

    object tmp = dt.Rows[0]["ACTIVATION_DATE"];
    _ACTIVATION_DATE = tmp is DbNull ? null : (DateTime?) tmp;
    

    Then for the insert statement, do not include values directly in your SQL. Use a parameterized insert statement, and then you can just use a null DateTime? value to insert a null value. No need to mess around with string formats.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Couldn't find a function for this. I'm assuming I need to use regex? I'm
I couldn't find anything similar to this anywhere. I have an array of pointers
I couldn't find this in the documentation. If I have a complex object in
I couldn't find anything on this, but probably its just because I don't know
I couldn't find a suitable title for this. I'm going to express my query
Couldn't find similar enough question so I'll ask. I'm trying to use facebook log-in
couldn't find a similar topic but this may boil down to not knowing how
Couldn't find a better place to ask this so I hope you guys can
Couldn't find this anywhere, maybe I'm looking for the wrong verbs. I'm trying to
Couldn't find anything on this with a Google Search. Does anyone know how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.