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. Database & SysAdmin
  3. Database
  4. Inserting DateTime value

Inserting DateTime value

Scheduled Pinned Locked Moved Database
helpdatabasesecurityannouncement
2 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.
  • F Offline
    F Offline
    Francis B
    wrote on last edited by
    #1

    Hi! I have a problem when I try to insert DateTime value in an access database. I receive this exception: "Syntax error in INSERT INTO statement." I try two different ways to add Datetime and no one works! newDataRow["Timestamp"] = DateTime.Now.ToString(); and newDataRow["Timestamp"] = DateTime.Now; But if I remove all DateTime stuff in my database and my code, I am able to insert data whitout any problem. Here my code:

    DataSet dataSet = new DataSet();
    string dbGenConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
    string dbConn = dbGenConn + "fileName.mdb" + "; Persist Security Info=False;";
    OleDbConnection conn = new OleDbConnection(dbConn);
    conn.Open(); 
    
    OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM Data", conn));
    OleDbCommandBuilder dataCmdBuilder = new OleDbCommandBuilder(adapter);
    adapter.Fill(dataSet, "Data");
    
    for(int i = 0; i < items.Length; i++)
    {	
    	DataRow newDataRow = dataSet.Tables["Data"].NewRow();
    	newDataRow["DataID"] = i;
    	newDataRow["Timestamp"] = DateTime.Now.ToString();
    	newDataRow["Quality"] = 192
    	dataSet.Tables["Data"].Rows.Add(newDataRow);	
    }
    
    adapter.Update(dataSet, "Data");
    conn.Close();
    
    M 1 Reply Last reply
    0
    • F Francis B

      Hi! I have a problem when I try to insert DateTime value in an access database. I receive this exception: "Syntax error in INSERT INTO statement." I try two different ways to add Datetime and no one works! newDataRow["Timestamp"] = DateTime.Now.ToString(); and newDataRow["Timestamp"] = DateTime.Now; But if I remove all DateTime stuff in my database and my code, I am able to insert data whitout any problem. Here my code:

      DataSet dataSet = new DataSet();
      string dbGenConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
      string dbConn = dbGenConn + "fileName.mdb" + "; Persist Security Info=False;";
      OleDbConnection conn = new OleDbConnection(dbConn);
      conn.Open(); 
      
      OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM Data", conn));
      OleDbCommandBuilder dataCmdBuilder = new OleDbCommandBuilder(adapter);
      adapter.Fill(dataSet, "Data");
      
      for(int i = 0; i < items.Length; i++)
      {	
      	DataRow newDataRow = dataSet.Tables["Data"].NewRow();
      	newDataRow["DataID"] = i;
      	newDataRow["Timestamp"] = DateTime.Now.ToString();
      	newDataRow["Quality"] = 192
      	dataSet.Tables["Data"].Rows.Add(newDataRow);	
      }
      
      adapter.Update(dataSet, "Data");
      conn.Close();
      
      M Offline
      M Offline
      Marcel Harry
      wrote on last edited by
      #2

      i think this is the problem: newDataRow["Timestamp"] = DateTime.Now.ToString(); if your database field is a text field use newDataRow["Timestamp"] = Convert.ToString(DateTime.Now); and if it's a datetime (much better) use it this way newDataRow["Timestamp"] = DateTime.Now;

      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