Click here to Skip to main content
16,012,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to insert a data from C# to Access.I'm using access 2007.
I have tried the following codebut it is not working,
C#
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.12.0;Data Source=D:\\weightbridge.mdb;Persist Security Info=False");
            con.Open();
            cmd = new OleDbCommand("Insert into SupplierMaster Values('"+comboBox1.SelectedItem+"','"+textBox1.Text+"')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Request Sent Successfully");

Regards
Balamurugan
Posted
Updated 3-Jan-13 17:50pm
v2
Comments
Sandeep Mewara 3-Jan-13 23:51pm    
Elaborate 'not working'. Is there any error? What do you see when you DEBUG?
Balamurugan1989 4-Jan-13 0:09am    
I have used the below code i'm not getting error in coding but it s not saving in access.

string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO SupplierMaster VALUES('" +comboBox1.SelectedItem + "','" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Supplier Master Details Saved Successfully");

This code is horrible. Read up on SQL Injection to see how I can use your form to erase your database. Either way, you need to tell us the error message if you want us to help you. Your code looks fine, assuming the table only has two fields that can be inserted. I'd add the SQL to list what columns you want to insert in to, and then if that does not help, read the error message ( we are going to tell you that it means what it says ) and post it here if you still don't get it ( edit your post )
 
Share this answer
 
Comments
Balamurugan1989 4-Jan-13 0:09am    
I have used the below code i'm not getting error in coding but it s not saving in access.

string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO SupplierMaster VALUES('" +comboBox1.SelectedItem + "','" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Supplier Master Details Saved Successfully");
Christian Graus 4-Jan-13 0:29am    
You gave me a two vote, but didn't bother to do anything I suggested ? What is the format of the table ?
if (dr != null && dr.HasRows)
{
//dbReader.Read() will return true until there are no more rows to be read
while (dr.Read())
{
listBox1.Items.Add(dr["SupCode"]);
}
}
 
Share this answer
 
private void button5_Click(object sender, EventArgs e)
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\weightbridge.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO SupplierMaster VALUES('" + textBox2.Text + "','" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SupplierMaster Saved Successfully");

}
 
Share this answer
 
v2
Comments
fjdiewornncalwe 21-Feb-13 16:45pm    
My 1: SQL Injection Nightmare.

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