Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone,
I'm developping an application witch allows the user to fill a form with text.
Therefore I use textboxes.
I can save the data contained in the textboxes to an Access database but there is a problem when the text contains single (') or double quotes (").

How can I solve the problem ?


Precision : it is a desktop application WinForm in C#

What I have tried:

using Texbox1.Text doesn't work since the quotes appears in the SQL command and are interpreted as part of the command and not as data

here the code
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Historique_Rdv.accdb");

string command = "INSERT INTO RendezVous " +
    "(IDAnimal, NumeroRdv, DateRdv, Prestation, Temps, ComplementInfo, Paye, IDProprietaire)  " +
    "VALUES(" + int.Parse(TB_idAnimalNewRdv.Text) + ", " + int.Parse(TB_numeroRdvNouveauRdv.Text) + ", '" + TB_dateNouveauRdv.Text + "', '" + TB_actionNouveauRdv.Text + "', '" + TB_tempsNouveauRdv.Text + "', '" + TB_complementInfoNouveauRdv.Text + "', " + int.Parse(TB_payeNouveauRdv.Text) + ", " + int.Parse(TB_idProprietaireNewRdv.Text) + " )";

OleDbCommand cmdd = new OleDbCommand(command, connection);

Updated 20-Feb-24 5:13am
Richard MacCutchan 20-Feb-24 5:49am    
Without seeing any code we cannot guess what the problem is. Are you using proper parameterised queries in your SQL?

I can save the data contained in the textboxes to an Access database but there is a problem when the text contains single (') or double quotes (").

This is probably linked to the way you are trying to save the text.
You should read about SQL Injection vulnerability.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Share this answer
Alain Elshocht 20-Feb-24 12:56pm    
Thank you, tested and approved ;-)

Now I have to solve a decimal number problem ...
The reason your current code doesn't work is because you're using string concatenation to build the SQL INSERT query. NEVER DO THIS!!!

Use parameterized queries instead. This way, the double quotes in your text get escaped properly instead of being interpreted as part of your SQL statement.

Once you're done reading the links in Solution 2, Google for "C# sql parameterized queries" to find information on how to build parameter objects for your questions and put the parameter placeholders in your SQL statement.

Oh, and you'll make it much easier to debug your SQL code when you do this too.
Share this answer
Alain Elshocht 20-Feb-24 13:58pm    
thank you ... I've been wrong for so many years now ! ;-)

Self learning leads sometimes to bad learning

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