Autopopulate Primary Key in Form

curlyq

New member
Joined
Apr 5, 2015
Messages
4
Programming Experience
Beginner
Warning, extreme newbie here - plain English required! I have an sql database that auto-populates a primary key field in a table and increments by 1 - it works fine when entering data directly into the table. As I am trying to create a form for an application - I would like for the primary key to be a read only id that is filled out in the form when it loads, no user input needed. But when adding data through the form it asks for an entry since it is not a 'null' field. Right now I have the "id" control as a read-only text box. Thanks for any help.
 
How EXACTLY are you saving the data to the database? Most commonly, you'd use a DataTable to store data locally and populate that first, then save your changes from the DataTable back to the database using a data adapter or table adapter. In that case, the DataTable can generate an ID for you when you create the record if it's configured appropriately.

It's important to note that that is just a temporary ID though. It is useful for identifying the record locally before you save it, particularly if you want to create related child records. The real ID will still be generated by the database when you save and that can be retrieved back into the DataTable if desired. The final ID will often be the same as the temporary ID but there is no guarantee of that so you should never rely on it.

If you absolutely must know the final ID before saving the record then you can use a sequence, which is a fairly new feature in SQL Server that allows you to have the database generate a sequential ID for you, just like an identity, but have it done before saving new records. Unlike an identity, a sequence is managed separately to a table.
 
My goal is to have an application that asks for input via a form, in this case it is a race setup form for the user to save race date, time, participants, etc. The RaceID field is not important for the user and I don't want him to have to come up with a value to enter every time he fills out a form. I hadn't planned for the user to see the datatable, as it looks a little confusing to have a form with controls AND a table with the same information. So, I am hoping to have data saved to the table via the form and not have it pre-populated. Maybe there is another better way to go about this, but I'm just learning and trying to teach myself using youtube videos!

What I am trying to do is build an application for a pigeon racing club that enters weekly race information, calculates the velocities of the flyers and ranks them fastest to slowest for a report. There are plenty of programs like this in existence, but we can't afford them! So, I'm going to need help along the way... maybe my approach is not correct but any ideas would be appreciated.
 
It sounds like what you should do is to create your table with the primary key column configured as an identity, meaning that it will auto-generate the ID when a record is inserted. You can then use a typed DataSet with a table adapter or an untyped DataSet/DataTable with a data adapter, bind the DataSet/DataTable to a BindingSource and bind that to your individual controls. You can then add a row via the BindingSource, populate it via the controls and then, after adding, editing and deleting as many rows as you like, you use the adapter to save all the changes to the database in a batch. You can prepopulate the DataSet/DataTable with data from the database or not as you like.
 
Back
Top