Generating sequential number
-
Thursday, March 01, 2012 9:52 AM
Hi,
I am writing a order manag sys , what is the best way to generate sequential order number somthing like ORD00001 , ORD00002 ....
Thanks
All Replies
-
Thursday, March 01, 2012 10:16 AM
Please add this function to your project to get the autoIncrementOrderID.
public string AutoIncrementOrderID() { /* //wrt the code for retrieving the specific column value(OrderID) //by sorting the column dec ordr(eg: select top 1 o_id from orderTab order by o_id //desc. */ string new_ord_id=""; int ord_num=0; string ord_id=//here u have to get the value from the table. if(has rows) { ord_num=Int32.parse(ord_id.SubString(3,5)); } ord_num++; if((ord_num>=1) && (order_num<=9)) { new_ord_id="ORD0000"+ord_num; } else if ((ord_num>=10) && (order_num<=99)) { new_ord_id="ORD000"+ord_num; } else if ((ord_num>=100) && (order_num<=999)) { new_ord_id="ORD00"+ord_num; } /*You can include as many as if conditions as per your requirment*/ return new_ord_id; }Thanks and Regards.
K
-
Thursday, March 01, 2012 10:43 AM
thanks for your reply , the only proble with this approach is if two or more users try to create order at same time you might end up with duplicate order number.
-
Thursday, March 01, 2012 10:46 AM
Hi XFahadx
its Better to use Database functionality for update of to avoid dupilcates
make a table according to your need and generate the number...accordingly
Harshad..... Always 4 U
- Edited by Harjohn Thursday, March 01, 2012 10:47 AM
-
Thursday, March 01, 2012 11:03 AMIf it is multi user environment then work with Transaction with isolation levels.
-
Thursday, March 01, 2012 3:15 PM
Virtually all databases have a mechanism for generating a unique incrementing ID. Just use that. Don't re-invent the wheel. It will do it efficiently, and it will handle all of the corner cases of a highly multithreaded environment.
If you really, really need the ORD in front with leading zeros then just make a derived column that takes the auto-generated ID and appends the ORD+padded zeros.
-
Thursday, March 01, 2012 3:38 PM
Don't use all those if statements. Use actual string formatting.
new_ord_id = "ORD" + ord_num.ToString("D6");

