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

vb.net datatable .clear method issue

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
0
michael_krell
Asked:
michael_krell
  • 5
  • 4
1 Solution
 
nepaluzCommented:
try calling the reset method, i.e
Dim cv As DataTable = New DataTable("MES")
cv.Reset()

Open in new window

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

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
michael_krellAuthor Commented:
I have tried that.... sorry
0
 
nepaluzCommented:
Hmmm!! Be right back. (would help if you show some code on how the table is set up)
0
 
michael_krellAuthor 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

0
 
michael_krellAuthor 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....
0
 
nepaluzCommented:
stranger things have happened I suppose .... care to share so we can avoid the same?
0
 
michael_krellAuthor 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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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