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: -
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")
-
-
-
Thanks
2 1958
what is this dgvComputers.. you take help of ..?
Thanks
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. -
-
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
-
row("RecordNum") = dsHardware.Tables("Computers").Compute("Max(RecordNum)", Nothing) + 1
-
dsHardware.Tables("Computers").Rows.Add(row)
-
MsgBox(row("RecordNum").ToString)
-
MsgBox(dsHardware.Tables("Computers").Rows(dsHardware.Tables("Computers").Rows.Count - 1)("RecordNum").ToString)
-
-
-
'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.GetInsertCommand()
-
daComputers.InsertCommand = query.GetInsertCommand
-
-
'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")
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: For example John Smith |
last post by:
I'm doing a data transfer from Access to SQL Server, I wish to keep the
identity column (autonumber) values as all the data is already related. I
tried the first table append query including the...
|
by: serge |
last post by:
I have a scenario where two tables are in a One-to-Many relationship
and I need to move the data from the Many table to the One table so
that it becomes a One-to-One relationship.
I need to...
|
by: Christian Soltenborn |
last post by:
Hi experts,
I am working on a university project based on VB .NET. I have a
connection to MS Access, and I want to insert some data into the
database. The sql command
INSERT INTO Shoe (Brand,...
|
by: efgh |
last post by:
I'm fairly confident in my knowledge of SQL but I'm stumped with regards
with an Insert Into statement in Access 2003. I've got a 4 column table
that I've simplified about as much as I can, no...
|
by: authorking |
last post by:
I use the following code to insert a data record in to a datatable of an
access database.But every time I execute the command, there will rise an
exception and the insert operation can't be...
|
by: teddysnips |
last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!)
but I can't get it to work because it references ntext fields.
Is there any alternative? I could write it in laborious code...
|
by: evandela |
last post by:
I am having pretty much of the same problem but with an access
Database. When I insert a new product, I need to update a second
reference table with the primary key of the new insert. I have...
|
by: nano2k |
last post by:
Hi
Shortly, I keep invoices in a table.
Occasionally, someone will fire the execution of a stored procedure
(SP) that performs several UPDATEs against (potentially) all invoices
OLDER than a...
|
by: mark mestrom |
last post by:
Hi all, i have a problem.
In my access-application i am trying to insert a record in a linked table with resides in a sqlserver 2000 database. However, the primary key in that table is an INT with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |