Data type question for OLEdb data types

Discussion in 'ASP .Net' started by Brian Henry, Oct 4, 2003.

  1. Brian Henry

    Brian Henry Guest

    I have an access database, and one of the fields in the table I am inserting
    into has a date/time data type. What is the correct OleDb data type to
    insert the date and time that it is at the moment that the record was
    inserted at. I had this, but it gives me a "data type mismatch" error on
    run.

    Dim cmdPostQuestion As New OleDb.OleDbCommand("INSERT INTO hd_questionsasked
    (askedBy,questionText,dateAsked) VALUES (?,?,?)")

    cmdPostQuestion.Connection = dbConnection

    If dbConnection.State = ConnectionState.Closed Then

    dbConnection.Open()

    End If

    cmdPostQuestion.Parameters.Add("askedBy", OleDb.OleDbType.VarWChar).Value =
    getUserName()

    cmdPostQuestion.Parameters.Add("dateAsked", OleDb.OleDbType.DBDate).Value =
    Now.Date

    cmdPostQuestion.Parameters.Add("questionText",
    OleDb.OleDbType.VarWChar).Value = Me.txtAskedQuestion.Text

    cmdPostQuestion.ExecuteNonQuery()
    dbConnection.Close()


    if i take out anything relateing to the date, it works just fine...
     
    Brian Henry, Oct 4, 2003
    #1
    1. Advertising

  2. Brian Henry

    Kirk Guest

    Brian,

    When you are using the OleDBCommand to execute a paramaterized query, the
    parameters must be added to the command in the same order they appear in the
    SQL statement. Move the DateAsked Parameter.add statement to after the
    questionText Parameter.Add statement.

    Also, try using Date.Today.ToString instead of Now.Date. Just an issue of
    using the .Net Framework version instead of the VB compatibility version.

    This should fix the problem.
    ****************************
    Dim cmdPostQuestion As New OleDb.OleDbCommand("INSERT INTO hd_questionsasked
    (askedBy,questionText,dateAsked) VALUES (?,?,?)")

    cmdPostQuestion.Connection = dbConnection

    If dbConnection.State = ConnectionState.Closed Then

    dbConnection.Open()

    End If

    cmdPostQuestion.Parameters.Add("askedBy", OleDb.OleDbType.VarWChar).Value =
    getUserName()

    cmdPostQuestion.Parameters.Add("questionText",
    OleDb.OleDbType.VarWChar).Value =Me.txtAskedQuestion.Text

    cmdPostQuestion.Parameters.Add("dateAsked", OleDb.OleDbType.DBDate).Value =
    Date.Today.ToString

    cmdPostQuestion.ExecuteNonQuery()
    dbConnection.Close()
    **************************************

    Kirk Graves

    "Brian Henry" <> wrote in message
    news:%...
    > I have an access database, and one of the fields in the table I am

    inserting
    > into has a date/time data type. What is the correct OleDb data type to
    > insert the date and time that it is at the moment that the record was
    > inserted at. I had this, but it gives me a "data type mismatch" error on
    > run.
    >
    > Dim cmdPostQuestion As New OleDb.OleDbCommand("INSERT INTO

    hd_questionsasked
    > (askedBy,questionText,dateAsked) VALUES (?,?,?)")
    >
    > cmdPostQuestion.Connection = dbConnection
    >
    > If dbConnection.State = ConnectionState.Closed Then
    >
    > dbConnection.Open()
    >
    > End If
    >
    > cmdPostQuestion.Parameters.Add("askedBy", OleDb.OleDbType.VarWChar).Value

    =
    > getUserName()
    >
    > cmdPostQuestion.Parameters.Add("dateAsked", OleDb.OleDbType.DBDate).Value

    =
    > Now.Date
    >
    > cmdPostQuestion.Parameters.Add("questionText",
    > OleDb.OleDbType.VarWChar).Value = Me.txtAskedQuestion.Text
    >
    > cmdPostQuestion.ExecuteNonQuery()
    > dbConnection.Close()
    >
    >
    > if i take out anything relateing to the date, it works just fine...
    >
    >
     
    Kirk, Oct 5, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Hanson
    Replies:
    4
    Views:
    573
    Tu-Thach
    Jul 22, 2003
  2. Robert Hanson
    Replies:
    1
    Views:
    351
    Robert V. Hanson
    Jul 22, 2003
  3. Bob [BVP]
    Replies:
    2
    Views:
    410
    Bob [BVP]
    Jan 21, 2005
  4. NicK chlam via DotNetMonster.com
    Replies:
    0
    Views:
    4,867
    NicK chlam via DotNetMonster.com
    Mar 21, 2005
  5. psychomad
    Replies:
    2
    Views:
    5,220
    Alexey Smirnov
    Apr 10, 2007
Loading...

Share This Page