Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
connection.Open();
               OleDbCommand command = new OleDbCommand();
               command.Connection = connection;
               command.CommandText = "insert into tblcustomer where customername='" + txt_customername.Text + "' and Name='" + txt_Name.Text + "' and Surname='" + txt_Surname.Text + "'";

               command.ExecuteNonQuery();
               MessageBox.Show("Data Saved");
               connection.Close();


What I have tried:

i have add new data but he not save my access table so please help me
Posted
Updated 23-Oct-20 23:27pm

Are you sure you need a WHERE clause on an INSERT statement? Also, do not use string concatenation to build SQL commands, it can lead to the loss of your data; see bobby-tables.com: A guide to preventing SQL injection[^].

Also, do not display messages like "Data Saved", when you are not checking the results of your SQL commands.
 
Share this answer
 
v2
Comments
Maciej Los 8-May-18 3:54am    
Good point!
C#
command.CommandText = "insert into tblcustomer where customername='" + txt_customername.Text + "' and Name='" + txt_Name.Text + "' and Surname='" + txt_Surname.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
 
Comments
Maciej Los 8-May-18 3:54am    
5ed!
Patrice T 8-May-18 6:10am    
Thank you
First of all, all notes about SqlInjection are very important! So, please, read about it before you start doing anything.

Instead of such of statement:
command.CommandText = "insert into tblcustomer where customername='" + txt_customername.Text + "' and Name='" + txt_Name.Text + "' and Surname='" + txt_Surname.Text + "'";

you should use parameterized query:
command.CommandText = "INSERT INTO tblcustomer (customername, Name, Surname)
VALUES(@custname, @name, @surname)";
command.Parameters.AddWithValue("@custname", txt_customername.Text);
command.Parameters.AddWithValue("@name", txt_Name.Text);
command.Parameters.AddWithValue("@surname", txt_Surname.Text);


For further details, please see:
OleDbParameterCollection.AddWithValue Method (String, Object) (System.Data.OleDb)[^]
 
Share this answer
 
v2
Comments
Najamdohad 8-May-18 4:30am    
command.ExecuteNonQuery();

that problem is show
Maciej Los 8-May-18 4:37am    
?
Najamdohad 8-May-18 5:22am    
Sir i type your all code same to same then i run my form so the error is come to line 53 (command.ExecuteNonQuery();
error show
(Error system.invalid operation exception. executenonquery requires an open and availabe connection.
the connction's currents state is closed.
at system.data.olebd.oledb connection.checkstateopen(stringmethod)
at system.data.olebd.oledb connection.checkstateopen(stringmethod)
at system.data.olebd.oledb connection.checkstateopen(stringmethod)
at system.data.olebd.oledb command.exeutenonquery()
at Blood.Form3.btn_save_click(object sender,Eventargs e) in
c:\users\Najam\documents\visual studio 2015\project\blood\blood\form3.cs:line53)
Maciej Los 8-May-18 5:28am    
Seems you connection is closed or unavailable. What's your connection string?
It should looks like: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;

How you do create a connection?

I suggest to use statement as:
using (OleDbConnection connection = new OleDbConnection(sConStr))
{
    connection.Open();
    //here the code responsible for command creation and execution
}<pre>
Najamdohad 8-May-18 5:40am    
yes sir i have create same code for connection
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = D:\MASTER\MASTERBLOOD.mdb;
Persist Security Info = False; ";
private void btn_add_Click(object sender, EventArgs e)
{

string sqlQuery = "INSERT INTO tblinventory(`product_code`,`product_name`,`price`,`category`,`stock`) values (?,?,?,?,?)";
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.Oledb.12.0;Data Source=db.accdb"))
using (OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
{
if ((txtproductcode.Text == "") || (txtproductname.Text == "") || (txtprice.Text == "") || (txtcategory.Text == "") || (txtquantity.Text == ""))
{
MessageBox.Show("Please fill the all fields!");
}
else {
conn.Open();
cmd.Parameters.AddWithValue("@product_code", txtproductcode.Text);
cmd.Parameters.AddWithValue("@product_name", txtproductname.Text);
cmd.Parameters.AddWithValue("@price", txtprice.Text);
cmd.Parameters.AddWithValue("@category", txtcategory.Text);
cmd.Parameters.AddWithValue("@stock", txtquantity.Text);
MessageBox.Show("Successfully Added!");

txtproductcode.Clear();
txtproductname.Clear();
txtprice.Clear();
txtquantity.Clear();
txtcategory.ResetText();
cmd.ExecuteNonQuery();

}
}
}
 
Share this answer
 
Comments
CHill60 24-Oct-20 7:37am    
An uncommented, unformatted code dump is not a good solution and this question was adequately answered over 2 years ago. Stick to answering recent questions where the OP still needs help

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