Click here to Skip to main content
15,064,665 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Database/registration.accdb");
using (OleDbConnection con = new OleDbConnection(ConnectionString))
{
    con.Open();

    OleDbDataAdapter SQLAdapter = new OleDbDataAdapter("insert into tblproducts([pname],[pprice],[pselprice],[pcatid],[psubid],[psize],[pmaterial],[pcolor],[pdimension],[pitemweight],[pmaxweight],[pdescription],[pproductdetails],[freedelivery],[30dayreturn],[cod]) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','" + DropDownList3.SelectedItem.Value + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + DropDownList4.SelectedItem.Value + "','" + DropDownList5.SelectedItem.Value + "','" + DropDownList6.SelectedItem.Value+ "')", con);
        DataTable DT = new DataTable();
        SQLAdapter.Fill(DT);
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        DropDownList1.SelectedItem.Value ="";
        DropDownList2.SelectedItem.Value ="";
        DropDownList3.SelectedItem.Value ="";
        TextBox7.Text = "";
        TextBox8.Text = "";
        TextBox9.Text = "";
        TextBox10.Text = "";
        TextBox11.Text = "";
        TextBox12.Text = "";
        TextBox13.Text = "";
        DropDownList4.SelectedItem.Value = "";
        DropDownList5.SelectedItem.Value = "";
        DropDownList6.SelectedItem.Value = "";
        TextBox1.Focus();
        Response.Write("Congratulations, Product Added Successfully!!!");

    }


What I have tried:

correct me if there is any error in this code other than name of columns
Posted
Updated 19-Aug-21 5:08am
Comments
Richard MacCutchan 19-Aug-21 11:06am
   
Apart from the fact that you are creating your SQL command totally the wrong way*, you did not tell us what the error is.

*as already explained in two out of your three previous questions.

There's one massive error in your code: it is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]


Beyond that, using the default names provided by the designer is a mistake. You should always provide meaningful names for your controls, otherwise you'll forget which control represents which value.

And using Response.Write is almost always wrong - the value will be sent to the user outside of the normal page output.

However, since you haven't explained what problem you're facing, we can't tell you any more than that.
   
There is a huge problem in your code, it is 'SQL Injection" :
C#
OleDbDataAdapter SQLAdapter = new OleDbDataAdapter("insert into tblproducts([pname],[pprice],[pselprice],[pcatid],[psubid],[psize],[pmaterial],[pcolor],[pdimension],[pitemweight],[pmaxweight],[pdescription],[pproductdetails],[freedelivery],[30dayreturn],[cod]) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','" + DropDownList3.SelectedItem.Value + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + DropDownList4.SelectedItem.Value + "','" + DropDownList5.SelectedItem.Value + "','" + DropDownList6.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[^]
   

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