ponki.d.monkey Posted February 14, 2009 Share Posted February 14, 2009 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 More sharing options...
ponki.d.monkey Posted February 14, 2009 Author Share Posted February 14, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.