Connecting Tech Pros Worldwide Forums | Help | Site Map

Insert Into Access Database - Primary Key

Newbie
 
Join Date: Mar 2008
Posts: 29
#1: Sep 3 '08
Can someone help me out with this one. I am trying to insert a row of data into an access database. The primary key is set to auto increment. It will work once but the next time you try an insert it says. "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. " This error occurs on the daComputers.Update(dsHardware, "Computers") line

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. Dim row As DataRow = dsHardware.Tables("Computers").NewRow
  2.         row("Style") = cmbAddStyle.SelectedItem.Key.ToString
  3.         row("Location") = cmbAddLocation.SelectedItem.key.ToString
  4.         row("Sys_ID") = txtAddName.Text
  5.         row("Assignment") = txtAddOwner.Text
  6.         row("Dell_Tag") = txtAddTag.Text
  7.         row("Processor") = txtAddProcessor.Text
  8.         row("RAM") = txtAddRam.Text
  9.         row("Hard_Drive") = txtAddHardDrive.Text
  10.         row("Windows_OS") = txtAddos.Text
  11.         row("Role") = txtAddRole.Text
  12.         row("Netware") = chkAddNetware.Checked
  13.         dsHardware.Tables("Computers").Rows.Add(row)
  14.  
  15.  'Update the dataset to the values of the textboxes
  16.           dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Sys_ID") = txtDeviceName.Text
  17.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Assignment") = txtOwner.Text
  18.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Dell_Tag") = txtServiceTag.Text
  19.         dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("RecordNum") = DBNull.Value
  20.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Processor") = txtProcessor.Text
  21.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("RAM") = txtRAM.Text
  22.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Hard_Drive") = txtHardDrive.Text
  23.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Windows_OS") = txtOS.Text
  24.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Role") = txtRole.Text
  25.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Decommissioned") = cbxDecomm.Checked
  26.          dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Netware") = cbxNetware.Checked
  27.  
  28.  
  29.         'Update the dataset to the values of the comboboxes
  30.         If Not cmbLocation.SelectedIndex = -1 Then
  31.             dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Location") = cmbLocation.SelectedItem.Key.ToString
  32.         End If
  33.         If Not cmbStyle.SelectedIndex = -1 Then
  34.             dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Style") = cmbStyle.SelectedItem.Key.ToString
  35.         End If
  36.  
  37.         'End the editing of the dataset so we can update the database
  38.         Me.BindingContext(dsHardware, "Computers").EndCurrentEdit()
  39.         Me.BindingContext(dsHardware, "Style").EndCurrentEdit()
  40.         Me.BindingContext(dsHardware, "Location").EndCurrentEdit()
  41.  
  42.         'Create SQL Query
  43.         Dim query As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(daComputers)
  44.         query.GetUpdateCommand()
  45.         daComputers.UpdateCommand = query.GetUpdateCommand
  46.  
  47.         'And Update the database - If creation of location or style ever occurs wrap the updates in the sql catcher and update the database all at once
  48.         daComputers.Update(dsHardware, "Computers")
  49.  
  50.  
  51.  
Thanks

Familiar Sight
 
Join Date: Apr 2008
Posts: 149
#2: Sep 3 '08

re: Insert Into Access Database - Primary Key


what is this dgvComputers.. you take help of ..?

Thanks
Newbie
 
Join Date: Mar 2008
Posts: 29
#3: Sep 3 '08

re: Insert Into Access Database - Primary Key


A datagridview. I have been trying some other things. Here is the newest way that I am doing it. I basically changed the update command of the dataadapter to a insert command.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Dim row As DataRow = dsHardware.Tables("Computers").NewRow
  3.         row("Style") = cmbAddStyle.SelectedItem.Key.ToString
  4.         row("Location") = cmbAddLocation.SelectedItem.key.ToString
  5.         row("Sys_ID") = txtAddName.Text
  6.         row("Assignment") = txtAddOwner.Text
  7.         row("Dell_Tag") = txtAddTag.Text
  8.         row("Processor") = txtAddProcessor.Text
  9.         row("RAM") = txtAddRam.Text
  10.         row("Hard_Drive") = txtAddHardDrive.Text
  11.         row("Windows_OS") = txtAddos.Text
  12.         row("Role") = txtAddRole.Text
  13.         row("Netware") = chkAddNetware.Checked
  14.         row("RecordNum") = dsHardware.Tables("Computers").Compute("Max(RecordNum)", Nothing) + 1
  15.         dsHardware.Tables("Computers").Rows.Add(row)
  16.         MsgBox(row("RecordNum").ToString)
  17.         MsgBox(dsHardware.Tables("Computers").Rows(dsHardware.Tables("Computers").Rows.Count - 1)("RecordNum").ToString)
  18.  
  19.  
  20. 'End the editing of the dataset so we can update the database
  21.         Me.BindingContext(dsHardware, "Computers").EndCurrentEdit()
  22.         Me.BindingContext(dsHardware, "Style").EndCurrentEdit()
  23.         Me.BindingContext(dsHardware, "Location").EndCurrentEdit()
  24.  
  25.         'Create SQL Query
  26.         Dim query As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(daComputers)
  27.         query.GetInsertCommand()
  28.         daComputers.InsertCommand = query.GetInsertCommand
  29.  
  30.         'And Update the database - If creation of location or style ever occurs wrap the updates in the sql catcher and update the database all at once
  31.         daComputers.Update(dsHardware, "Computers")
  32.  
This code has the same outcome. "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. " This error occurs on the daComputers.Update(dsHardware, "Computers") line

I think it comes from the dataset or datadapter not knowing that I inserted a row, and it is creating another row with the same primary key.
Reply