Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone Good Afternoon. Im sure this question is easy but I dont know where to start. How can I do this?

I am creating a fill up form in VB.Net and my Primary Key is like this. 00001,00002,00003 and so on and I will save this.

I will use Select Max in MYSQL so I can check whats the Max in Primary Key Field. For Example the last data in Mysql is 00001 how can i show it in a textbox with +1? I mean if 00001 is the last in MySQL in VB.Net is 00002

I hope you can help me.


What I have tried:

As of now nothing because I dont know where to start.
Updated 5-May-16 22:57pm
Tomas Takac 6-May-16 4:51am    
I suggest you rethink this approach. Consider this:
1) Does this value have a business meaning? If yes, don't make it your PK, use an identity instead (or whatever the equivalent is in MySQL). If no, no reason to show it in the UI.
2) Are you storing this as string? If yes then don't, store it as an integer and only format it in UI.
3) If you really need to do this for some reason the beware of concurrency issues. When two users do SELECT MAX at the same time they will get the same number.

1 solution

Do not do this!
Although it's possible, MySQL is a multiuser system, so it is very, very likely that two or more users will end up trying to use the number. That's dangerous: either someone will lose data, or the wrong data will get attached to the wrong client.
Use an AUTO_INCREMENT[^] solumn instead, and fetch the number only after you have created the record.

Formatting an integer with leading zeros in .NET is trivial:
Dim i as Integer = 123
Dim s as String = i.ToString("D5")
Will give you a string "00123"
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