Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 10-Sep-12 2:21am
Comments
lukeer at 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.
digimanus at 10-Sep-12 7:30am
   
unique per person or unique for everyone?
__TR__ at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
__TR__ at 10-Sep-12 7:52am
   
Good suggestion. +5.
digimanus at 10-Sep-12 8:45am
   
well...GUID is longer than 8 digits
__TR__ at 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 at 11-Sep-12 0:25am
   
Thank you TR, :)
Ese ochuko at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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[^]
  Permalink  
Comments
Ese ochuko at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi ,
Check this
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this:
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);
}
  Permalink  
Comments
Ese ochuko at 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 at 10-Sep-12 23:47pm
   
no...
Ese ochuko at 11-Sep-12 2:50am
   
Ok thanks, so hw many million unique numbers will i be able to generate with the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 719
1 Sergey Alexandrovich Kryukov 325
2 Peter Leow 280
3 CPallini 245
4 TheRealSteveJudge 208
0 Sergey Alexandrovich Kryukov 8,963
1 OriginalGriff 6,766
2 Peter Leow 4,262
3 Zoltán Zörgő 3,769
4 Richard MacCutchan 2,588


Advertise | Privacy | Mobile
Web03 | 2.8.150128.1 | Last Updated 10 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100