2

Sorry if this isn't the place to post this, but I don't know anywhere else to find help. The insert command won't work, might be something dumb but I can't find what's causing the problem, it says that it's a syntax error.

The code:

conn.Open();
            OleDbCommand cmd = new OleDbCommand("Insert into Partes(User, Título, Fecha, Link, CODEMP, CODFOR, NROFOR) " +
                "Values (@user, @titulo, @fecha, @link, @codemp, @codfor, @nrofor)", conn);
            cmd.Parameters.AddWithValue("user", Convert.ToString(Session["User"]));
            cmd.Parameters.AddWithValue("titulo", title.Text);
            cmd.Parameters.AddWithValue("fecha", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            cmd.Parameters.AddWithValue("link", link.Text);
            cmd.Parameters.AddWithValue("codemp", codemp.Text);
            cmd.Parameters.AddWithValue("codfor", codfor.Text);
            cmd.Parameters.AddWithValue("nrofor", nrofor.Text);
            cmd.ExecuteNonQuery();
  System.Data.OleDb.OleDbException
  HResult=0x80040E14
  Mensaje = Error de sintaxis en la instrucción INSERT INTO.
  Origen = <No se puede evaluar el origen de la excepción>
  Seguimiento de la pila:
<No se puede evaluar el seguimiento de la pila de excepciones>

"Syntax error in the INSER INTO instruction." "The exception stack trace can't be evaluated."

mason
  • 31,774
  • 10
  • 77
  • 121
  • maybe it's not "Título" but "Titulo" ? – Rui Caramalho Jul 22 '19 at 13:18
  • Already tried that, but it's not the problem. The field is "Título" in the database and I have other fields as "Descripción" that work. – Sebastian Braga Jul 22 '19 at 13:20
  • Try to pass a fixed value in User or in the others ("user", "randomvalue"); to see if that's the problem. Debug line by line and see where it crashes (Insert a breakpoint (F9) and while running press F10 to iterate trough); Check the constraints on sql; – TheOne__ Jul 22 '19 at 13:29
  • Have you done inserts before with SQL-style parameters `@`? It should work but I don't know if it works on older versions of Access. If yours is not old, nevermind. – wazz Jul 22 '19 at 13:40
  • Tried with fixed values but I got nothing. Checked the constraints too, they are all short text fields and datetime for "fecha", they don't have other restrictions, all the required fields are in the clause. The problem is in the "cmd.ExecuteNonQuery();" line – Sebastian Braga Jul 22 '19 at 13:41
  • @wazz yes, I did this in this project already and it works fine. – Sebastian Braga Jul 22 '19 at 13:42
  • Just for fun try moving the `conn.Open()` to one line before `cmd.ExecuteNonQuery()`. – wazz Jul 22 '19 at 13:44
  • cmd.Parameters.Add("@Parameter", SqlDbType.DateTime).Value = MyDateTimeVariable; try to add like this. more information [here](https://stackoverflow.com/a/10946437/10191493) and [here](https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) – TheOne__ Jul 22 '19 at 13:44
  • Tried moving `conn.Open()`, noting happenned. Also tried the cmd.Parameters.Add but did nothing. In the other insert clause the date parameter is the same as in this one and works fine. – Sebastian Braga Jul 22 '19 at 14:00
  • 1
    hmm... try to rename User column name or enclose it with square brackets like this [User] bc it's a reserved keyword for MSAccess. if not try [this](https://stackoverflow.com/a/20844530/10191493) - (You have to add the parameters in the order you want them.) – TheOne__ Jul 22 '19 at 14:08
  • Are you closing the connection after each insert? Access will lock the table when you do the insert and possibly will stay locked if the connection is not terminated. – B. Seberle Jul 22 '19 at 14:09
  • @TheOne__ that was the problem. "User" is a reserved word for MS Access. It was working for the select and update commands but not for the insert. Changed it to "Usuario" and now everything worked. Thank you. – Sebastian Braga Jul 22 '19 at 14:25
  • @SebastianBraga awesome! it's good to know :) Please mark my comment as usefull by clicking on the up arrow, so we can help more people! – TheOne__ Jul 22 '19 at 14:33

0 Answers0