Results 1 to 15 of 15

Thread: Inserting Date Into SQL Server

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Inserting Date Into SQL Server

    I have a field set as Datetime and i have a variable that gets todays date... when i try to insert the date i get this error..

    System.Data.OleDb.OleDbException: Syntax error converting datetime from character string.

    Here is my code

    VB Code:
    1. Imports System.Data.OleDb
    2. Public Class WebForm2
    3.     Inherits System.Web.UI.Page
    4.     Dim Conn As OleDbConnection, Command As OleDbCommand, DataRead As OleDbDataReader
    5.  
    6.  
    7.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim Title As String, Teaser As String, Full As String, TheDate As String
    9.         Title = Request.Form("txtTitle")
    10.         Teaser = Request.Form("txtTeaser")
    11.         Full = Request.Form("txtFull")
    12.         TheDate = CDate(Date.Today.ToString)'Todays date i.e... 6/20/2003
    13.  
    14.         ExecuteQuery("INSERT INTO tblNews (NewsTitle,NewsTeaser,NewsFull,NewsDate) VALUES ('Title','Teaser','Full','TheDate')", "Test")
    15.  
    16.         Conn.Close()
    17.  
    18.     End Sub
    19.     Public Sub ExecuteQuery(ByVal SQL As String, ByVal DB As String)
    20.         If Application("Connection") Is Nothing Then
    21.             Try
    22.                 Conn = New OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings.Get(DB))
    23.                 Conn.Open()
    24.                 Command = New OleDbCommand(SQL, Conn)
    25.                 DataRead = Command.ExecuteReader(CommandBehavior.CloseConnection)
    26.             Catch ex As Exception
    27.                 Response.Write(ex)
    28.             End Try
    29.         Else
    30.             Response.Write("Couldn't execute Query, Already Connected. Close the connection and try again.")
    31.         End If
    32.     End Sub
    33.  
    34. End Class

    I dont domprendeh why it's saying it cant convert the date



    Thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Change this
    VB Code:
    1. TheDate = CDate(Date.Today.ToString)'Todays date i.e... 6/20/2003
    to
    VB Code:
    1. TheDate = Date.Today.ToShortDateString() 'Todays date i.e... 6/20/2003

    Also what is the data type of the Date column in the Database?

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Nope i'm getting the same thing.

    I'm using a DateTime datatype.

    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    If the datatype for the DB column is DateTime, then you dont have to use a string variable. Do this
    VB Code:
    1. Dim TheDate As DateTime
    2. TheDate = DateTime.Today
    That should work.

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    don't you need to put hashes around the date or is that just access?
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  6. #6

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Still getting the same error

    System.Data.OleDb.OleDbException: Syntax error converting datetime from character string

    I Tried

    Dim TheDate as DateTime

    TheDate = Datetime.Now

    and

    TheDate = DateTime.Today

    But anyways i don't want to store the time just the date.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    Dates and SQL server - what a nightmare..... The only reliable way I have found is to convert the date to the following format when inserting - dd/mmm/yy i.e. 12 apr 03. Hope this helps.
    Wind and waves resolves all problems.

  8. #8
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    Also just noticed that in the title u are saying thast your are inserting into SQL server. If so use the sqldata objects and the parameter objects. Much faster.
    Wind and waves resolves all problems.

  9. #9
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I just did a web app, and I had no problem inserting dates.

  10. #10

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Originally posted by DevGrp
    I just did a web app, and I had no problem inserting dates.
    And umm did you do it differently than i have already tried?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  11. #11

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    It's very odd that i can manually go into SQL Manager and manualy type in the string 6/20/2003 into the date field and it works just fine, but if i try to send the exact same string thru code it gives an error...

    This is really annoying.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    *bitting my tounge*
    Ah, screw it....
    Why in the world do people insist on doing SQL this way?! And why do they possibly think that this will even work?
    And 10 replies, and not a single right one...
    For crying out loud. This will not work! Never has and NEVER WILL!
    VB Code:
    1. ExecuteQuery("INSERT INTO tblNews (NewsTitle,NewsTeaser,NewsFull,NewsDate) VALUES ('Title','Teaser','Full','TheDate')", "Test")
    It pisses me off to see this kind of programming, and then no-one understands why it won't work.
    Fleck! Pull your smeggin heads out of your arses!
    THIS is what you need.
    VB Code:
    1. ExecuteQuery("INSERT INTO tblNews (NewsTitle,NewsTeaser,NewsFull,NewsDate) VALUES ('" & Title & "','" & Teaser & "','" & Full &"','" & TheDate &"')", "Test")
    The reason it doesn't work is because you ARE PASSING IN THE LITTERALSTRINGS.... NOT THE VARIABLES..... they have to be concatenated together to form the full SQL ....
    Last edited by techgnome; Jun 23rd, 2003 at 11:09 AM.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Ahh yep, that would be it . See i've been programming with PHP for the last 2 years where you pass variables as strings as i was trying to do. Getting used to VB again!

    Thanks mate
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  14. #14
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I was just about to suggest using parameters. It'll save you alot of headaches.

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by Arc
    Ahh yep, that would be it . See i've been programming with PHP for the last 2 years where you pass variables as strings as i was trying to do. Getting used to VB again!

    Thanks mate
    I wondered about that after I posted. I re-looked at the code, and thought "I wonder if he's thinking PHP?" I'm also glad that you didn't take my rant too seriously, but it's such a common mistake and I don't understand why people think that it will work.... The other part that kinda ticked me off was that every one went all over the place......
    Glad you got your answer though.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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