Click here to Skip to main content
15,936,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed an application for registration kids at Creche. There's one text box where I need a child's identification number. In this number it starts with a year e.g. 22 for 2022, and I need this number to increase automatically and in sequence every time I run the application. I have code for that and it works correctly when running the application. But when I re start the application it count from scratch instead of continuing from the last digit. See my code below.

What I have tried:

Public Class RegistrationForm

Dim cyear As String = DateTime.Now.ToString("yy")
Dim StudentNumber As Integer = My.Settings.StudentNumber
Dim studentTotal As Integer = 0

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
My.Settings.StudentNumber = StudentNumber
If (BtnSave.Enabled = True) Then
studentTotal += 1
StudentNumber = cyear & studentTotal.ToString("0000")
CH_NumberTextBox.Text = StudentNumber.ToString
My.Settings.Save()
End If
Posted
Updated 22-Apr-22 9:22am
Comments
Richard MacCutchan 22-Apr-22 12:44pm    
Check that your settings are being correctly updated.
jsc42 22-Apr-22 12:57pm    
The settings would have to be User, not Application. But it is the wrong place to save it. You want something that is pan-user, in case a different user uses your application. I assume that the registration details are being saved to a database, so the best place is the save the value in a table in the database.

Also, I note that you are setting StudentNumber as an increment of studentTotal, which you are setting to zero at the start of each run. This is another reason you are restarting the count each time.

Finally, for now, if you have 50 children who attend 200 days a year your counter will roll over from 9999 back to 0000. It would be better if the id number was independent of the year so it can grow as big as it needs.
0x01AA 22-Apr-22 16:03pm    
You mix several things up and finally you don't assign the actual value to My.Settings.StudentNumber before you call My.Settings.Save()

1 solution

Using a MariaDB with table named Last_ID, I use the following whenever I require a new unique id.

BEGIN
SET @last = 0;
SELECT `last_member_id` FROM `Last_ID` WHERE id = 1 INTO @last FOR UPDATE;
SET @last = @last + 1;
UPDATE `Last_ID` SET `last_member_id` = @last WHERE id = 1;
COMMIT;
SELECT id, `last_member_id` FROM `Last_ID` WHERE id = 1;
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900