Hi,

I am trying to make a program that allows one to see a database (MS Access) and add items to the database.
The database works perfectly (update and suchs) but the adding won't, sadly enough.
The error I get is (translated as i'm using a Dutch version): Syntaxisfault in characterline. in query 'item);
Item is the thing that needs to be added.
The code below uses a typeString which is the choice you have to make with a combobox.
Both options won't work, because of the same reason, even though they are different.

string SQLString = "";
if (typeString == "Usable")
{
   SQLString = "INSERT INTO Items(Naam) VALUES('" + Item.Replace("'", Item) + ");";
}
else
{
   if (typeString == "Combo")
   {
       SQLString = "INSERT INTO Combo(Naam) VALUES('" + Item.Replace("'", "''") + ");";
   }
   else
   {
       MessageBox.Show("You need to select item type!");
   }
}

Recommended Answers

All 6 Replies

In line 4, you have

SQLString = "INSERT INTO Items(Naam) VALUES('" + Item.Replace("'", Item) + ");";

should it be

SQLString = "INSERT INTO Items(Naam) VALUES('" + Item.Replace("'", "''") + ");";

like you have later in the code?

Hi,

I am trying to make a program that allows one to see a database (MS Access) and add items to the database.
The database works perfectly (update and suchs) but the adding won't, sadly enough.
The error I get is (translated as i'm using a Dutch version): Syntaxisfault in characterline. in query 'item);
Item is the thing that needs to be added.
The code below uses a typeString which is the choice you have to make with a combobox.
Both options won't work, because of the same reason, even though they are different.

string SQLString = "";
if (typeString == "Usable")
{
   SQLString = "INSERT INTO Items(Naam) VALUES('" + Item.Replace("'", Item) + ");";
}
else
{
   if (typeString == "Combo")
   {
       SQLString = "INSERT INTO Combo(Naam) VALUES('" + Item.Replace("'", "''") + ");";
   }
   else
   {
       MessageBox.Show("You need to select item type!");
   }
}

See the bolded line.

Oh, it's because you forgot the ending apostrophe. Your statments both end in "Values('" + .... + ")" and both should read "Values('" + ... + "')" But either way, "Item.Replace("'",Item)" isn't going to work.

Tried and it worked :D
Many thanks to you (will put this thread on solved)

i have this code, but dont word, i dont know why, please help me

string cadena = "insert into Huespedes_V "
                +"([No], [Nombre], [Apellidos], [No_Identidad], [No_Cuarto], [Fecha_Entrada], [Fecha_Salida], [Acompanantes], [Dias_Hospedado], [Cuota_Persona], [Total_Cobro], [No_Recibo], [Fecha_Recibo]) "
            +@"values No, Nombre, Apellidos, No_identidad, No_Cuarto, Fecha_Entrada, Fecha_Salida, Acompanantes, Dias_Hospedado, Cuota_Persona, Total_Cobro, No_Recibo, @Fecha_Recibo)";

ocomando = new OleDbCommand(cadena, oconectar); 
ocomando.Parameters.Add(new OleDbParameter("@No", OleDbType.Integer));
            ocomando.Parameters.Add(new OleDbParameter("@Nombre", OleDbType.VarChar, 20));
            ocomando.Parameters.Add(new OleDbParameter("@Apellidos", OleDbType.VarChar, 20));
            ocomando.Parameters.Add(new OleDbParameter("@No_Identidad", OleDbType.Double));
            ocomando.Parameters.Add(new OleDbParameter("@No_Cuarto", OleDbType.Integer));
            ocomando.Parameters.Add(new OleDbParameter("@Fecha_Entrada", OleDbType.VarChar, 20));
            ocomando.Parameters.Add(new OleDbParameter("@Fecha_Salida", OleDbType.VarChar, 20));
            ocomando.Parameters.Add(new OleDbParameter("@Acompanantes", OleDbType.Integer));
            ocomando.Parameters.Add(new OleDbParameter("@Dias_Hospedado", OleDbType.Integer));            
            ocomando.Parameters.Add(new OleDbParameter("@Cuota_Persona", OleDbType.Integer));
            ocomando.Parameters.Add(new OleDbParameter("@Total_Cobro", OleDbType.Integer));
            ocomando.Parameters.Add(new OleDbParameter("@No_Recibo", OleDbType.Integer));
            ocomando.Parameters.Add(new OleDbParameter("@Fecha_Recibo", OleDbType.VarChar, 20));

            ocomando.Parameters["@No"].Value = h.No_Orden;
            ocomando.Parameters["@Nombre"].Value = h.Nombre;
            ocomando.Parameters["@Apellidos"].Value = h.Apellidos;
            ocomando.Parameters["@No_Identidad"].Value = h.CI;
            ocomando.Parameters["@No_Cuarto"].Value = h.No_Habitacion;
            ocomando.Parameters["@Fecha_Entrada"].Value = h.Fecha_Entrada;
            ocomando.Parameters["@Fecha_Salida"].Value = h.Fecha_Salida;
            ocomando.Parameters["@Acompanantes"].Value = h.Cant_Acompanantes;
            ocomando.Parameters["@Dias_Hospedado"].Value = h.Dias_Hospedado;
            ocomando.Parameters["@Cuota_Persona"].Value = h.Cuota_Persona;
            ocomando.Parameters["@Total_Cobro"].Value = h.Total_Cobrar;
            ocomando.Parameters["@No_Recibo"].Value = h.No_Cobrado;
            ocomando.Parameters["@Fecha_Recibo"].Value = h.Fecha_Cobrado;

            ocomando.Connection.Open();
            ocomando.ExecuteNonQuery();
            ocomando.Connection.Close();

You should change:

values No, Nombre, Apellidos, No_identidad, No_Cuarto, Fecha_Entrada, Fecha_Salida, Acompanantes, Dias_Hospedado, Cuota_Persona, Total_Cobro, No_Recibo, @Fecha_Recibo)";

to

values (@No, @Nombre, @Apellidos, @No_identidad, @No_Cuarto, @Fecha_Entrada, @Fecha_Salida, @Acompanantes, @Dias_Hospedado, @Cuota_Persona, @Total_Cobro, @No_Recibo, @Fecha_Recibo)";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.