0

Hi I'm creating an auto generated ID with a first string of GP and 6 char of GUID and auto_increment 000001 depending on ID. So it will be something like this

GP-FJISLD-00001

Here's my GUID generated string. It execute 32 GUID which is not to be good when I'm trying to look for the data by searching the primary key.

String GUID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();
txt_ouput.Text = GUID.ToString();

Any help please? Thanks :D

truelogicINC
  • 119
  • 14
  • 1
    Not an answer to your question, but note that you can replace `Guid.NewGuid().ToString().Replace("-", "").ToUpper()` with `Guid.NewGuid().ToString("N")`. – Matthew Watson Aug 11 '15 at 07:47
  • 7
    If you cut a part of GUID out, it's no longer globally unique. There's no longer any point in using a GUID in a first place, any random number will do *better*. – Luaan Aug 11 '15 at 07:47
  • i don't understand :( – Backs Aug 11 '15 at 07:47
  • What I mean is, I have GP- String and 6 characters of GUID in the middle then incrementating 000002 depending if there's already id 1 does exists – truelogicINC Aug 11 '15 at 07:58
  • Also, the example result contains non-hexadecimal characters, so it's not a Guid. (At least not in the default hexadecimal representation.) – sisve Aug 11 '15 at 07:59
  • sir @SimonSvensson huh? Ah yeah. But anyhow, do you have an idea how can I create 000002 unique id when the users already submitted data with a primary id of 1. You know, for the user know how many records does he/she already encoded. – truelogicINC Aug 11 '15 at 08:02
  • Based on my understanding you just want a random String so you can [generate](http://stackoverflow.com/a/1344242/1993545) it in your one – WiiMaxx Aug 11 '15 at 08:08

2 Answers2

1

Have you tried the string.Substring method with a pseudo-randomizer?

int id = yourId;
int pseudoRandomizerStart = (yourId % 32) - 1; //32 is the length of a Guid, -1 to make sure it lands on the correct index.
string firstSixGuidChars = GUID.Substring(pseudoRandomizerStart, pseudoRandomizerStart + 5); //this is based on index.
string idString = id.ToString();

//Hypothetically, you want your id to be, at minimum 6 digits long, 
//with preceding 0s if the number is less than 6 digits 
//and the whole number if it's more than 6 digits.
if(idString.Length < 6){
idString = "000000" + idString;
idString = idString.Substring(idString.Length - 6);
}

So in theory, id 00001 wil generate the first six characters of a Guid, 00002 will generate index 1 to 6 of a Guid, and so on. If your Id is unique, then this should always return a unique key.

It will then get your id, append zeroes before it based on what you need, then return back a six-digit-id to you(preceded by zeroes if the number is less than 6 digits.)

The only problem with this and your example is that Guid returns 32 hexadecimal digits, so I can't guarantee Guid will return an all-alphabetical part(based on your example).

Nathan
  • 1,480
  • 1
  • 12
  • 21
  • Sir. Honestly, it works!!! But in the next 000001. I want it to be updated whenever I added a new records on my table, the next sequence will be 000002, next submission is 000003...0000100 so on and so forth. For the user know how many data has been submitted on the table. Any help sir? :) – truelogicINC Aug 11 '15 at 08:14
  • Edited answer. Please take a look. – Nathan Aug 11 '15 at 09:15
1

By only taking the first six characters of the GUID, I would have reservations about just how unique this is going to be...

It sounds to me like what you're actually trying to do here is to have a compound key, involving Type, ID and SequenceNumber.

My advice would be to implement a primary key based on the ID alone (the full guid) and then have the other fields on non-clustered indexes.

Martin Milan
  • 6,170
  • 2
  • 30
  • 43