Click here to Skip to main content
14,934,818 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
hi,
im using windows application in c#. i want to generate a ID no in a text box when i run the form first time. while second time, the number generated must be +1 of the old number. can be given condition like between 10000 and 20000.for eg: 00154 for first time means next time when i run the form i want 00155.
Posted
Comments
[no name] 5-Sep-12 12:23pm
   
Okay so go ahead and do that.
   
Why?
--SA
sahabiswarup 6-Sep-12 2:26am
   
select max(cust_id)+1 as CustID from customer_master where cust_id like '10000%'

The first question would be WHY?

If you're going to use this as a primary key in a table, DON'T!
   
Here is an example to generate random number as well as string..

Hope it helps you to understand the logic..and you can create whatever condition you want.


StringBuilder builder = new StringBuilder();
        builder.Append(RandomString(4, true));
        builder.Append(RandomNumber(1000, 9999));


Generate random number
private string RandomString(int size, bool lowerCase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (lowerCase)
            return builder.ToString().ToLower();
        return builder.ToString();
    }


Generate Random Number
private int RandomNumber(int min, int max)
{
    Random random = new Random();
    return random.Next(min, max);
}


You may also apply query to generate serial number
string sqlQuery = "select max(" + colName + ") + 1 from " + tblName;


If you still have any query don't hesitate to ask..
Happy coding.
   
v2
Comments
Dave Kreskowiak 5-Sep-12 13:44pm
   
string sqlQuery = "select max(" + colName + ") + 1 from " + tblName;

And what would happen if two clients executed this code at the same time??
sahabiswarup 6-Sep-12 2:28am
   
that query doesn't executed at the same time; there must be some time difference between two request so i don't think that may be some problem.

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