15,997,408 members
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
[no name] 5-Sep-12 12:23pm
Okay so go ahead and do that.
Sergey Alexandrovich Kryukov 5-Sep-12 14:27pm
Why?
--SA
sahabiswarup 6-Sep-12 2:26am
select max(cust_id)+1 as CustID from customer_master where cust_id like '10000%'

## Solution 2

The first question would be WHY?

If you're going to use this as a primary key in a table, DON'T!

## Solution 1

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
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.