Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > how can I refresh the primary key of the new record from the database to bindingsource control?
 

how can I refresh the primary key of the new record from the database to bindingsource control?

Question:

I addeda new recordbybindingsource.addnew(), and then saved it to database.The bindingsource show the primary key is -1. How can Iget the primary key from the database to bindingsourcecontrol?

I knowTableAdapter.fill() can get the whole table, include the primary key, but is there any quickly way?


Problem:

When I runbindingsource1.addnew() to add a new record, the primary key of the new record is -1.Afterthe record was saved to database, the primary key is -1 also.
If I do some other changes and save the changes again, the bindingsource tells me the constraint conflicted. It caused by the added record's primary key, the record's primary key is -1 and it cann't beupdate to database.

long live man  Monday, July 13, 2009 3:55 AM
Beth has an example here:

http://blogs.msdn.com/bethmassi/archive/2009/05/14/using-tableadapters-to-insert-related-data-into-an-ms-access-database.aspx

It is for Access, but the same type of thing may work in SQL Server as well.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Monday, July 13, 2009 5:03 PM
Beth has an example here:

http://blogs.msdn.com/bethmassi/archive/2009/05/14/using-tableadapters-to-insert-related-data-into-an-ms-access-database.aspx

It is for Access, but the same type of thing may work in SQL Server as well.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Monday, July 13, 2009 5:03 PM
Thanks a lot , Deborahk.

Yes ,it's the problem of the access database since it doesn't support batch statement.
long live man  Tuesday, July 14, 2009 12:37 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView: values don't appear in DataGridViewComboBoxColumn
• How do I generate typed objects from stored procedures?
• Filtering data source for a combo box in a datagridview
• Large XML in WinForm
• New Row Creation Problem
• DataSource method vs DataBind method when to use
• Insertion, Deletion and Updation in DataGridView
• DataGridViewColumn with array of RadioButtons
• Repeater display five rows
• DataGridView adding two rows !! Why ?