locked
save to table without primary key RRS feed

  • Question

  • Hi,

    I use VB 2005 Express.

    Problem:
    When I save some data back to a table (Access) with a primary key everything is fine. But when save data back to a table without primary key I get an exception error:
     
    Update requires a valid UpdateCommand when passed DataRow collection with modified rows

    So my question is then:
    1. Is it possible to update a table without primary key?
    2. in that case, how?
    I use the following kode for updating:
    Me.Validate()
    Me.TblTunablesBindingSource.EndEdit()
    Me.TblTunablesTableAdapter.Update_(Me.TunablesDataSet.tblTunables)

     

    I have exported the table in Access from an external software, and to be able to import it again it must have the same format (format is without primary key)


    anyone?
    Thursday, February 2, 2006 6:17 PM

Answers

  • CommandBuilder class which generates update/insert/delete commands from select command can only build commands if you have primary key (and you always should have a primary key). You choices would be to add primary key (way to go) or provide update/insert/delete commands (i.e. set TblTunablesTableAdapter.*Command properties) manually (bad choice, but would work).

    Thursday, February 2, 2006 10:40 PM

All replies

  • CommandBuilder class which generates update/insert/delete commands from select command can only build commands if you have primary key (and you always should have a primary key). You choices would be to add primary key (way to go) or provide update/insert/delete commands (i.e. set TblTunablesTableAdapter.*Command properties) manually (bad choice, but would work).

    Thursday, February 2, 2006 10:40 PM
  • thats tricky because the primary key is what identifies a unique record. Its very difficult to single out a record without a key. The alternative would be to check each and every field's values in a where clause and thats not very reliable at all especially if the data changes while its out the system.

    The inbuilt data objects of ADO.NET will require a key for updating individual rows, simply because there isn't really a reliable alternative. One way or another there has to be a key.

    A way you can get around the problem is by ordering your data. If you can ensure that the order in which the records are exported is the same as when you import then you can maybe work out the key. first row, id 1, new rows new id or order alphabetically for strings.

    There's maybe other ways but it depends on the nature of the data.

    Thursday, February 2, 2006 10:45 PM