0

I'm developing an application for taking orders in C# and DevExpress, and I need a function that generates a unique order number. The order number must contain letters and digits and has a length of 20 .. I've seen things like Guid.NewGuid() but I don't want it to be totally random, nor to be just an auto increment number ..

Can anyone help? even if it's a script in a different language, I need ideas desperately :)

NadaNK
  • 782
  • 2
  • 10
  • 26
  • 5
    If you don't want it to be random, how do you want it? – James Hull May 29 '12 at 10:36
  • 3
    `Guid.NewGuid()` is unique. Not random. – Oded May 29 '12 at 10:37
  • something like an auto incremented string , hexadecimal numbers, .. things that reflect the order between numbers (ord1 is before ord2 and so on ..) – NadaNK May 29 '12 at 10:41
  • 1
    Order numbers best be tracked by incrementing numbers, what are you looking for. Give a few examples. – Erez Robinson May 29 '12 at 10:44
  • I'm thinking about taking the primary key which is an auto number, pad it with zeros until the length becomes 20 .. but this way I'm limiting the order number to be made of digits only .. – NadaNK May 29 '12 at 10:51

6 Answers6

1

You can create type of your own . lets say yyyyMMddWWW-YYY-XXXXXXX where WWW is the store number, YYY the cashier id XXXXXXX is a hexadecimal number ( -> maybe an actual autoincrement number that you turn it into hex ) . This is just an idea . Im afraid you have to decide by the elements of your system how it will be .

edited : also if you can apply a check digit algorithm on it will also help in avoiding mistakes

isioutis
  • 304
  • 2
  • 13
0

Two different methods:

  • Create MD5 or SHA1 hash of current time
  • Hash of increment number
0

Like Oded suggested, Guid is not random (well, not if you have a network card). It's based on time and location coordinates. See Raymond Chens blog post for a detailed explanation.
You are best off using an auto incremented int for order ids. I don't understand why you wouldn't want to use it or failing that a Guid?

I can't think of any way other then an auto id to maintain uniqueness and represent the order of your different orders in your system.

Darshana
  • 2,462
  • 6
  • 28
  • 54
James Hull
  • 3,669
  • 2
  • 27
  • 36
0

One thought comes to mind.
Take the DateTime.Now.Ticks convert it to hexadecimal string.
Voila, String.Format("{0:X}", value);
If not long enough , you said you need 20 digits, you can always pad with zeros.

Erez Robinson
  • 794
  • 4
  • 9
0
  1. Get the mother board ID
  2. Get the hdd ID
  3. Merge it by any way
  4. Add your secret code
  5. Apply MD5
  6. Apply Base54

Result: the serial code which is linked to the currect client PC :)

Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
0

My two cents.

If you need ideas then take a look at the Luhn and Luhn mod N algorithms.

While these algorithms are not unique code generators, they may give you some ideas on how to generate codes that can be validated (such that you could validate the code for correctness before sending it off to the database).

SAK
  • 440
  • 3
  • 17