Results 1 to 6 of 6

Thread: inserting time stamp into database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    KL Malaysia
    Posts
    64

    inserting time stamp into database

    Here'e the code:
    Code:
    Dim objConn As New OleDbConnection(strDSN)
    Dim objCmd As New OleDbCommand("nwsInsert", objConn)
    objCmd.CommandType = CommandType.StoredProcedure
    
    'get the input time
    Dim dttDate As DateTime
    dttDate = Date.Now.UtcNow
    
    objParam = New OleDbParameter("@postedon", OleDbType.DBTimeStamp)
    objParam.Value = dttDate
    objCmd.Parameters.Add(objParam)
    
    'execute the append
    objCmd.Connection.Open()
    objCmd.ExecuteNonQuery()
    objCmd.Connection.Close()
    objConn.Dispose()
    I'm trying to access an Access database, and the @postedon parameter is supposed to be inserted into a field of type Date/Time and the format is General Date. I tried various OleDbType - DBTimeStamp, DBDate, Date, DBTime, Char - all resulted in "System.Data.OleDb.OleDbException: Data type mismatch in criteria expression." What is the right way to add dates?

  2. #2
    pvb
    Guest
    I created a table(myTable) with two columns('myId' as Numeric and 'myTimeStamp' as Date/Time). Created an action query(qryMyTable_Insert) that took two params to do the insert. Following code executes the query:
    VB Code:
    1. Public Sub timeStampTest()
    2.  
    3.         Dim sConnString As String = _
    4.                  "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    5.                  "Data Source=E:\Progs\DotNet\adosql7test\test.mdb;"
    6.         Dim oleCn As OleDb.OleDbConnection
    7.         Dim sCmdText As String
    8.         Dim oleCmd As OleDb.OleDbCommand
    9.         Dim oleParam As OleDb.OleDbParameter
    10.         oleCn = New OleDb.OleDbConnection(sConnString)
    11.         Try
    12.             oleCn.Open()
    13.             sCmdText = "qryMyTable_Insert"
    14.             oleCmd = New OleDb.OleDbCommand(sCmdText, oleCn)
    15.             oleCmd.CommandType = CommandType.StoredProcedure
    16.  
    17.             oleParam = New OleDb.OleDbParameter("@newId", OleDbType.Numeric)
    18.             oleParam.Value = 5
    19.             oleCmd.Parameters.Add(oleParam)
    20.  
    21.             oleParam = New OleDb.OleDbParameter("@newTimeStamp", OleDbType.Date)
    22.             oleParam.Value = Date.Now()
    23.             oleCmd.Parameters.Add(oleParam)
    24.  
    25.             oleCmd.ExecuteNonQuery()
    26.  
    27.         Catch oleEx As OleDbException
    28.             MessageBox.Show(oleEx.Message.ToString())
    29.         Finally
    30.             oleCmd.Dispose()
    31.             oleCn.Close()
    32.             oleCn.Dispose()
    33.         End Try
    34.         MessageBox.Show("Done!")
    35.     End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    KL Malaysia
    Posts
    64
    Which means your SQL statement is:

    INSERT INTO table ( newID, newTimeStamp )
    VALUES ( [@newID], [@newTimeStamp] )

    Confusingly, i can add dates but not other values, like news title or news content. Which means:

    INSERT INTO table ( theTimeStamp )
    VALUES ( [@theTimeStamp] )

    would work but

    INSERT INTO table ( theTimeStamp, theTitle )
    VALUES ( [@theTimeStamp], [@theTitle] )

    won't work. Is there really a syntatical error here?

  4. #4
    pvb
    Guest
    my query is the following:
    Code:
    INSERT INTO myTable ( myId, myTimeStamp )
    VALUES ([newId], [newTimeStamp]);
    ...and again the 'myId' field is Numeric and the 'myTimeStamp' is Date/Time. I'm using MS Access 2000 format if that makes a difference. I haven't upgraded from Beta 2 yet so maybe if you're on the final release something's changed.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    KL Malaysia
    Posts
    64

    more oledbtype

    I used a loop to check the data type name using objReader.GetDataTypeName and found out that:

    1) a field of type Text is DBTYPE_WVARCHAR, while
    2) a field of type Memo is DBTYPE_WLONGVARCHAR.

    So, I used OleDbType.VarWChar and OleDbType.LongVarWChar instead of OleDbType.Char. Still, it results in Data type mismatch in criteria expression.... what else could result in such an exception ???

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    KL Malaysia
    Posts
    64

    mystery solved

    I have finally solved the problem. The types are correct - but that is not the problem. The problem is somehow the order of adding parameters matter. That is, if your SQL statement is:
    Code:
    INSERT INTO table (field1, field2) VALUES (@field1, @field2)
    the parameters addition must also follow the same order:
    Code:
    Dim objParam as New OleDbParameter("@field1", OleDbType.Integer
    objParam.Value = 0
    objCmd.Parameters.Add(objParam)
    
    objParam as New OleDbParameter("@field2", OleDbType.Char
    objParam.Value = "testing"
    objCmd.Parameters.Add(objParam)
    and this wouldn't work! :
    Code:
    Dim objParam as New OleDbParameter("@field2", OleDbType.Char
    objParam.Value = "testing"
    objCmd.Parameters.Add(objParam)
    
    objParam as New OleDbParameter("@field1", OleDbType.Integer
    objParam.Value = 0
    objCmd.Parameters.Add(objParam)
    I hope this helps for anyone stuck with the same problem that I had for 2 days!

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