Adding information to a dataset...

G

Guest

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
 
G

Guest

Mistake in the earlier code:

drwx("UnitNumber") = 123456
dasUnitInfo.Tables("UnitInfo").Rows.Add(drwx)

should have read:

Dim drwx As DataRow = dasUnitInfo.Tables("UnitInfo").NewRow()
drwx("UnitNumber") = 123456

Niels
 
G

Guest

I've found another way round it too?!?

When the program launches the datatable is active and has the focus. there
is a flashing cursor in the unitnumber field which has a value of (null). now
if I click on a different datatable then it imports fine - the error only
happens if the datatable that holds the unitinfo is selected. Why would this
happen, and why wouldn't it have the same effect on the information being
imported to the alternate datatable? the information in the second datatable
also had a primary key and the table structure is pulled from a blank access
table

I'm confused! :-o

Niels
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top