Search code examples
c#sequential

How to construct sequential ID logic


I got an textbox1.text="J001" when the form "insert" is showed up. there's a button in "insert" so if we click that button, mbox said "J001 is added".

But then, I want to change the textbox.text = "J002"; so I'm using.

string splitter=textbox1.text.toString().subString(1)

It will return 001. and now what do i have to make it 002? I can't +1 on it though, in additional, I'm not using any database, so it will become "J001" again when we close the form, and reopen them. click the button, the text become "J002". close the form, reopen, it shows "J001" and infinite loop.


Solution

  • It's quite difficult to understand what you want, but maybe this is what you are looking for:

    string splitter = textbox1.Text.ToString().Substring(1);
    
    try {
        int number = Convert.ToInt32(splitter) + 1;
    } catch (FormatException e) {
        // Not a number...
    }
    
    textbox1.Text = String.Format("J{0}", number.ToString());