CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Location
    Stuttgart, Germany
    Posts
    56

    Update DATABASE using DataAdapter's update method

    Hi guys,

    I want to update this datatable into my DB but the table in the DB that I use, it doesn't use Primary Key and I really don't need it. Therefore I get this Exception error (System.InvalidOperationException: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information).

    If I go to the DB and I add a new column into the table and assign it like PK it works but I really want to avoid this and keep the table like that.

    Any suggestion please?

    Thank you in advance

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Update DATABASE using DataAdapter's update method

    [quote=raulbolanos;1867249]... it doesn't use Primary Key and I really don't need it. Therefore I get this Exception error (System.InvalidOperationException: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information).
    [/quoté]
    Yes very easy:
    Are there any columns which have only unique values then use that column and declare it as unique and to be the primary key.

    If you cannot have single one row which is filled with unique values, maybe you will have two or more columns who together are resulting in a unique condition. For example Consider a tabel which has two columns: post_section_No and streetname. You may have streetnames and they are not unique itself, because there are some streets with the same name in different districts so you have different post-Section-numbers with the same streetname. On the other side your post destrict names are also not unique, because you have lots of streets in each district. But both columns together are one key because a defined post-section-No together with a streetname is unique. ( Consider you dont have the same name in the same district twice, which can happen here in Vienna ! )
    Ok in this example we would simple declare streetname together with post_section_No to be the unique key and that all about.

    If you dont have any possibility for a unique key define one. There is no way around, because how the program should differ between two lines if it has no exact data which one is to be updated. So how this should work then. You would need to compare all columns to find a specific line then and what happens if even this is not unique ?

    I dont know anything about your database, but if you dont have a possibility for a Primary Key, maybe you need to change your design. Maybe simple add an autoincrement number and all is done.
    There is no other way out then the described one.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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

Featured