How do I create a random number with 3 alaph and 9 digits

Hello,

I have a web form and I am using the FormView data control connected to an AccessDataSource.  I am using the web form as a data entry page.  

When I click on the Insert button (from the Insert Item Template) to add the new record to the DB table I would like to have an random number like ACN999999999 generated and automatically filled into to the AutoID Field OR is it better to have random number like ICN999999999 generated and automatically filled into to the AutoID Field when the page loads or opens?

To make sure I do not have duplicate AutoID Numbers will the code have to query against tblAutoInventory AutoID field?

I would like to have a random number because the I do not want to use the default AutoNumber and so I can create one to one and one to many relationships with other tables that I will have in the future.  If you have a better sugestion please let me know?  

NOTE:  Instead of using a random number like ICN999999999 I thought of combining the the following to create a unique ID.  The Information will come from the form fields that I have to fill in.

Fields
*******
Default Fixed Value: ICN (Inventory Control Number Automaticall filled in)
AutoMFGNumber: 12345 (Ford)
DateRecordCreated: 1014071100PM (Date/Time Field  -- System Inserts Time in the field Automatically)

Please HELP me understand how this can be done with VB code.


Thank you in advance,
cesemjAsked:
Who is Participating?
 
McExpCommented:
your plans sound very dangerous, I agree that it is quite unlikely that you will get the same number twice in the imediate future but every time you create a new number you increase the chances of running into a colission with a previously generated random number.

If you are going to be generating your own random keys you might like to consider the GUID Class?

Will they be used by the user to reference the item externally? If not just use a GUID.

if you need something unique how about as you suggest using the DateTime but appending an aditional value to ensure that entrys inserted within the same minute/second (depending on the resolution you chose to record) stay unique. Maybe you could use a webservice that provides the unique ID so that you have a central point where you can control what gets used.

If you do feel it is absolutly essential to use this random technique and not the more conventional Identity field from the DB then make sure you've designed it well becuase you will be stuck with this implimentation.

Note:
the Random function is only pseudo random.
0
 
surajgupthaCommented:
Generate 12 random numbers. first 3 a number between 1-26 each representing a alphabet and 9 random numbers as a number between 0-9

You can generate the random number within a range using
Random a = new Random();
a.Next(26);
where 26 is the max value
0
 
cesemjAuthor Commented:
Thank you,  Do you have an example and/or article reference that can tell me where to start inorder to build or add the code, for exampke do I add the code to the Protected Sub Button1_click section.  

Thank you in advance,
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
surajgupthaCommented:
I agree with the above. I suggest generating these numbers at the database as a sequence
0
 
cesemjAuthor Commented:
Hello,

No, the Auto Inventory Record ID will only be used by the user internally.  The number will be used to track new parts and parts that have been shipped and/or received, similar to the way Experts Exchange has a unique ID associated with a question that is posted.

I thought about the design approach and agree that it would be best to use date&Time but appending an aditional value which wil equal something like this : ICN101407110023.

Do you have an example and/or reference article that is using this approach,

Thank you again in advance,
 
0
 
surajgupthaCommented:
I feel that even the additional value should not be a random number. It should be a number that increments sequentially. Otherwise you could get into a situation where u can have the same number for two records. But again you want to use the Date and the Time which will make sure that it is unique enough but to what precision are you going to take the time?
0
 
cesemjAuthor Commented:
Thank you all for guidance,
0
 
cesemjAuthor Commented:
Hello,

I like the concept about the GUID Class.  I am reading information articles about the Pros and Cons about using GUIDs.

0
 
McExpCommented:
How are your guids going?
0
 
cesemjAuthor Commented:
Thank you for asking, It is going ok,

I created a test table named GUIDTest and created two fields, guidID and name:

guidID: Autonumber set to Replication ID
name: Text
When I create a new record the GUID is created.  When I wen into VisualStudio and created a test webform with an accessDataSource bound to the form via the Formview (Insert) and GridView (Edit) controls and used the form view to insert new record, to see if it will work the same as if the GUID field is set to AutoNumber-LongInteger, but for some reason I receive a message when I click on the Insert button saying that guidID cannot be NULL.  It almost seems like I have to have the GUID number apear in the guidID field on the Test page when the testpage opens.  I am going to recreate the page and try it again.  Please let me know if you have any ideas why I might be running into this problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.