Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody
i want to design a winform with two textboxes the first box is pincode, the pincode txtbox is readonly so cannot be edited, the other box is fullname. i want that when the form is loaded the pincode textboxes automatically generate 8 digit unique pin,that will be inserted into the primary key column in the table so no two pincode number should be the same. if i load the form again another pin is generated and so on and on and on, .
Thanks in advance
Posted
Updated 13-May-19 11:38am
Comments
lukeer 10-Sep-12 7:29am    
Let the DB generate that pin as primary key. You should somehow be able to tell the DBMS what number to start with and then auto-increment for each new entry.
Herman<T>.Instance 10-Sep-12 7:30am    
unique per person or unique for everyone?
__TR__ 10-Sep-12 7:33am    
Use the Identity[^] property in sql server and you don't have to worry about generating unique numbers for your primary key.
Ese ochuko 10-Sep-12 10:59am    
The thing is the pin will be given to the user and that will be their ID, using GUID i dont think it will be convenient for users. i want the pin of all the users to be atleast 8digit or even 10 provided it is in digit and is unique. For instance, just as credit card number is unique for every user, the account number in a bank is unique for every users, and even the telephone number is unique for every users. Hope you undastand what am saying. Thanks for your help.

 
Share this answer
 
Comments
__TR__ 10-Sep-12 7:52am    
Good suggestion. +5.
Herman<T>.Instance 10-Sep-12 8:45am    
well...GUID is longer than 8 digits
__TR__ 10-Sep-12 8:59am    
Yes. But I think using random class to generate Unique numbers is not a good idea as there is a chance the number may repeat. So i thought GUID was a better option. That's why I said it was a good suggestion not a solution :)
Prasad_Kulkarni 11-Sep-12 0:25am    
Thank you TR, :)
Ese ochuko 11-Sep-12 2:36am    
Hi guys the thing is guid is too long and it wont be convenient to the user, becos the pin will be given to the users as their ID. Take for example acount number of a bank,phone number they are all unique for different users.so thats the idea am trying to put in place even if the number is 10 digit there is no problem provided its unique for every user
Normally 8 digits are not to be sure that your ID is unique. I suggest you to use GUID's as they are standard and provide sufficient uniqueness. See here for more information:
http://en.wikipedia.org/wiki/Globally_unique_identifier[^]
and refer the answer by Prasad for how to create the ID's in C#.

Here is also a CP article discussing the topic and providing some more options for you:
Generating Unique Keys in .Net[^]
 
Share this answer
 
Comments
Ese ochuko 11-Sep-12 2:39am    
Hi guys the thing is guid is too long and it wont be convenient to the user, becos the pin will be given to the users as their ID. Take for example acount number of a bank,phone number they are all unique for different users.so thats the idea am trying to put in place even if the number is 10 digit there is no problem provided its unique for every user. Or is phone numbers and bank number not automatically generated?
Tanx for your help
Hi ,
Check this
C#
Random rand = new Random(100);
int ccc=  rand.Next(000000000, 999999999);

http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx[^]
http://www.dotnetperls.com/random[^]
Best Regards
M.Mitwalli
 
Share this answer
 
Hi,

C#
protected void form1_Load(object sender, EventArgs e)
{
   //Get the maximum ID from database
   int ID = //From Database.

   txtPostCode.Text=(ID + 1).ToString();

}

protected void btnSave_CLick(object sender,EventArgs e)
{
  // save button click

 // pass the text box value to database.
}


hope it helps.
 
Share this answer
 
Try this:
C#
using System.Security.Cryptography; // Import this Dll
public string Get8Digits()
{
    var bytes = new byte[4];
    var rng = RandomNumberGenerator.Create();
    rng.GetBytes(bytes);
    uint random = BitConverter.ToUInt32(bytes, 0) % 100000000;
    return String.Format("{0:D8}", random);
}
 
Share this answer
 
Comments
Ese ochuko 10-Sep-12 10:47am    
Is there any possibility that there wont be any digit that will be the same from your code becos am inserting it into the primary key table
prashant patil 4987 10-Sep-12 23:47pm    
no...
Ese ochuko 11-Sep-12 2:50am    
Ok thanks, so hw many million unique numbers will i be able to generate with the code
Member 10316920 23-Jul-18 5:45am    
it generates the duplicate number.
Generate unique numbers using datetime as below
DateTime.Now.ToString("MMddmmssff")

it will be unique like GUID but GUID create alphanumeric.
 
Share this answer
 
Comments
CHill60 14-May-19 4:45am    
Just including month and milliseconds does not make the number unique - the uniqueness is essential to the OP's application. Far better to use an identity column initialised appropriately - as suggested by @lukeer 7 years ago.

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