J
JoWilliam
Hi,
I've written a VB .Net program (written in Visual Studio .Net 2003) which
scrapes data from a web site and stores it in an Access database. The
database has 1 table with 11 columns, one of which is the primary key (not
an auto-increment field).
The program first calls OleDbDataAdapter.Fill to populate a DataSet with 6
of the columns from the Access db. It then adds data from the web page to
this DataSet, using DataTable.Rows.Add. When it has parsed the whole page
(20 records), it calls OleDbDataAdapter.Update with the DataSet and then
continues with the next web page.
This works fine when I start with an empty Access database. However, when I
run the program again (on the same web page and the data on it hasn't
changed), each call to the Update method causes an exception which I trap:
System.Data.OleDb.OleDbException: The changes you requested to the table
were not successful because they would create duplicate values in the index,
primary key, or relationship. Change the data in the field or fields that
contain duplicate data, remove the index, or redefine the index to permit
duplicate entries and try again.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
I'd rather than not use a Try... Catch... End Try block to prevent this
exception from crashing my program, and reckon that the Update method should
be able to work out which rows have changed and which haven't. Starting
with an empty database, on the 2nd run of the program the dataset contains
158 records. The first 79 of these are the ones read from the Access db
using the initial .Fill call and have a RowState of Unchanged; the next 79
are the ones read from the web site (identical to the first 79) and have a
RowState of Added. This is obviously where the problem originates and I
think the DataSet should only contain 79 records since the primary keys of
the first 79 are identical to the second 79, but somehow maybe I haven't
told the DataAdapter which is the primary key column so it can't resolve the
dataset.
I must have missed something or coded something incorrectly (this is my
first OleDb program!), so would appreciate any advice.
thanks, JW
I've written a VB .Net program (written in Visual Studio .Net 2003) which
scrapes data from a web site and stores it in an Access database. The
database has 1 table with 11 columns, one of which is the primary key (not
an auto-increment field).
The program first calls OleDbDataAdapter.Fill to populate a DataSet with 6
of the columns from the Access db. It then adds data from the web page to
this DataSet, using DataTable.Rows.Add. When it has parsed the whole page
(20 records), it calls OleDbDataAdapter.Update with the DataSet and then
continues with the next web page.
This works fine when I start with an empty Access database. However, when I
run the program again (on the same web page and the data on it hasn't
changed), each call to the Update method causes an exception which I trap:
System.Data.OleDb.OleDbException: The changes you requested to the table
were not successful because they would create duplicate values in the index,
primary key, or relationship. Change the data in the field or fields that
contain duplicate data, remove the index, or redefine the index to permit
duplicate entries and try again.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
I'd rather than not use a Try... Catch... End Try block to prevent this
exception from crashing my program, and reckon that the Update method should
be able to work out which rows have changed and which haven't. Starting
with an empty database, on the 2nd run of the program the dataset contains
158 records. The first 79 of these are the ones read from the Access db
using the initial .Fill call and have a RowState of Unchanged; the next 79
are the ones read from the web site (identical to the first 79) and have a
RowState of Added. This is obviously where the problem originates and I
think the DataSet should only contain 79 records since the primary keys of
the first 79 are identical to the second 79, but somehow maybe I haven't
told the DataAdapter which is the primary key column so it can't resolve the
dataset.
I must have missed something or coded something incorrectly (this is my
first OleDb program!), so would appreciate any advice.
thanks, JW