:eek: Hi,

Im having problems in adding new records in a database connected to .net. The data type of ID in access is NUMBER. When I click SAVE it shows this error message: "Column 'ID' is constrained to be unique. Value '1' is already present."

Here is the code for SAVE..
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If inc <> -1 Then
NavigateRecords()
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("HostingDB").NewRow()
dsNewRow.Item("ID") = txtCiD.Text
dsNewRow.Item("CName") = txtCName.Text
dsNewRow.Item("PCont") = txtPCont.Text

Try
ds.Tables("HostingDB").Rows.Add(dsNewRow)
Catch ex As Exception
MsgBox(ex.Message)
End Try

da.Update(ds, "HostingDB")
MsgBox("New Record added to the Database")
End If
End Sub


Thanks in advance!

Hi,
As the Column 'ID' is unique, before inserting giv a check for existing ID and proceed with inserting to the database. or else create the ID as autonumber and insert the rest columns. so with this, access with increment the ID field with 1 and you can add the column without any error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.