Results 1 to 2 of 2

Thread: Primary Key Error when Update DB from Data Adapter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    66

    Primary Key Error when Update DB from Data Adapter

    I have a process where a user will click a button and load an xml file on their machine to the database. The code errors out on the red line. Stating:

    The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.


    Here is the code
    VB Code:
    1. Dim cnn As New OleDbConnection(AppSettings("cnn") & Server.MapPath(AppSettings("MapPath")))
    2.         Dim File As New FileInfo("c:\Items.xml")
    3.         Dim cmdText As New System.Text.StringBuilder()
    4.         Dim DS As New DataSet()
    5.         Dim DA As OleDbDataAdapter
    6.         Dim cmdB As OleDbCommandBuilder
    7.         Dim PK(1) As DataColumn
    8.         Dim Tbl As New DataTable("Catalog_tbl")
    9.  
    10.         If File.Directory.Exists Then
    11.             If File.Exists Then
    12.                 ''create tbl to load xml data into
    13.                 'Tbl.Columns.Add(New DataColumn("ItemNumber", GetType(String)))
    14.                 'Tbl.Columns.Add(New DataColumn("Artist", GetType(String)))
    15.                 'Tbl.Columns.Add(New DataColumn("Title", GetType(String)))
    16.                 'Tbl.Columns.Add(New DataColumn("Bid", GetType(Double)))
    17.                 'Tbl.Columns.Add(New DataColumn("Medium", GetType(String)))
    18.                 'Tbl.Columns.Add(New DataColumn("Height", GetType(String)))
    19.                 'Tbl.Columns.Add(New DataColumn("Width", GetType(String)))
    20.                 'Tbl.Columns.Add(New DataColumn("Description", GetType(String)))
    21.                 'Tbl.Columns.Add(New DataColumn("ArtMarket", GetType(String)))
    22.                 'Tbl.Columns.Add(New DataColumn("AuctionTime", GetType(String)))
    23.                 'Tbl.Columns.Add(New DataColumn("ArtistWebSite", GetType(String)))
    24.                 'Tbl.Columns.Add(New DataColumn("ArtMarketWebSite", GetType(String)))
    25.                 'Tbl.Columns.Add(New DataColumn("LECurrent", GetType(Integer)))
    26.                 'Tbl.Columns.Add(New DataColumn("LEPrevious", GetType(Integer)))
    27.                 'Tbl.Columns.Add(New DataColumn("JudgeAward", GetType(Integer)))
    28.                 'Tbl.Columns.Add(New DataColumn("JudgeCitation", GetType(Integer)))
    29.                 'Tbl.Columns.Add(New DataColumn("PeoplesChoice", GetType(Integer)))
    30.                 'DS.Tables.Add(Tbl)
    31.  
    32.                 'select sql
    33.                 cmdText.Append("SELECT ")
    34.                 cmdText.Append("ItemNumber, ")
    35.                 cmdText.Append("Artist, ")
    36.                 cmdText.Append("Title, ")
    37.                 cmdText.Append("Bid, ")
    38.                 cmdText.Append("Medium, ")
    39.                 cmdText.Append("Height, ")
    40.                 cmdText.Append("Width, ")
    41.                 cmdText.Append("Description, ")
    42.                 cmdText.Append("ArtMarket, ")
    43.                 cmdText.Append("AuctionTime, ")
    44.                 cmdText.Append("ArtistWebSite, ")
    45.                 cmdText.Append("ArtMarketWebSite, ")
    46.                 cmdText.Append("LECurrent, ")
    47.                 cmdText.Append("LEPrevious, ")
    48.                 cmdText.Append("JudgeAward, ")
    49.                 cmdText.Append("JudgeCitation, ")
    50.                 cmdText.Append("PeoplesChoice ")
    51.                 cmdText.Append("FROM Catalog_tbl ")
    52.                 cmdText.Append("ORDER BY ItemNumber")
    53.  
    54.                 DA = New OleDbDataAdapter(cmdText.ToString, cnn)
    55.                 'auto generate insert, update, delete statements
    56.                 cmdB = New OleDbCommandBuilder(DA)
    57.                 DA.Fill(DS, Tbl.TableName) 'load current records
    58.                 'create primary key
    59.                 PK(0) = Tbl.Columns("ItemNumber")
    60.                 Tbl.PrimaryKey = PK
    61.                 'delete all records
    62.                 Tbl.Rows.Clear()
    63.                 DS.ReadXml(File.FullName) 'load xml file
    64.                 'insert/update records in web database
    65.                 [color=red]DA.Update(DS, "Catalog_tbl")[/color]
    66.                 Response.Write("all done")
    67.             End If
    68.         End If
    69.  
    70.         DS.Dispose()
    71.         DS = Nothing
    72.         DA.Dispose()
    73.         DA = Nothing
    74.         cnn.Dispose()
    75.         cnn = Nothing
    76.         cmdB.Dispose()
    77.         cmdB = Nothing
    78.         Tbl.Dispose()
    79.         Tbl = Nothing
    Jason Meckley
    Database Analyst
    WITF

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Well, aparently one or more of your fields in your DB is set to be unique. And when you try to add the next row, some of the fields are not unique, meaning that two rows contain the same data in one of the unique fields.

    Either check that the data unique, or alter your DB to allow duplicates.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width