Jump to content

Update Query In Ms Access 2007


ponki.d.monkey
 Share

Recommended Posts

Hi! I have a very big problem with the Update query I created in my C# program for MS Access 2007. This one is driving me nuts. I can't make the Update statement work. All the other statements are working (such as Insert, Select and Delete), but the Update statement does not. The weird thing is when I execute the exact same query statement directly in MS Access, it works perfectly well. I can't figure out why it's not working in C#. I don't get any errors at all when I run the program or when I use the Update button in my GUI. I'm not sure if this is the right place to ask about this problem, but any help will be much appreciated. I included the code snippet below. Please take a look at it. Thanks!

public override void UpdateClient(IClient client){	   using (OleDbConnection connection = new OleDbConnection(DatabaseHelper.StringConnection))	   {				connection.Open();				using (OleDbCommand command = connection.CreateCommand())				{					command.CommandType = CommandType.Text;					command.Parameters.AddWithValue("@clientID", client.ClientID);					command.Parameters.AddWithValue("@lastName", client.LastName);					command.Parameters.AddWithValue("@firstName", client.FirstName);					command.Parameters.AddWithValue("@middleName", client.MiddleName);					command.Parameters.AddWithValue("@address", client.Address);					command.Parameters.AddWithValue("@contactNumber", client.ContactNumber);					command.Parameters.AddWithValue("@email", client.Email);					command.CommandText = "UPDATE Contact SET LastName = @lastName, " +										  "FirstName = @firstName, MiddleName = @middleName, " +										  "Address = @address, ContactNumber = @contactNumber, " +										  "Email = @email WHERE ContactID = @clientID";					command.ExecuteNonQuery();				}		}}

Link to comment
Share on other sites

Well, after a combined 4 hours of thinking and relaxing (then thinking again... :) ), I was able to solve my own problem. This may come in handy for somebody else out there who may be having the same problem like this that's why I decided to post this solution. Anyway, before I show you the code...I read before that the solution I'm about to show you is not a very good practice, but this is the only work around that I could think of (at least for now). So it's up to you to decide whether to use it or not. So good luck! Here's the code:

public override void UpdateClient(IClient client){	   using (OleDbConnection connection = new OleDbConnection(DatabaseHelper.StringConnection))	   {				connection.Open();				using (OleDbCommand command = connection.CreateCommand())				{					command.CommandType = CommandType.Text;					command.CommandText = string.Format("UPDATE Contact SET LastName = \'{0}\', " +										  "FirstName = \'{1}\', MiddleName = \'{2}\', Address = \'{3}\', " +										  "ContactNumber = \'{4}\', Email = \'{5}\' WHERE ContactID = {6}", 										  client.LastName, client.FirstName, client.MiddleName,										  client.Address, client.ContactNumber, client.Email, client.ClientID);					command.ExecuteNonQuery();				}	   }}

Oh, and if anyone else has a better idea, I'm still open to any suggestion. Thanks a lot! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...