Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
protected void Button1_Click(object sender, EventArgs e)
   {
       {
           OleDbConnection con = new OleDbConnection();
           con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Database/registration.accdb");
           con.Open();

           OleDbDataAdapter SQLAdapter = new OleDbDataAdapter("insert into size(sizename,catid,subcatid) values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Value + "','"+DropDownList2.SelectedItem.Value+"')", con);
           DataTable DT = new DataTable();
           SQLAdapter.Fill(DT);
           Response.Write("Size Added Successfully");
           TextBox1.Text = string.Empty;
           TextBox1.Text = "";
           con.Close();
           DropDownList1.ClearSelection();
           DropDownList1.Items.FindByValue("0").Selected = true;
       }
   }


What I have tried:

try to correct syntax error in the insert into statement
Posted
Updated 16-Aug-21 20:00pm

Don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app, and the syntax error will probably go away at the same time.
 
Share this answer
 
Quote:
try to correct syntax error in the insert into statement

The problem is that it is impossible to know what is wrong because of the way you build the query. Your query depends on the contains of variables including user input.
In your query, user input is promoted to code this security problem is named SQL Injection.
C#
OleDbDataAdapter SQLAdapter = new OleDbDataAdapter("insert into size(sizename,catid,subcatid) values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Value + "','"+DropDownList2.SelectedItem.Value+"')", con);

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
 

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