Update Dataset

I have a program that uses the same database for each computer the program is run from. The program uses datasets but the problem I am running into is storing (updating) the new data. For example if program one stores a record with the primary key of "A". But Program two already stored a record with the primary key of "A" after program one's dataset was filled. When the 2nd record is added with the same primary key NO exeption is thrown, and records with the same PK are in the DB. How do I prevent that from occuring

Answer this question

Update Dataset

  • Eva DELORD

    Try having a refresh button and if they don't refresh that when they make there changes

    a try statement the updates and if fails the error will be a catch statement

    The error could simple do the refresh and have them try again appling data change


  • Michal Levy

    If you can see the records with identical keys in the database, then the database itself is not enforcing the unique key constraint. What database are you using for your backend

    If the database doesn't enforce the constraint, then the DataSet won't throw an exception because it will have no indication that anything has gone wrong.

    If the database enforces the constraint then when the insert occurs, an error will be thrown. It sounds like what you really need is some way to have the database generate the unique primary key value. That's why someone else suggested that you use an AutoNumber/Identity column. In that case you wouldn't send the value in the INSERT statement, it would automatically choose the next unique value on the server.

    Depending on the backend, you could also insert through a stored procedure that knows how to generate the key. However, if the scenario requires the key to be generated on the client (for example, a user typing in a person's social security number), then the only way to guarantee uniqueness across individual clients is to make the server enforce it at the table level. You could make a client-side constraint on the DataSet, but that isn't going to help you if the duplicate value is coming from another machine at the same time. The server should be the ultimate authority on enforcing the constraint.

    If the value has to be entered by the client, and the server is enforcing the constraint, you will get an error when you try to insert a duplicate value. You can catch the exception and give the user a chance to pick a new key.

    Thanks,
    Sarah



  • Blaze Stratos

    you should use autoincrement or unique fields for PK and relations. otherwise this is the issue. if you want to add yourown unique field just make it unique and that's it. do not use it as PK.

    if you do this you will have no problem with PK duplication



  • George.Saliba

    I have a refresh button that fills the dataset again, although if I fill the dataset before the edited data is saved to the database all the changes are discared. Currently I am using this try statement:

    Try
    'Attemps to update the database with the new datafound'
    Me.AdapterCitations.Update(Me.DataSet.Citations)
    Catch MyError As Exception
    'Displays a message to the user about the unkown exception that occured'
    MsgBox("A error occured.", MsgBoxStyle.Information, "Record Error")
    End Try

    And no exeptions were thrown... Once I opened the database their were two records with identical primary keys.


  • James Relyea

    Currently the PK column is StickerNo because it has to be a unique value. So if I would set the primary key to a autoincreace I still need a way to make the StickerNo unique.
  • Update Dataset