I have the following code in a Sub which is called by a do loop statement for
each line starting with unit info in an e-mail based game that I play. I'm
exctracting the keywords from the text and then trying to add them to a
dataset that draw's it's columnnames from an empty access database table (the
primary key of the database is UnitNo):
Dim drw1 As DataRow = dasUnitInfo.Tables("UnitInfo").NewRow()
drw1("UnitNumber") = UnitNo
drw1("UnitName") = UnitName
drw1("FactionNumber") = FactionNo
dasUnitInfo.Tables("UnitInfo").Rows.Add(drw1)
Now what happens is strange. the 1st time it hits the drw1 statement it has
the value of 1244 as the unitnumber. it seems to add the row fine (I can't
tell what info it adds as it's in th middle of the code). the next time it
hits the drw1 statement it's using 1741 as the unitnumber, however when it
tries to add the row it errors out with the message:
Colum UnitNumber is constrained to be unique. Value 1741 is already present.
Because it's in a try/catch statement it carries on and imports the
remaining rows fine. However the 1st unitnumber row isn't in the dataset.
now the only way I've found to get round this issue is by using the following
command just before the loop statement starts:
drwx("UnitNumber") = 123456
dasUnitInfo.Tables("UnitInfo").Rows.Add(drwx)
if I put the "dasUnitInfo.Tables("UnitInfo").Rows.Add(drwx) " command to
the end of the above then it does the same error but with the 1st unitnumber
not the second.
it seems like it need some sort of info in the promary key field before
it'll consider adding the rows correctly. Is there a reason for this? is
there a better way to make it work without this fix above as I feel it's not
a very neat way to fix the problem?
Any advice would be appreciated
Niels