Click here to Skip to main content
15,902,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this my code insert in accdb using visual c# ........the column data type text,number(double,percentage) like this ...i try to fill all text box the value inserted ...

but i try to click without entering values ,receive this error .another error is if i insert single text box value ,other was empty also this error
this error
+		Execute NonQuery "insert into hhh VALUES('' ,'','','','','mmm','','','','','','','','','','','','','','','','')"	Command Text = "insert into hhh VALUES('' ,'','','','','mmm','','','','','','','','','','','','','','','','')", Connection String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\data\\ggg.accdb"	
returning number of row affected


What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
{
    cmd = new OleDbCommand("insert into hhh VALUES('" + 
        textBox1.Text + "' ,'" + textBox2.Text + "','" +
        textBox3.Text + "','" + textBox4.Text + "','" +
        textBox5.Text + "','" + textBox6.Text + "','" +
        textBox7.Text + "','" + textBox8.Text + "','" +
        textBox9.Text + "','" + textBox10.Text + "','" +
        textBox11.Text + "','" + textBox12.Text + "','" +
        textBox13.Text + "','" + textBox14.Text + "','" +
        textBox15.Text + "','" + textBox16.Text + "','" +
        textBox17.Text + "','" + textBox18.Text + "','" +
        textBox19.Text + "','" + textBox20.Text + "','" +
        textBox21.Text + "','" + textBox22.Text + "')");

    con.Open();
    int n = cmd.ExecuteNonQuery();

    if (n > 0)
    {
        MessageBox.Show("Record Submitted", "Congrats");
    }
    else
        MessageBox.Show("insertion failed");

    con.Close();
}
Posted
Updated 14-Sep-17 16:55pm
v2

Firstly, your SQL command has a syntax error. Please read up on how to use SQL commands: SQL INSERT INTO Statement[^] - TL;DR: you are not specifying the matching table fields for the data that you are inserting.

Secondly, when posting data to a DB, you should not use string concatenation, but instead use parameters to avoid SQL Injection[^] attacks. Here is the correct way: SqlCommand.Parameters Property - MSDN[^]
 
Share this answer
 
v2
Quote:
the column data type text,number(double,percentage)

You pass all values as strings, no number. Make sure of datatype of every values.

Advice: Never use automatic naming for your textboxes, instead give meaningful names, it will make changes easier in future.
C#
cmd = new OleDbCommand("insert into hhh VALUES('" +
    textBox1.Text + "' ,'" + textBox2.Text + "','" +
    textBox3.Text + "','" + textBox4.Text + "','" +
    textBox5.Text + "','" + textBox6.Text + "','" +
    textBox7.Text + "','" + textBox8.Text + "','" +
    textBox9.Text + "','" + textBox10.Text + "','" +
    textBox11.Text + "','" + textBox12.Text + "','" +
    textBox13.Text + "','" + textBox14.Text + "','" +
    textBox15.Text + "','" + textBox16.Text + "','" +
    textBox17.Text + "','" + textBox18.Text + "','" +
    textBox19.Text + "','" + textBox20.Text + "','" +
    textBox21.Text + "','" + textBox22.Text + "')");

Not a solution to your question, but another problem you have.
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[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
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