Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..
Updated 2-Apr-11 23:41pm

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 ([^]).

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.
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:
...bad idea...
#realJSOP 3-Apr-11 9:19am
5 - Proposed as answer
Wendelius 3-Apr-11 9:34am
Thank you :)
Agree, this is the Answer. My 5.
Wendelius 5-Apr-11 13:35pm
Thanks :)

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