1
       private string GenerateID()
        {


        }
        private void auto()
        {
            AdmissionNo.Text = "A-" + GenerateID();

        }

with prefix of A like below A-0001 A-0002 and so on .

7
  • similar to: stackoverflow.com/questions/19958518/…? Commented Sep 4, 2014 at 10:48
  • i m not satisfy that answer there is no solution like my prob. Commented Sep 4, 2014 at 10:49
  • do you have a database in your system? or a way of saving a variable permanently? Commented Sep 4, 2014 at 10:51
  • yeah i have a database in my system. Commented Sep 4, 2014 at 10:54
  • and befor i generate random id 0-9 but now i want to generate sequential id like above so how implemet it Commented Sep 4, 2014 at 10:55

3 Answers 3

1

You can use below code.

private string GenerateId()
{
    int lastAddedId = 8; // get this value from database
    string demo = Convert.ToString(lastAddedId + 1).PadLeft(4, '0');
    return demo;
    // it will return 0009
}

private void Auto()
{
    AdmissionNo.Text = "A-" + GenerateId();
    // here it will set the text as "A-0009"
}
Sign up to request clarification or add additional context in comments.

Comments

0

Look at this

public class Program
{
    private static int _globalSequence;

    static void Main(string[] args)
    {
        _globalSequence = 0;

        for (int i = 0; i < 10; i++)
        {
            Randomize(i);
            Console.WriteLine("----------------------------------------->");
        }


        Console.ReadLine();
    }

    static void Randomize(int seed)
    {
        Random r = new Random();
        if (_globalSequence == 0) _globalSequence = r.Next();

        Console.WriteLine("Random: {0}", _globalSequence);
        int localSequence = Interlocked.Increment(ref _globalSequence);

        Console.WriteLine("Increment: {0}, Output: {1}", _globalSequence, localSequence);
    }

}

Comments

0

Whether it is an windows application or not is IMHO not relevant. I'd rather care about thread safety. Hence, I would use something like this:

public sealed class Sequence
{
    private int value = 0;

    public Sequence(string prefix)
    {
        this.Prefix = prefix;
    }

    public string Prefix { get; }

    public int GetNextValue()
    {
        return System.Threading.Interlocked.Increment(ref this.value);
    }

    public string GetNextNumber()
    {
        return $"{this.Prefix}{this.GetNextValue():0000}";
    }
}

This could easily be enhanced to use the a digit count. So the "0000" part could be dynamically specified as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.