How to keep my datatableadapter insync with my dataset

Posted on 2009-04-17
Last Modified: 2013-11-26
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

Question by:dyarosh
    1 Comment
    LVL 83

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now