Results 1 to 9 of 9

Thread: Database Update from dataset (how to retrieve primary key assigned by DB?)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    68

    Unhappy Database Update from dataset (how to retrieve primary key assigned by DB?)

    I have a database Access table "CUSTOMER"
    the primary key is an integer AUTO increment.

    In my program when I create a customer, I add a row in my dataset with the primary key empty, so access assign itself the key when I use Adpxxx.update(DSCustomer).

    But I need to know at that time the CUSTOMER key generated by access for an oher process.

    Is there someone who can help me ?

    Many thanks

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    That you need can be done in someway or another, but if you really need that why not produce a value for the field yourself.

    Maybe you need that filed to work on another table (linked ones). If thats the case why not creating relationships in datasets?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    In SQL Server I use this inside a stored proceedure. Maybe there is something similar in access to use to get the identity.

    INSERT INTO Topics
    (UserName, IPAddress)
    VALUES
    (@UserName, @IPAddress)

    SELECT @PostID = @@IDENTITY

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    68
    I need to display "Customer xxx has been created" just after the DB update.

    Any solution ?




  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You can read that value from the dataset. Because its value is decided at dataset level. So when you add a row to your dataset table the autonumber filed is automatically filled in the row too.
    Consider this code:
    VB Code:
    1. Dim dr as DataRow
    2. dr=myds.mytb.NewRow
    3. myds.mytb.Rows.Add(dr)
    4. Messagebox.Show(dr("the_Autonumber_field").Tostring)
    That should solve your problem.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Lunatic3
    You can read that value from the dataset. Because its value is decided at dataset level. So when you add a row to your dataset table the autonumber filed is automatically filled in the row too.
    Consider this code:
    VB Code:
    1. Dim dr as DataRow
    2. dr=myds.mytb.NewRow
    3. myds.mytb.Rows.Add(dr)
    4. Messagebox.Show(dr("the_Autonumber_field").Tostring)
    That should solve your problem.
    That is weird, I wouldn't think you could do that. What happens if someone else were to update the database before and that number was taken? Then the autonumber would be different...wouldn't it? Or is the DataSet not totally disconnected in this scenerio?

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    That is weird, I wouldn't think you could do that...
    Not weird really. If you believe that dataset table acts somewhat like a real database table then you shouldnt expect it to have its primary key set to Null or equal in all the rows, so it has to define a value for the primarykey which is considered to be an AutoNumber.

    What happens if someone else were to update the database before and that number was taken?
    I dont know what happens in this case. Maybe if you use optimistic cuncurrency the dataset updates the row which is added by another user, or ... Someone should test it.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8
    Junior Member
    Join Date
    May 2003
    Posts
    28
    Doesn't the FillSchema() method of the adapter retrieve the primary key information ?
    Regards --
    Vujjeni

  9. #9
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    oxbow123 if you can save a date/time field in your database, store that time in a variable then do a search on that field
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

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