Results 1 to 7 of 7

Thread: how to insert null value in date field?

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    how to insert null value in date field?

    i have a maskedtextbox which will accept only date in the format of "DD-MM-yyyy" no i want to insert this date in my db. i have done this successfully.

    Now i want to insert null value in through that maskedtextbox .

    please give me a sample code for the above

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: how to insert null value in date field?

    Check if the textbox is blank then insert System.Data.SqlTypes.SqlDateTime.Null to DB date field.
    thanks
    amrita

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: how to insert null value in date field?

    thanks for ur reply. i am using odbc connection so i need odbc connectivity code

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: how to insert null value in date field?

    I would suggest not using a MaskedTextBox. The .NET Framework provides the DateTimePicker for date/time display and input. If you want to support null values, set ShowCheckBox to True and use whether or not it is checked to determine whether to insert a value or not, e.g.
    vb.net Code:
    1. If completedDatePicker.Checked Then
    2.     row("CompletedDate") = completedDatePicker.Value.Date
    3. Else
    4.     row("CompletedDate") = DBNull.Value
    5. End If
    You always use DBNull.Value to represent a null value no matter the data type or the provider.

  5. #5
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: how to insert null value in date field?

    Simply inserting NULL will do the job , I guess.
    thanks
    amrita

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: how to insert null value in date field?

    Quote Originally Posted by amrita View Post
    Simply inserting NULL will do the job , I guess.
    Only if you write it literally into the SQl code, which you shouldn't do. If you're using parameters, as you should, or a DataTable then you must use DBNull.Value.

  7. #7
    Banned
    Join Date
    Nov 2010
    Location
    texas
    Posts
    4

    Re: how to insert null value in date field?

    thank u so much for sharing this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width