Identity field doesn't increment using .NET application (in development)
Posted on 2007-04-07
This one kills me!
I am building a .NET application that for managing a database (SQL Server Express 2005).
In my database I have several tables, two of which (for the sake of presenting the problem) are "Patients" and "Therapists".
Both tables are built similarly; There is a PatientSID (PK, int, not null) and a TherapistSID (PK, int, not null); Both are defined as Identity, with an Identity seed of 1, and an Identity Increment of 1.
In my application, I have two forms: (you guessed right...) "Patients" and "Therapists".
On the forms I'm using auto-generated Navigation Bars (made by dragging the accordant table from the Data Source Explorer window to its form), for retrieving, adding, editing and deleting data in my database.
The two forms, like the two tables in the database, were built the same (more then once, I must say...)!!
Now, the problem:
When trying to add a record to the "Patients" form (by using the "Add" and "Save" buttons on the Navigation Bar), the record is being added to the "Patients" table just fine.
For some reason, when trying to add a record to the "Therapists" table, using the "Therapists" form (again by using the "Add" and "Save" buttons on the Navigation Bar), the record is not being added, because the "TherapistSID" is not incrementing, and I get an error message, saying that the "TherapistSID" doesn't allow NULLs.
The code running (and failing...) for the action of saving the new added record is:
Private Sub TherapistsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TherapistsBindingNavigatorSaveItem.Click
Me.TherapistsBindingSource.EndEdit() ' This is where it fails!
For the "Patients", the code is identical.
BTW, When adding a record directly into the table, in the SQL Server environment, it works just fine!
What's going on??