Can't add data to a dataset based on an Access query with an AutoNumber Primary Key
-
I am writing in VB.NET, enhancing a working program. Part of the program involves getting data from various outside sources and saving it in tables in an Access database. I am adding a new outside source (for the first time for quite a while), and am tearing my hair out trying to get code that is working with previously created DataTables and their derivatives to work with a newly created set. I used the dataset configuration facility to create a DataTable based on a Query from my new Access table. The Access query returns all the fields of recent records in the Table and is updateable in Access. The dataset query is
SELECT PriKey, ChannelNumber, ChannelName, ReadingDate, Power FROM qryRecentSpyderData
PriKey is an AutoNumber primary key. Other than different field names, I have an apparently identical setup working without problems in another function. The failing code is:
Dim dst1 As New EnergyDataSet Dim tba1 As New EnergyDataSetTableAdapters.SpyderMinTableAdapter Dim tbl1 As New EnergyDataSet.SpyderMinDataDataTable Dim rowData() As EnergyDataSet.SpyderMinDataRow Dim rowNew As EnergyDataSet.SpyderMinDataRow
...
For intCount = intStart To 0 Step -1
rowNew = tbl1.NewSpyderMinDataRow
rowNew.ChannelNumber = intChannel
rowNew.ChannelName = minhArray(intCount).strChannel
rowNew.ReadingDate = minhArray(intCount).dtSampleTime
rowNew.Power = minhArray(intCount).sngPower
tbl1.Rows.Add(rowNew)
NextThe Rows.Add line throws a "Nulls not allowed" exception the first time through the loop. If I relax constraints, it throws a "Duplicate Primary Key" exception the second time through. I know that I haven't set the Primary Key - Access will set it in due course! I cannot see why this fails, while essentially identical code using what appears to be an exactly comparable dataset succeeds. What am I missing?
-
I am writing in VB.NET, enhancing a working program. Part of the program involves getting data from various outside sources and saving it in tables in an Access database. I am adding a new outside source (for the first time for quite a while), and am tearing my hair out trying to get code that is working with previously created DataTables and their derivatives to work with a newly created set. I used the dataset configuration facility to create a DataTable based on a Query from my new Access table. The Access query returns all the fields of recent records in the Table and is updateable in Access. The dataset query is
SELECT PriKey, ChannelNumber, ChannelName, ReadingDate, Power FROM qryRecentSpyderData
PriKey is an AutoNumber primary key. Other than different field names, I have an apparently identical setup working without problems in another function. The failing code is:
Dim dst1 As New EnergyDataSet Dim tba1 As New EnergyDataSetTableAdapters.SpyderMinTableAdapter Dim tbl1 As New EnergyDataSet.SpyderMinDataDataTable Dim rowData() As EnergyDataSet.SpyderMinDataRow Dim rowNew As EnergyDataSet.SpyderMinDataRow
...
For intCount = intStart To 0 Step -1
rowNew = tbl1.NewSpyderMinDataRow
rowNew.ChannelNumber = intChannel
rowNew.ChannelName = minhArray(intCount).strChannel
rowNew.ReadingDate = minhArray(intCount).dtSampleTime
rowNew.Power = minhArray(intCount).sngPower
tbl1.Rows.Add(rowNew)
NextThe Rows.Add line throws a "Nulls not allowed" exception the first time through the loop. If I relax constraints, it throws a "Duplicate Primary Key" exception the second time through. I know that I haven't set the Primary Key - Access will set it in due course! I cannot see why this fails, while essentially identical code using what appears to be an exactly comparable dataset succeeds. What am I missing?
Well, I have a workaround - if I delete the reference to the Primary Key field in the DataTableAdapter definition, the code works and the database does get updated properly when I .Update it from the table. Since I don't need access to the Primary Key in the VB.Net code for anything, this is acceptable, but I still don't understand why the older code, whose table definitions include the relevant Primary Keys, works.