Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Can't add data to a dataset based on an Access query with an AutoNumber Primary Key

Can't add data to a dataset based on an Access query with an AutoNumber Primary Key

Scheduled Pinned Locked Moved .NET (Core and Framework)
databaseworkspacecsharpquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter R Fletcher
    wrote on last edited by
    #1

    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)
    Next

    The 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?

    P 1 Reply Last reply
    0
    • P Peter R Fletcher

      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)
      Next

      The 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?

      P Offline
      P Offline
      Peter R Fletcher
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups