Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to save a record BOOK_NAME value "Mit'in gizli tarihi" in this value it gives error "In operator without () in query expression "Mit'in......('. " while saving record. Because of operator " ' "

But I can save this record from directly mdb file. why I can't save from windows form.

string query = "Insert INTO BOOKS(BOOK_NAME,TYPE,AUTHOR,PUBLISH_YEAR,PUBLISH_COMPANY,INFO) VALUES ('" + txtBookName.Text  + "', '"
                    + comboTypes.Text + "', '" +  txtBookAuthor.Text+ "', '" +txtPunlishYear.Text  + "', '" +txtPublishCo.Text  + "','"+ txtInfo.Text +"' )";

OleDbCommand Command = new OleDbCommand(query, dbConnection);

 if (dbConnection.State.ToString() == "Closed")
                    dbConnection.Open();
Command.ExecuteReader();

dbConnection.Close();
Posted

Use parameters[^] instead of string concatenation.

Regards
Espen Harlinn
 
Share this answer
 
The problem is that the BOOK_NAME has an apostrophe (single quote) in it, which is the SQL string delimiter, causing the rest of your insert to be misaligned. To escape the default behavior you need to replace the single quote ' with two single quotes '' on any strings you insert into the database. For example:
"... '" + txtBookName.Text.Replace("'", "''") + "' ..."

However, using parameters as Espen Harlinn recommended, is safer and will do this automatically for you.
 
Share this answer
 
Thank you guys.

@JOAT-MON

this is a very quick fix.

@Espen Harlinn

Thank you.

I will study about Parameters.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900