Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Dim RandomGenerator As Random
Dim intRandomNumber As Integer

RandomGenerator = New Random()
intRandomNumber = RandomGenerator.Next(100, 1000)

I have using the above coding to generate id for my table in database but the id does not generate automatically..
Posted
Updated 2-Apr-11 23:41pm
v2

1 solution

The first question is, why do you want to generate an ID in your application? IF you're using SQL Server for example, then you can define the ID column as identity (http://msdn.microsoft.com/en-us/library/ms186775.aspx[^]).

Another thing is that if you just generate random numbers it's quite likely that you get the same number at some point so it wouldn't serve as a unique ID.
 
Share this answer
 
Comments
nazila24 3-Apr-11 5:16am    
i have generate ID using SQL server but i have problem when the id that generate by sqlserver cannot insert into another table as foreign key..thats why i'm try generate id manually.
Wendelius 3-Apr-11 5:29am    
For that purpose you can use for example SCOPE_IDENTITY(), see: http://msdn.microsoft.com/en-us/library/ms190315.aspx
Sergey Alexandrovich Kryukov 5-Apr-11 0:37am    
...bad idea...
--SA
#realJSOP 3-Apr-11 9:19am    
5 - Proposed as answer
Wendelius 3-Apr-11 9:34am    
Thank you :)

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