vb.net datatable .clear method issue

michael_krell
michael_krell used Ask the Experts™
on
So i am trying to clear a datatable using the .clear() method for the table.

The issue i am running into is even though the table is cleared and there is no data in it i am getting an exception when i try to add data back into it.

Column 'DelayId' is constrained to be unique.  Value '2440' is already present.

so it seems like there is data already there, but when i check the row count before this code is executed it is 0. also i have tried .AcceptChanges() after the clear and it doesnt seem to make a difference.

I must be missing somethign stupid. Please help Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
try calling the reset method, i.e
Dim cv As DataTable = New DataTable("MES")
cv.Reset()

Open in new window

Author

Commented:
I believe when i did that it killed everything. meaning my columns were gone as well.
Commented:
Dim cv As DataTable = New DataTable("MES")
cv.Rows.Clear()
cv.AcceptChanges()

Open in new window

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
I have tried that.... sorry

Commented:
Hmmm!! Be right back. (would help if you show some code on how the table is set up)

Author

Commented:
This is the code for how the table is setup
With PopStartIDs
            .TableName = "PopStartIDs"
            .Columns.Add("DelayId")
            .PrimaryKey = New DataColumn() {.Columns("DelayID")}
            .Columns("DelayId").Unique = True

        End With

Open in new window

Author

Commented:
ok. so i may have figured it out.......

Looks like i was doing some stupid stuff with the data i was loading into it....

Commented:
stranger things have happened I suppose .... care to share so we can avoid the same?

Author

Commented:
Nothing complicated. I had a dataset that I was filling with data and using a dataadapter and .fill method. Whenever that code ran it added new records instead of replacing. I just cleared the table in the SQL dataset before running the .fill

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial