Results 1 to 3 of 3

Thread: Problem with datagrid identity column

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    6

    Problem with datagrid identity column

    I have a datagridview with contacts on a form with clients. The underlying table for contacts has a field for Contactnumber. This is a identity column so its new number is generated by SQL Server.

    I add a row and when I move to the next record on the form, the form ánd the information in the datagrid is saved to the database. So far, so good. The problem is: when I move back and try to delete that newly made row I get an error about concurrency.

    It's logical actually, the record is saved, but the dataset of the datagrid doesn't yet know what the new contactnumber is. Apparently it's not information that it gets back automatically from SQL Server.

    How can I make the dataset know the new identiy of the row that is saved to the database? I use a simple da.Update(dsVerwijderingen, Tabelnaam) to save the changes to the database.

  2. #2
    Addicted Member Cristian's Avatar
    Join Date
    Jun 2006
    Posts
    228

    Re: Problem with datagrid identity column

    First, you must to said what sql sentence you are to using.
    I know that is possible specify when you are using SqlCommand parameters, that a parameter to be input/output. This meaning that when you execute an update, the parameter is saved and then read it from the database back to the datatable asociated.
    In the case of identity column maybe the parameter could be output only because its value is get after the update is executed.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with datagrid identity column

    Your InsertCommand has to include a query to retrieve that data, e.g.
    SQL Code:
    1. INSERT INTO Person (FirstName, LastName) VALUES (@FirstName, @LastName);
    2. SELECT PersonID = SCOPE_IDENTITY()
    Now the PersonID column will be refreshed with the last generated ID when a record is inserted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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