save null to date field

  • I have a date/time field in SQL Server 2000.  My dot net app has a data grid where user can enter an appointment time. 

    I want to let user edit or even delete the time.  If the user deletes the time, how can I save "Null" to the database in dot net.

    Thank  you,

  • Two things you need, first the column has to be defined nullable in the database.  Then you can save System.DBNull when you update the table.

  • I have the column defined as nullable.  I tried the following:

    dr.appt_time = System.DBNull.Value

    and get intellisense msg -- Value of type System.DBNull cannot be converted to date.

    dr.appt_time = System.DBNull gives me the msg:

    DBNull is a type in System and cannot be used as an expression.

    I am trying to update via a dataset in a web app. 

    This shouldn't be that hard!!!  Any other suggestions?

  • put an explicit cast on the assignment

    dr.appt_time = (datetime) System.DBNull.Value

  • Hello...

     

    You might take a look at the so called "SmartDate" Datatype. This Datatype is NOT is default .NEt Datatype, but it is included with CSLA.Net from Rocky Lhotka. Its a datatype that will allow easier handling of NULLs in text fields ect. (Since it will accept blank strings and not blow up, also you can define if "NULL" means min or max date for comparisons) Try to look it up...I found it very handy. And there is also a "smartDatareader" which will strip away your NULLs since most times they are not "needed"

  • I tried this

    dr.appt_time = DirectCast(System.DBNull.Value, DateTime)

     

    and got error:  Invalid cast exception

    What am I doing wrong here?

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply