0

I`m sure this question is easy but I don't 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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 4
    May I say that this is totally wrong from the start? If your primary key is a number then use a number not a string that looks like a number. Formatting is something that should be done on the client side – Steve May 06 '16 at 08:14
  • Ok Sir thanks for the Tip I get it now, I think I will use an Auto Increment and PK Field but I want this kind of method, I hope you can help me.:( –  May 06 '16 at 08:17
  • You can view this topic : http://stackoverflow.com/questions/611340/how-can-i-set-autoincrement-format-to-0001-in-mysql – Abdellah OUMGHAR May 06 '16 at 08:18
  • Further to @Steve's comment, if you're using MySQL then you should be using a sequence to generate your PK values. When you display them, you can use whatever formatting tools are best for the situation. – jmcilhinney May 06 '16 at 08:18
  • As mentioned, you should format your nuumber only when you display it: `LblNum.Text = (oldMax+1).ToString("00000")`. But let the database generate the next number, this should be only for information. If multiple users create a new record at the same time you don't know the next number anyway. – Tim Schmelter May 06 '16 at 08:21
  • Thank you so much for the tip, as of know I dont what code to do. –  May 06 '16 at 08:21
  • `I will use Select Max in MYSQL` - I assume you have already accounted for the fact that if two computers do this at the same time, they will see the same value? – GSerg May 06 '16 at 08:21
  • Wow Sir GSerg TY for the tip. Now i know TY So much for your help Everyone –  May 06 '16 at 08:23
  • Everyone TY for the help –  May 06 '16 at 08:24

1 Answers1

0

Instead of TextBox1.Text you have to provide your next no that may be 3 or 4 and you will get your next code with in five digit format with left padded 0.

Dim s As String = TextBox1.Text
s = s.PadLeft(5, "0")
MsgBox(s)

Or you can use following example n would be your current no may from database.

Dim n As Integer = Val(TextBox1.Text)
MsgBox((n + 1).ToString("00000"))