CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2011
    Posts
    2

    [RESOLVED] Update Database Records

    I'm trying to get a C# program to interface with a MS access database and so far everything has worked perfectly. But now I have stumbled up a problem while trying to update an existing record.

    The code I have is

    Code:
                string Lname = Client_LName_Field.Text;
                string Fname = Client_FName_Field.Text;
                string Address = Client_StAddress_Field.Text;
                string Suburb = Client_Suburb_Field.Text;
                string City = Client_City_Field.Text;
                string Phone = Client_Phone_Field.Text;
    
                //DataRow updateRow = DB.dsClient.Tables["T_Client"].Rows[foundId];
                DataRow updateRow = DB.dtClient.Rows[foundId];
    
                updateRow["LastName"] = Lname;
                updateRow["FirstName"] = Fname;
                updateRow["StreetAddress"] = Address;
                updateRow["Suburb"] = Suburb;
                updateRow["City"] = City;
                updateRow["PhoneNumber"] = Phone;
    
    
                DB.daClient.Update(DB.dsClient, "T_Client");
    Where first set of variables is where the updated values are coming from.
    The commented out line was a way I got the row to update but found the line below it was much more simpler to use.

    the next block of code is for the actual update where the values are placed in the old row.

    and the last line is the the update of the database.

    DB is the class where all the database variables are stored.
    daClient is the data adapter for the Client table in the database
    dsClient is the data set for the client table
    dtClient is the data table for the client table in the database
    foundID id the position number of the row to edit
    T_Client is the name of the client table in the database

    What the code does is it updates the loaded database data that is running in the memory while the program is running but does not update the actual stored database in the file on the HDD so the changes to the database only exist while the program is running.

    Any ideas as to what the problem is or how to fix it would be appreciated.

    Thanks

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Update Database Records

    from the code you've provided I can't tell what the problem is. I also don't know how you're connecting- ADO.NET, old ADO, DAO, etc.

    I highly reccomend Linq to SQL- look into it because it makes working with databases much, much easier.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Dec 2009
    Posts
    109

    Re: Update Database Records

    Quote Originally Posted by arweth View Post
    I'm trying to get a C# program to interface with a MS access database and so far everything has worked perfectly. But now I have stumbled up a problem while trying to update an existing record.

    The code I have is

    Code:
                string Lname = Client_LName_Field.Text;
                string Fname = Client_FName_Field.Text;
                string Address = Client_StAddress_Field.Text;
                string Suburb = Client_Suburb_Field.Text;
                string City = Client_City_Field.Text;
                string Phone = Client_Phone_Field.Text;
    
                //DataRow updateRow = DB.dsClient.Tables["T_Client"].Rows[foundId];
                DataRow updateRow = DB.dtClient.Rows[foundId];
    
                updateRow["LastName"] = Lname;
                updateRow["FirstName"] = Fname;
                updateRow["StreetAddress"] = Address;
                updateRow["Suburb"] = Suburb;
                updateRow["City"] = City;
                updateRow["PhoneNumber"] = Phone;
    
    
                DB.daClient.Update(DB.dsClient, "T_Client");
    Where first set of variables is where the updated values are coming from.
    The commented out line was a way I got the row to update but found the line below it was much more simpler to use.

    the next block of code is for the actual update where the values are placed in the old row.

    and the last line is the the update of the database.

    DB is the class where all the database variables are stored.
    daClient is the data adapter for the Client table in the database
    dsClient is the data set for the client table
    dtClient is the data table for the client table in the database
    foundID id the position number of the row to edit
    T_Client is the name of the client table in the database

    What the code does is it updates the loaded database data that is running in the memory while the program is running but does not update the actual stored database in the file on the HDD so the changes to the database only exist while the program is running.

    Any ideas as to what the problem is or how to fix it would be appreciated.

    Thanks
    Why don't you try creating the Update command by yourself? That might work. Create an OledbCommand object and write an Update statement in the commandstring property of the object.

    Like so:

    Code:
    string cmdstr = "....." //Write your Update statement here
    OledbCommand cmd = new OledbCommand(cmdstr) //Don't forget to specify the connection to use
    cmd.ExecuteNonQuery();
    Once it's executed, if there aren't any errors, your db's surely updated. Go back and check the db to verify that it's been updated.

    Lemme know if it works.

  4. #4
    Join Date
    Oct 2011
    Posts
    2

    Re: Update Database Records

    Thanks for the suggestions I managed to find a way to do it don't know if it was the best solution but it works so I'm happy with it now.

    If you wanted to know the code I ended up using was:

    Code:
                DataRow updateRow = DB.dtClient.Rows[foundId];
                //Begin update
                updateRow.BeginEdit();
    
                //Update details
                updateRow["LastName"] = Lname;
                updateRow["FirstName"] = Fname;
                updateRow["StreetAddress"] = Address;
                updateRow["Suburb"] = Suburb;
                updateRow["City"] = City;
                updateRow["PhoneNumber"] = Phone;
    
                //End edit
                updateRow.EndEdit();
    
                //Update Database
                DB.daClient.Update(DB.dsClient, "T_Client");
    So it seems to make it work well it needed the row.BeginEdit and row.EndEdit methods to run before the actual update takes place.

  5. #5
    Join Date
    Dec 2009
    Posts
    109

    Re: Update Database Records

    Hmmm. I've never actually used BeginEdit() before. Will try using it some time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured