473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Insert Into Access Database - Primary Key

29
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
Sep 3 '08 #1
2 1958
madankarmukta
308 256MB
what is this dgvComputers.. you take help of ..?

Thanks
Sep 3 '08 #2
cday119
29
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.
Sep 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
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...
14
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...
1
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,...
6
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...
4
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...
3
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...
3
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...
8
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.