Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Making datetime field null, SQL

Making datetime field null, SQL

Scheduled Pinned Locked Moved Visual Basic
databasealgorithmshelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aqzman_
    wrote on last edited by
    #1

    Hey guys, I have a question that I hope isn't too hard to get an anwser too, although even after all the Googling, and searching Code Project I've done I haven't had much luck yet. What I'm trying to do is make a Datetime field in a SQL database null (it's already set to allow nulls), but no matter how many different ways I try I either get an error or the default lowest time is just filled in (1900/01/01 12:00:00 AM). A time being there relys on the users input, so in some cases it might need to be filled in, in other cases it doesn't. Anyway, here is my code, if anymore is needed just ask!

        Dim DateClosed As Date
        State = (ddlState.SelectedItem.Value)
        If State = 0 Then
            DateClosed = CDate(lstYear2.SelectedValue & "/" & lstMonth2.SelectedValue & "/" & lstDay2.SelectedValue)
            Time = " " & ddlHour2.SelectedItem.Value + ":" + ddlMin2.SelectedItem.Value + ddlAMPM2.SelectedItem.Value
            DateClosed += Time
        Else
            DateClosed = Nothing
        End If
    

    Just incase anyone needs to know, State is a drop down box with two selections. Open or closed. Open = 1 and Closed = 0. Thanks, aqzman

    D 1 Reply Last reply
    0
    • A aqzman_

      Hey guys, I have a question that I hope isn't too hard to get an anwser too, although even after all the Googling, and searching Code Project I've done I haven't had much luck yet. What I'm trying to do is make a Datetime field in a SQL database null (it's already set to allow nulls), but no matter how many different ways I try I either get an error or the default lowest time is just filled in (1900/01/01 12:00:00 AM). A time being there relys on the users input, so in some cases it might need to be filled in, in other cases it doesn't. Anyway, here is my code, if anymore is needed just ask!

          Dim DateClosed As Date
          State = (ddlState.SelectedItem.Value)
          If State = 0 Then
              DateClosed = CDate(lstYear2.SelectedValue & "/" & lstMonth2.SelectedValue & "/" & lstDay2.SelectedValue)
              Time = " " & ddlHour2.SelectedItem.Value + ":" + ddlMin2.SelectedItem.Value + ddlAMPM2.SelectedItem.Value
              DateClosed += Time
          Else
              DateClosed = Nothing
          End If
      

      Just incase anyone needs to know, State is a drop down box with two selections. Open or closed. Open = 1 and Closed = 0. Thanks, aqzman

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Well, this code doesn't have anything to do with SQL. This only applies to VB.NET 2005 (.NET Framework 2.0) and above! It will NOT work with the .NET Framework 1.1 and below! But, Date, which is just an alias for the DateTime type, normally cannot be Nothing, or null in C#. You'd have to declare it a special way to get it to work.

      Dim myDate As New Nullable(Of DateTime)
      myDate = New Date(2007, 7, 7)
      If myDate.HasValue Then
          ' whatever you want if there is a value
      Else
          ' myDate is Nothing, so...
      End If
      

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Well, this code doesn't have anything to do with SQL. This only applies to VB.NET 2005 (.NET Framework 2.0) and above! It will NOT work with the .NET Framework 1.1 and below! But, Date, which is just an alias for the DateTime type, normally cannot be Nothing, or null in C#. You'd have to declare it a special way to get it to work.

        Dim myDate As New Nullable(Of DateTime)
        myDate = New Date(2007, 7, 7)
        If myDate.HasValue Then
            ' whatever you want if there is a value
        Else
            ' myDate is Nothing, so...
        End If
        

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        A Offline
        A Offline
        aqzman_
        wrote on last edited by
        #3

        Hi, Thanks a lot for the reply! I've actually tried the As New Nullable(Of DateTime) code before, but there's a problem with that in other parts of my code. One part is where I use a '+' symbol it says "Operator '+' is not defined for types 'System.Nullable(Of Date)'" and it also causes an error on my SQL insert statement which is "Operator '&' is not defined for types 'String' and 'System.Nullable(Of Date)'." I'm fairly new to VB.NET, SQL, and ASP.NET but those things seem to be almost totally unrelated as far as a nullable variable goes, guess not. Oh, and just to let you know, I am using the .NET 2.0 framework. Thanks again for the reply, aqzman

        D 1 Reply Last reply
        0
        • A aqzman_

          Hi, Thanks a lot for the reply! I've actually tried the As New Nullable(Of DateTime) code before, but there's a problem with that in other parts of my code. One part is where I use a '+' symbol it says "Operator '+' is not defined for types 'System.Nullable(Of Date)'" and it also causes an error on my SQL insert statement which is "Operator '&' is not defined for types 'String' and 'System.Nullable(Of Date)'." I'm fairly new to VB.NET, SQL, and ASP.NET but those things seem to be almost totally unrelated as far as a nullable variable goes, guess not. Oh, and just to let you know, I am using the .NET 2.0 framework. Thanks again for the reply, aqzman

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          aqzman_ wrote:

          I've actually tried the As New Nullable(Of DateTime) code before, but there's a problem with that in other parts of my code. One part is where I use a '+' symbol it says "Operator '+' is not defined for types 'System.Nullable(Of Date)'" and it also causes an error on my SQL insert statement which is "Operator '&' is not defined for types 'String' and 'System.Nullable(Of Date)'."

          That's because you have to get the value out of the Nullable type before you can use it.

          Dim myDate As Nullable(Of DateTime)
          myDate = New Date(2007, 7, 7)
          
          ' myDate.Value returns a Date object, IF there is a value...
          If myDate.HasValue Then
              Debug.WriteLine("DATE: " & **myDate.Value**.ToShortDateString())
          Else
              Debug.WriteLine("DATE: Nothing")
          End If
          

          -- modified at 13:46 Monday 16th July, 2007

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          A 1 Reply Last reply
          0
          • D Dave Kreskowiak

            aqzman_ wrote:

            I've actually tried the As New Nullable(Of DateTime) code before, but there's a problem with that in other parts of my code. One part is where I use a '+' symbol it says "Operator '+' is not defined for types 'System.Nullable(Of Date)'" and it also causes an error on my SQL insert statement which is "Operator '&' is not defined for types 'String' and 'System.Nullable(Of Date)'."

            That's because you have to get the value out of the Nullable type before you can use it.

            Dim myDate As Nullable(Of DateTime)
            myDate = New Date(2007, 7, 7)
            
            ' myDate.Value returns a Date object, IF there is a value...
            If myDate.HasValue Then
                Debug.WriteLine("DATE: " & **myDate.Value**.ToShortDateString())
            Else
                Debug.WriteLine("DATE: Nothing")
            End If
            

            -- modified at 13:46 Monday 16th July, 2007

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            A Offline
            A Offline
            aqzman_
            wrote on last edited by
            #5

            Thanks a lot for your help! I really appreciate it.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups