Duplicate Record

J

Jim Heavey

I have getting an error indicating that a unique constraint has been violated
and I am not sure why this is occuring.

The sequence of events are as follows:
Bind the Listbox to a Dataview
The form is displayed and one row shows in the List box as it should
I add row or rows and they all show up just fine in the listbox.
I loop through the Dataview to ensure that there are no duplicates and there
are no duplicates.
As soon as I click on any other row in the listbox - the Listbox
SelectIndexChanged event fire and executes just fine then the program
terminates with the following error message:
"An unhandled exception of type 'System.Data.ConstraintException' occurred in
system.data.dll

Additional information: Column 'UserId, ID' is constrained to be unique.
Value 'Dad, 4' is already present."

Why am I getting a duplicate error, when I can not see a duplicate in the
table when I list all the values?

Here is the code that I am using...

This creates the DataTable and sets the constraints:

Dim dtProfileName As DataTable = New DataTable("ProfileName")
'****** Profile Name Table
Dim UserId As DataColumn = dtProfileName.Columns.Add("UserId",
GetType(String))
UserId.AllowDBNull = False
Dim NameID As DataColumn = dtProfileName.Columns.Add("ID", GetType(Integer))
NameID.AllowDBNull = False
dtProfileName.Columns.Add("Name", GetType(String))
' Set up Contraints for ProfileName
Dim nmCols() As DataColumn = {dtProfileName.Columns("UserID"),
dtProfileName.Columns("ID")}
dtProfileName.PrimaryKey = nmCols
frmMP3Search.ds.Tables.Add(dtProfileName)

'Create The DataView and set the sort property
frmMP3Search.dvProfileName.Table = frmMP3Search.ds.Tables("ProfileName")
frmMP3Search.dvProfileName.Sort = "Name"

Now The Code to Bind The control

'Bind the Datagrid and the dataset and datatable
lstSearchRuleNames.DisplayMember = "Name"
lstSearchRuleNames.ValueMember = "ID"
lstSearchRuleNames.DataSource = dvProfileName
lstSearchRuleNames.DataBindings.Add("SelectedValue", dvProfileName, "ID")

Now The code to Add New Rows

Dim id As Integer = GetNextIDNumber()
row2 = frmMP3Search.dvProfileName.Table.NewRow
row2.Item("ID") = id
row2.Item("UserID") = frmMP3Search.userName
row2.Item("Name") = newName
frmMP3Search.dvProfileName.Table.Rows.Add(row2)

Now The code to commit the Changes

frmMP3Search.dvProfileName.Table.AcceptChanges()
Now the code to List all The values in the DataView
For Each row In frmMP3Search.dvProfileName.Table.Rows
Console.WriteLine("Record Value: User Name = " & row.Item("UserID") & " ID
= " &
row.Item("ID") & " Name = " & row.Item("Name"))
Next

This is the routine called above which provides a unique "ID"

Private Function GetNextIDNumber() As Integer
Dim id As Integer
Dim row As DataRow
For Each row In frmMP3Search.dvProfileName.Table.Rows
Console.WriteLine("The ID Value = " & row.Item("ID"))
If id <= row.Item("ID") Then
id = row.Item("ID") + 1
End If
Next
Return id
End Function

This is the result of the printing of all of the values in the dataview
imediately prior to the error:

total Number Of Records now in the view = 2
Record Value: User Name = Dad ID = 4 Name = Microsoft Word and Text Documents
Record Value: User Name = Dad ID = 5 Name = aaaaaa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top