Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
How to check if DateTime variable is not assigned?

I want to pass a 'Myobject' object into my function. Like this:

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

...


But.. It is possible that this object will have not assigned StartDate. So, I will get an error from SQL like this: "SqlDateTime overflow. Must be between ..."

I tried like that, but it didn't work:

C#
...

using (SqlCommand sc = new SqlCommand("AddSchedule", sqlConnection))
{
    sc.CommandType = CommandType.StoredProcedure;
    sc.CommandTimeout = 100;
    sc.Transaction = transaction;

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

}


Please help.

----------------------------------------------------------------------------------------------------

I still can't solve that:/ This is... weird!
Once again, VERY SIMPLE code:

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

and PROGRAM:
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));
    }



mo.StartDate.HasValue returns always true...
What the hell ?


EDIT:
All I want is this 'if':
if (mo.StartDate.HasValue)
Something which tell me, if the DateTime property of the object was used or not - thats all I want. There must be an easy solution..

I even tried


C#
MyObject mo = new MyObject();

if ((mo.StartDate.Value) == null) // This returns FALSE! HOW?!
{
    sc.Parameters.Add(new SqlParameter("@inStartDate", DBNull.Value));
}
Posted
Updated 5-Jul-11 3:18am
v4
Comments
dasblinkenlight 6-Jul-11 8:22am    
// This returns FALSE! HOW?!

mo.StartDate.Value is of type DateTime, which is a struct. It cannot possibly be null.

You also have to be aware that the range of SQL DateTime is smaller than the range of .NET type.

In particular if you are using smalldatetime, then a DateTime with it default value would out of SQL range.

For normal SQL datetime, there is a problem using DateTime.MaxValue since SQL type is less precise. After rounding to SQL precision, the value would be of of rangle since it would round to year 10000.
 
Share this answer
 
Try _schedule.StartDate.Value instead of _schedule.StartDate, and make sure the corresponding SP parameter is nullable.
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 19:24pm    
He's doing that.
dasblinkenlight 4-Jul-11 19:38pm    
Not really: _schedule.StartDate is of type Nullable<datetime>, and that's what the C# code is passing to the constructor of SqlParameter.
NDebata 4-Jul-11 23:23pm    
Yes he need to pass the value, instead of the object.
You're checking if your datetime has a value, but does your SQL code also check. Use is null as in, if @date is null, to check if you were passed a null, if you need to write special case code to deal with that. The real issue is in your SP as far as I can see, you're checking and passing in a null if your value is null, so your code in C# is fine.
 
Share this answer
 
You may use StartDate = Default(DateTime)..
 
Share this answer
 
Comments
mnd017 5-Jul-11 8:48am    
I don't understand.. how ? where?
Perhaps your value is not defaulting to null ? StartDate.HasValue will work. If "if (mo.StartDate.GetValueOrDefault() != DateTime.MinValue)" is working for you, then your value is defaulting to DateTime.MinValue. I'd suggest adding a constructor to your class that initialises it to null, and then using it the way you first intended. As it stands, you may as well not have a nullable object.
 
Share this answer
 
Comments
Dalek Dave 5-Jul-11 20:42pm    
Two answers on the same question?

Gaming it?
dasblinkenlight 6-Jul-11 8:55am    
This question has been edited four times. By the end of the last edit, it has become a different question.
You need to remove '@' from parameter name strings:
C#
if (_schedule.StartDate.HasValue)
    {
        sc.Parameters.Add(new SqlParameter("inStartDate", _schedule.StartDate));
    }
    else
    {
        sc.Parameters.Add(new SqlParameter("inStartDate", DBNull.Value));
    }
 
Share this answer
 
Hi mnd701,

Why do you get null in the StartDate ?
I tried the following code and it just works fine, shows the length as 19.

C#
public DateTime StartTime
{
    get;
    set;
}
private void button4_Click(object sender, EventArgs e)
{
    MessageBox.Show("StartTimeLen = " + StartTime.ToString().Length);
}


Just StartTime property is made and on a button click I display this message box, It shows me "19" as its length.

I think its not the issue that StartDate is null or doesn't have a value - by default DateTime has its min value for its each field.
As pointed by Phillipe Mori, the SQL has a low range of Date & Time and the date you get in C# doesn't fit the range of SQL & that's why you are facing error.

I recommend debug your app by displaying StartDate before passing to SQL. If you still can't make out, provide the full error message to help you better.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900