?
Solved

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

Posted on 2007-10-14
10
Medium Priority
?
152 Views
Last Modified: 2013-11-26
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,
0
Comment
Question by:cesemj
  • 5
  • 3
  • 2
10 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20075141
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
 

Author Comment

by:cesemj
ID: 20075169
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
 
LVL 16

Accepted Solution

by:
McExp earned 1360 total points
ID: 20075193
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:surajguptha
ID: 20075233
I agree with the above. I suggest generating these numbers at the database as a sequence
0
 

Author Comment

by:cesemj
ID: 20075236
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
 
LVL 21

Assisted Solution

by:surajguptha
surajguptha earned 640 total points
ID: 20075652
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
 

Author Comment

by:cesemj
ID: 20081216
Thank you all for guidance,
0
 

Author Comment

by:cesemj
ID: 20081264
Hello,

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

0
 
LVL 16

Expert Comment

by:McExp
ID: 20112785
How are your guids going?
0
 

Author Comment

by:cesemj
ID: 20114754
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question