Wrox Programmer Forums
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2005, 08:13 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default Null in datetime

Can i aSSIGN NULL to a DateTime

 
Old January 25th, 2005, 01:35 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I don't think so. However, you can assign it DateTime.MinValue and then consider anything that equals DateTime.MinValue to be null.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 25th, 2005, 02:42 PM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Normally we would try to use DBNull.Value .. instead try SqlDateTime.Null .. it would work.

1. Make use of namespace :: System.Data.SqlTypes
2. SqlDateTime strSql; //Declare a variable
3. strSql = SqlDateTime.Null; //assigning null to that variable

Use this variable while adding parameters to the command object.

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old January 25th, 2005, 03:14 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

null is a reference type while DateTime is a value type(struct) so you can't assign them together.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old January 26th, 2005, 11:39 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ops!I should edit my previous post,
we can assign an object to a struct like DateTime(Unboxing) and also a struct to an object(Boxing),
Code:
DateTime dt1=new DateTime(m,y,d);
object o=dt1//Boxing
DateTime dt2=(DateTime)o//Unboxing
but about null,as I think there is nothing in null!so we can't use Boxing and Unboxing.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old January 26th, 2005, 01:17 PM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Check this out :: http://www.c-sharpcorner.com/Code/20...orDateTime.asp

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old January 26th, 2005, 01:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The article deals with inserting null in the database, for which DBNull.Value is indeed the most logical option.

However, the OP asked if you can assign null to a DateTime value.

Consider a simple class like this:
Code:
    public class Person
    {
    private DateTime birthDay;

    public void Save()
    {
        // save person to database
    }

    public DateTime BirthDay
    {
      get
      {
        return birthDay;
      }
      set
      {
        birthDay = value;
      }
    }
    }
How would you, inside the Save method, determine what the value of birthDay is? The article you linked to compares txtDate.Text against "" which just checks for an empty string.
However, inside your class, there is no textbox (or calendar control or whatever) to compare with. All you have is the backing birthDay variable.

There must be other ways to do this, but I usually use DateTime.MinValue to initialize the birthDay variable.
Then in the Save method, I check if the birthDay is still MinValue. If it is, I consider it to be equal to DBNull.Value and update my database accordingly.

Cheers,

Imar
 
Old January 26th, 2005, 06:15 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,I think like DateTime you obey the same logic for int or float or decimal,but I have a question here,what can we do for bool type?

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old January 26th, 2005, 06:19 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What do you mean with "obey the same logic for int or float or decimal"?

For bool you don't have much options, I guess. You can either initialize them to true or false. What you choose probably depends on what default value makes the most sense in your specific application.

The same applies to the other types you mentioned; you'll have to provide a sensible default....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Made For TV Movie by Incubus (Track 11 from the album: A Crow Left Of The Murder...) What's This?
 
Old January 27th, 2005, 08:53 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

sorry,I wasn't clear enough,
suppose for a person I have a boolean field in DB,it could have tree values,
  • 1-True
  • 2-False
  • 3-null
and in my C# code I would have something like below,
Code:
public class Person
{
    private DateTime birthDay;
    private bool isSingle;
    //...
    public bool IsSingle
    {
      get
      {
        return isSingle;
      }
      set
      {
        isSingle = value;
      }
    }
    //...
}
now for DateTime if in DB it has a null value,in C# code I choose DateTime.MinValue...(like other types for example if an integer field in DB has a null value I represent it with int.MinValue),but for bool there is nothing except true or false...
I found two solutions,Thanks.:)

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.





Similar Threads
Thread Thread Starter Forum Replies Last Post
datetime MathLearner Beginning VB 6 0 November 15th, 2007 07:12 AM
DateTime pzmrcd C# 3 July 26th, 2007 06:35 AM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
UTC DateTime to Local DateTime r_ganesh76 SQL Server 2000 1 April 4th, 2005 08:21 AM
DateTime DARSIN General .NET 1 December 1st, 2004 06:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.