Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

How to keep my datatableadapter insync with my dataset

I am using Visual Studio and VB.NET to create an application that connects to an MS Access database.  I have a function that inserts a new employee.  In the Load Event of my form I fill my employees table in my dataset.  There is a button on the formt that is clicked to do the insert.

I have included the insert code under CLICK EVENT.  Basically, I add a new row to my employees table, use a datarow to set the values, call the Add method of my emjployee table to add the new row and then call the Update method of the employee tableadapter to update the database.

Everything works as expected except one thing.  The EmployeeRow has a unique id, EmployeeID, that is an autoincrement field.  After the insert, the number of employees in both the employee table and employee table adapter increment by 1.  However, in the employee table, the EmployeeID is -1 while in the database the EmployeeID of the new record is 10108.  How can I get my dataset to reflect the correct EmployeeID?  Do I need to unload the table and reload it or is there an easier way?
                    EmployeeRow = PfmsDataSet1.Employees.NewRow()
                            EmployeeRow.EmployerID = 5  ' Set to Fair Share
                            EmployeeRow.SocialSecurityNumber = ec.SSN
                            EmployeeRow.FirstName = ec.FirstName
                            EmployeeRow.LastName = ec.LastName
                            EmployeeRow.HireDate = ec.DOH
                            EmployeeRow.ActiveDateinPlan = ec.DOH
                            EmployeeRow.EmployeeStatus = ec.EmployeeStat
                            EmployeeRow.PlanStatus = ec.PlanStat
                            EmployeeRow.EmployeeType = ec.EmployeeType
                            EmployeeRow.State = "PA"
                            EmployeeRow.Sex = "UNKNOWN"
                            EmployeeRow.MaritalStatus = "UNKNOWN"

Open in new window

1 Solution
The easiest way is to reload the dataset because the column would be readonly because of being autoincrement.

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now