Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 4-Jan-13 23:04pm
Comments
DR Delphi at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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());
 
}
  Permalink  
Comments
Marcus Kramer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Balamurugan1989 at 17-Jan-13 5:56am
   
Ya i have mentioned in WHERE clause itself...
Daniele Rota Nodari at 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 335
1 BillWoodruff 309
2 Sergey Alexandrovich Kryukov 290
3 George Jonsson 274
4 Gihan Liyanage 230
0 OriginalGriff 4,895
1 CPallini 4,010
2 Sergey Alexandrovich Kryukov 3,514
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100