Date/Time problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Devlon

    #1

    Date/Time problem

    Hi,

    I've created a class With a few properties of type "date".
    I also have a SQL-database (which I can't change) with columns of type
    "datetime"

    when I use ...

    clsInfo.date = now()

    .... I can't pas the date to the database without errors...


    When I create a date property of type string and I use it like this ...

    clsInfo.dateStr ing = "2006-12-2"

    .... it works fine...

    Anyone any idea's ? Do I have to convert the date to string before sending
    it to the database ?

    thanx

    John




  • GhostInAK

    #2
    Re: Date/Time problem

    Hello John,

    There's no need to convert it to string..

    Dim tConnection As SqlConnection = New SqlConnection(" connection string here")
    Dim tCommand as SqlCommand = New SQlCommand

    With tCommand
    .Connection = tConnection
    .CommandType = CommandType.Tex t
    .CommandText = "INSERT INTO someTable (dateField) VALUES (@dateValue)
    .Parameters.Add WithValue("@dat eValue", Now)
    End With

    tConnection.Ope n
    tCommand.Execut eNonQuery
    tConnection.Clo se


    Enjoy,
    -Boo
    Hi,
    >
    I've created a class With a few properties of type "date".
    I also have a SQL-database (which I can't change) with columns of type
    "datetime"
    when I use ...
    >
    clsInfo.date = now()
    >
    ... I can't pas the date to the database without errors...
    >
    When I create a date property of type string and I use it like this
    ...
    >
    clsInfo.dateStr ing = "2006-12-2"
    >
    ... it works fine...
    >
    Anyone any idea's ? Do I have to convert the date to string before
    sending it to the database ?
    >
    thanx
    >
    John
    >

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Date/Time problem

      In addition to Boo,

      As you have done it correct of course and your DateTime is a real DateTime
      field in your database and not a string as sometimes is done.

      Cor

      "GhostInAK" <ghostinak@gmai l.comschreef in bericht
      news:be1391bf15 dbd8c899a680582 a6f@news.micros oft.com...
      Hello John,
      >
      There's no need to convert it to string..
      >
      Dim tConnection As SqlConnection = New SqlConnection(" connection string
      here")
      Dim tCommand as SqlCommand = New SQlCommand
      >
      With tCommand
      .Connection = tConnection
      .CommandType = CommandType.Tex t
      .CommandText = "INSERT INTO someTable (dateField) VALUES (@dateValue)
      .Parameters.Add WithValue("@dat eValue", Now)
      End With
      >
      tConnection.Ope n
      tCommand.Execut eNonQuery
      tConnection.Clo se
      >
      >
      Enjoy,
      -Boo
      >
      >Hi,
      >>
      >I've created a class With a few properties of type "date".
      >I also have a SQL-database (which I can't change) with columns of type
      >"datetime"
      >when I use ...
      >>
      >clsInfo.date = now()
      >>
      >... I can't pas the date to the database without errors...
      >>
      >When I create a date property of type string and I use it like this
      >...
      >>
      >clsInfo.dateSt ring = "2006-12-2"
      >>
      >... it works fine...
      >>
      >Anyone any idea's ? Do I have to convert the date to string before
      >sending it to the database ?
      >>
      >thanx
      >>
      >John
      >>
      >
      >

      Comment

      Working...