-1

im tring to insert new data to an Access table. the table has 4 parameters. the first one is an autonumber. this is my code:

public void InsertToDB(int stationId, DateTime dateTime, double temperture)
{

    connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database1.accdb";

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = connect;
    cmd.CommandText = "Insert Into TempDate (StationID,DateTime,Temper)" + "Values(@ID,@dateTime,@temper)";

    cmd.Parameters.Add("@ID", OleDbType.Integer).Value = stationId;
    cmd.Parameters.Add("@dateTime", OleDbType.Date).Value = dateTime;
    cmd.Parameters.Add("@temper", OleDbType.Double).Value = temperture;

    connect.Open();
    cmd.ExecuteNonQuery();          //At this line exception is generating
    connect.Close();


}// insert to db

im getting this error:

ErrorCode=-2147217900

Nir Politi
  • 21
  • 2
  • 2
    Maybe just put that errorcode to google search?? Already gives so many results how to fix it, for instance http://stackoverflow.com/questions/13898551/i-am-getting-an-error-when-i-try-to-update-records-in-my-access-database – walther Sep 06 '14 at 14:51

2 Answers2

1

You are missing a space before Values. You can put it in one string, no need to concatenate them. Also, as Gord said - DateTime is a reserved word - so put it in brackets. For example:

cmd.CommandText = "Insert Into TempDate (StationID,[DateTime],Temper) Values (@ID,@dateTime,@temper)";
Donal
  • 31,121
  • 10
  • 63
  • 72
0

DATETIME is apparently a reserved word so you need to enclose it in square brackets:

cmd.CommandText = "Insert Into TempDate (StationID,[DateTime],Temper)" + ...
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418