How to set a date field to null

I have a bound text box showing a date field and I can enter a date into this no problem and save the changes.

I have spent the past two days trying to find a way to allow the user to delete the date i.e. set the field back to null!

I have found several articles on the net but nothing that provides an answer or explains why something that should be simple should be so hard!!



Answer this question

How to set a date field to null

  • Java Programmer

    To be honest, I have no plans to ever use that stuff. IMO it pulls too much database specific code into the presentation tier, and also forces me to not use stored procs.

  • Arjang Fahim

    I guess what you're asking is, you need a book on T-SQL I bought a book on SQL in general, it covered how to write stored procs, then I bought a series called the Gurus guide, from memory, which is full of excellent, in depth T-SQL info. T-SQL is SQL in SQL Server, searching for T-SQL might give you want you want.



  • banjaxed

    thx

  • ppl1

    I think your right, the auto generated stuff is proving to be more of a pain than a help!

    think I'm going to start from scratch and do everything through code, i thought the new drag and drop features would save me time but I have actually spent more time searching for answers to silly problems like this than I have developing my application.

    So much for progress!

    Thanks anyway


  • Mattxxxxxx

    I created the table using the visual design tools in the database explorere. The field is set to nullable.

    I can run the following SQL from the designer and it works and correctly nulls all the MyDate entries: -

    UPDATE MyTable SET MyDate = NULL

    I then created a simple form with a databound text box etc. but I cant 'erase' a date that has been entered. If I clear the textbox then I cant not change the focus away fom thath field and I have tried numerous ways to clear the field via code!

    i'm getting to the stage where I think it will be easier to store the date as a string!

    Sorry if my response sounds a bit off but I have been googling for an answer for the past few hours and come across many others in the same position and I just cant understand why MS have made something that was simple so hard! - I have never have problems like this in earlier versions of VB or when writing Oracle applications and even Access seems happy enough to handle NULL values!


  • Rahul Virli

    DateTime is not a nullable type. If it's nullable in your db, you need to allow it to be set that way in your UI, and use DBNull.Value as what you pass into the DB if that condition exists, instead of a datetime.



  • C#newbie

    hi,

    sorry if my question out of topic but i want to excuze in small question

    cgraus i want to ask you this question since long time ago,

    i delay learning sqlserver to a time when i need its features like storedprocedures and big scale databases , anyway that time didn't came yet . but my problem is i didn't find material to teach me sqlserver features from scratch all what i found was talking about sqlserver adminstration . i don't need any adminstration and don't want to be specialist in databases all what i want is simply to learn how to use its features, is there any guidance in that

    best regards



  • Edward JJJJ

    The core problem seems to be that you have a databound text box. I presume this means you have no control over the SQL being passed to the DB I do this sort of thing all the time, and where I need to support a NULL field on a non nullable type, I'd write code that passes my datatime to my stored proc something like this:

    // lots of other stuff to call the proc

    , (isDateTimeNull) DbValue.Null : MyDateTime,

    // rest of hte parameters

    Now, VB does not have : conditionals, but I believe iif does the same thing for you. However, if you relinquish control over your data base to automatically generated SQL, then you're stuck with what the library allows. And this auto generated stuff is new, there are bound to be wrinkles.



  • How to set a date field to null