Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to write the coding for Update and Delete button in C#.For data base i'm using Access 2007.I have tried the code but it s not working showing error the below code which i have used,
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("Update SupplierMaster SET [SupplierCode]='" +textBox2.Text + "',[SupplierName]='" +textBox1.Text + "',[SupplierCode]='" + ID + "' ", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SupplierMaster Updated Successfully");
Posted
Comments
DR Delphi 6-Jan-13 12:12pm    
What does the Error say exactly..? also I would recommend using Parameterized Query's when working with DBMS's this will eliminate any possibility of Sql Injection can you also show what the connection string is to the Access 2007 Database...

Within the SQL command you provided, SupplierCode field is specified twice.

Probably You have forgot to specify the WHERE clause, and maybe you also mistyped the last field name.

Regards,
Daniele.
 
Share this answer
 
Comments
Balamurugan1989 17-Jan-13 5:56am    
Ya i have mentioned in WHERE clause itself...
Daniele Rota Nodari 17-Jan-13 7:38am    
OK but...
In the original code, no WHERE clause was present and SupplierCode was present two times inside the SET clause.
Did the error occur due to oneor both of these things, thus fixing them the error was resolved?
If so please mark the solution as correct; otherwise let us kindly know where was the problem.
For Updating from access to C# we should follow the below code,
private void button1_Click(object sender, EventArgs e)
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
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("Update SupplierMaster SET [SupplierCode]=" +textBox2.Text + ",[SupplierName]='" +textBox1.Text + "' WHERE [SupplierCode]=" + ID + " ", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("SupplierMaster Updated Successfully");
this.SupplierMaster_Load(new object(),new EventArgs());
}
For Deleing from access to C# we should follow the below code,
private void button2_Click(object sender, EventArgs e)
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
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("Delete from SupplierMaster where [SupplierCode]=" + ID + " ", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted Successfully");
this.SupplierMaster_Load(new object(), new EventArgs());

}
 
Share this answer
 
Comments
fjdiewornncalwe 21-Feb-13 16:46pm    
My 1: SQL Injection Nightmare.
You basically did what was suggested in Solution 1, but don't return the credit to the person answering you. That's not very nice.

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