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

  • SEARCH
  • Home
  • 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 6125877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:17:59+00:00 2026-05-23T16:17:59+00:00

So, First of all. Code: I’ve got a class: public class Myobject { public

  • 0

So, First of all. Code:
I’ve got a class:

public class Myobject
{
    public string Code { get; set; }
    public DateTime? StartDate { get; set; }
}

And this is part of very simple source:

MyObject mo = new MyObject();
mo.Code= "sth";
// NO action on StartDate property! 

    if (mo.StartDate.HasValue)
    {
        sc.Parameters.Add(new SqlParameter("@inStartDate", mo.StartDate.Value));
    }
    else
    {
        sc.Parameters.Add(new SqlParameter("@inStartDate", DBNull.Value));
    }

Simple ‘if’ – Sql Server 2008, throw an error – when gets null Datetime (it has to be DBNull.Value)
So I want to check it first, and then pass right value or DBNull.

My problem is – this ‘if’ always retruns true! Why!?

Also tried that:

if (mo.StartDate.Value == null)

but it always returns false. How come it is not a null? It was not even created..

So.. How to check if DateTime object was not assigned?

  • 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-23T16:17:59+00:00Added an answer on May 23, 2026 at 4:17 pm

    Try this:

    
    if (mo.StartDate.GetValueOrDefault() != DateTime.MinValue) 
    {
      // True - mo.StartDate has value
    }
    else
    {
      // False - mo.StartDate doesn't have value
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all have a look at the following code (in this code shape
First of all, pardon my hacky code, I'm just trying to try this out
First some context: all the code pasted below is within another class declared as
First, an example of something that works as expected: (all code was executed in
First of all, do not be overwhelmed by the long code, I just put
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First of all there is a partial question regarding this, but it is not
I have few questions. First of all does code below violates law of Demeter?
First of all, most of the follow code I think is not related to
First of all, I'd like to point out that I'm not asking for code.

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.