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.Upd ate(dsHardware, "Computers" ) line
Here is the code:
Thanks
Here is the code:
Code:
Dim row As DataRow = dsHardware.Tables("Computers").NewRow
row("Style") = cmbAddStyle.SelectedItem.Key.ToString
row("Location") = cmbAddLocation.SelectedItem.key.ToString
row("Sys_ID") = txtAddName.Text
row("Assignment") = txtAddOwner.Text
row("Dell_Tag") = txtAddTag.Text
row("Processor") = txtAddProcessor.Text
row("RAM") = txtAddRam.Text
row("Hard_Drive") = txtAddHardDrive.Text
row("Windows_OS") = txtAddos.Text
row("Role") = txtAddRole.Text
row("Netware") = chkAddNetware.Checked
dsHardware.Tables("Computers").Rows.Add(row)
'Update the dataset to the values of the textboxes
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Sys_ID") = txtDeviceName.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Assignment") = txtOwner.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Dell_Tag") = txtServiceTag.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("RecordNum") = DBNull.Value
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Processor") = txtProcessor.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("RAM") = txtRAM.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Hard_Drive") = txtHardDrive.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Windows_OS") = txtOS.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Role") = txtRole.Text
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Decommissioned") = cbxDecomm.Checked
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Netware") = cbxNetware.Checked
'Update the dataset to the values of the comboboxes
If Not cmbLocation.SelectedIndex = -1 Then
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Location") = cmbLocation.SelectedItem.Key.ToString
End If
If Not cmbStyle.SelectedIndex = -1 Then
dsHardware.Tables("Computers").Rows(dgvComputers.CurrentRow.Index).Item("Style") = cmbStyle.SelectedItem.Key.ToString
End If
'End the editing of the dataset so we can update the database
Me.BindingContext(dsHardware, "Computers").EndCurrentEdit()
Me.BindingContext(dsHardware, "Style").EndCurrentEdit()
Me.BindingContext(dsHardware, "Location").EndCurrentEdit()
'Create SQL Query
Dim query As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(daComputers)
query.GetUpdateCommand()
daComputers.UpdateCommand = query.GetUpdateCommand
'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
daComputers.Update(dsHardware, "Computers")
Comment