0

Can anyone please tell me whats wrong with this code? Thanks in advance.

enter image description here

myConnection.ConnectionString = myConnectionString;
myConnection.Open();
OdbcCommand command = myConnection.CreateCommand();
command.CommandText = 
     "INSERT INTO Table1(type, from, to, depart, arrival, remain) VALUES(?, ?, ?, ?, ?, ?)";

TimeSpan time = new TimeSpan(10, 7, 00);
DateTime t = new DateTime(2016, 5, 5, 5, 4, 3);
command.Parameters.AddWithValue("@type", "test");
command.Parameters.AddWithValue("@from", "tet");
command.Parameters.AddWithValue("@to", "te");
command.Parameters.AddWithValue("@depart", t);
command.Parameters.AddWithValue("@arrival", t);
command.Parameters.AddWithValue("@remain", 4);

command.ExecuteNonQuery();
5
  • 1
    You are mixing named and positional parameters. Commented Nov 1, 2016 at 12:10
  • Still getting the same error Commented Nov 1, 2016 at 12:12
  • Generally your code looks ok. So what's wrong with it? Are you getting an exception or some unexpected result? Commented Nov 1, 2016 at 12:12
  • I am getting this exception: ERROR [42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. Commented Nov 1, 2016 at 12:13
  • Still getting the same error - what error is that then? Do you not think that might be a useful piece of information? Commented Nov 1, 2016 at 13:22

2 Answers 2

2

You're using Access reserved words type and from as column names in your table.

This can be a reason why you're getting such an error.

You can wrap such a column names with square brackets and change your command text to:

INSERT INTO Table1([type], [from], to, depart, arrival, remain) VALUES(?, ?, ?, ?, ?, ?)
Sign up to request clarification or add additional context in comments.

3 Comments

@Wagner'P If answer was helpful to you - you can mark it as accepted. This can indicate possible solution to someone who will experience such a problem in future. See stackoverflow.com/help/someone-answers for details.
How can I do that?
@Wagner'P well, it was described in the article I've linked in previous comment: "To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in"
0

This is coding for web application insertion function:

Coding:

    string dataPath = Server.MapPath("Your DataPath");
    string ConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataPath + ";Persist Security Info=True";            
    OleDbConnection Con = new OleDbConnection(ConString);
    OleDbCommand Command = Con.CreateCommand();
    Con.Open();
    Command.CommandText = "Insert into Table1(type, from, to, depart, arrival, remain) Values (@type, @from, @to, @depart, @arrival, @remain);      
    TimeSpan time = new TimeSpan(10, 7, 00);
    DateTime t = new DateTime(2016, 5, 5, 5, 4, 3);
    command.Parameters.AddWithValue("@type", "test");
    command.Parameters.AddWithValue("@from", "tet");
    command.Parameters.AddWithValue("@to", "te");
    command.Parameters.AddWithValue("@depart", t);
    command.Parameters.AddWithValue("@arrival", t);
    command.Parameters.AddWithValue("@remain", 4);
    command.ExecuteNonQuery();
    Con.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.