Adding new item through databinding of dataset

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
So I am trying to figure this databinding thing out, slowly getting there, this is probably a simple question but I have a form that I want to add a new record on open, so in the load event I call BindingNavigatorAddNewItem.PerformClick(), which works. Right now I have one field in the form, the only mandatory field in that table. I click save, it saves the first record, I think call BindingNavigatorAddNewItem.PerformClick() again to go to a new record. When I enter data this time and hit save, i get Violation of UNIQUE KEY constraint 'pk_flightplan'. Cannot insert duplicate key in object 'dbo.flightplan'. My table has an id field which is an identity column. When I click save, i get that error. How come it isnt creating the new row with the next higher id and allowing me to save it.

I get that error on this line
Me.TableAdapterManager.UpdateAll(Me.FlightTestAppDataSet)
 
It just wont add past the first row. if I truncate the table and do it again, it will add the first record, but nothing after that. What am I doing wrong
 
You shouldn't really be calling PerformClick on that button. It will work but it would be more correct to call the AddNew method of your BindingSource. That's what clicking the button does anyway. That will create a row but doesn't commit it to the underlying DataTable. To do that, you need to call EndEdit on the BindingSource, which is done automatically if the user navigates away from the new row.
 
Back
Top