intox1221
asked on
Randomly generating number
Hi, in my application, when a user clicks the add button, the text fileds are activated for the user to input client information, like client ID, name, address etc. The application also connects to a database. What i need to do is randomly generate a three digit clientID, and populate it to the client id textbox when the add button is pressed, but i also need to make sure that the ID generated should not already exist in the database... any ideas...
intox1221,
This is going to require generating a random number, then searching the database for it. If it's found, you'll have to repeat the process. The more users you get the more times you'll have to repeat this process.
I can help if you're certain this is what you want to do, but I'd take a different approach if possible. Instead of a random number, why not just SELECT MAX(userid) +1 to increment the userid by 1 each time?
Frodoman
This is going to require generating a random number, then searching the database for it. If it's found, you'll have to repeat the process. The more users you get the more times you'll have to repeat this process.
I can help if you're certain this is what you want to do, but I'd take a different approach if possible. Instead of a random number, why not just SELECT MAX(userid) +1 to increment the userid by 1 each time?
Frodoman
If you do go w/ the random approach, here's the random number code:
MyInteger = CInt(Int(1000*Rnd()))
MyInteger = CInt(Int(1000*Rnd()))
ASKER
i declared myinteger as int32 and then equated it with txtClient.text but the application pukes it...
something im missing ?
something im missing ?
Did you use tostring?
txtClient.text = YourInt32.ToString
txtClient.text = YourInt32.ToString
ASKER
i did..
MyInteger = Cint (txtclient.text)
Also i need to have option strict on which disallows implicit conversions.
MyInteger = Cint (txtclient.text)
Also i need to have option strict on which disallows implicit conversions.
I assume it's a type conversion problem? What is displayed in txtClient before the error? What error is shown?
You might have extra spaces so also try MyInteger = CType(trim(txtclient.text) ,Int32)
You might have extra spaces so also try MyInteger = CType(trim(txtclient.text)
ASKER
Still pukin it... this is what i got....
Dim myinteger As Int32
myinteger = CInt(Int(1000 * Rnd()))
myinteger = CType(Trim(txtClient.Text) , Int32)
Dim myinteger As Int32
myinteger = CInt(Int(1000 * Rnd()))
myinteger = CType(Trim(txtClient.Text)
Where is the error and what is the description of the error?
ASKER
thats the error message.
An unhandled exception of type 'System.InvalidCastExcepti on' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Integer' is not valid.
An unhandled exception of type 'System.InvalidCastExcepti
Additional information: Cast from string "" to type 'Integer' is not valid.
This means that txtClient is empty when you reach the code:
myinteger = CType(Trim(txtClient.Text) , Int32)
The block below will avoid the error and use a '0' if the textbox is empty, but you should probably examine your code for why it is empty.
If IsNumeric(txtClient.Text) Then
myinteger = CType(Trim(txtClient.Text) , Int32)
Else
myinteger = 0
End If
What exactly are you trying to accomplish with this code? Seems like you create the random number and you'd want to put it INTO your textbox instead of pulling something OUT of it...
Dim myinteger As Int32
myinteger = CInt(Int(1000 * Rnd()))
txtClient.Text = CType(myinteger,String)
myinteger = CType(Trim(txtClient.Text)
The block below will avoid the error and use a '0' if the textbox is empty, but you should probably examine your code for why it is empty.
If IsNumeric(txtClient.Text) Then
myinteger = CType(Trim(txtClient.Text)
Else
myinteger = 0
End If
What exactly are you trying to accomplish with this code? Seems like you create the random number and you'd want to put it INTO your textbox instead of pulling something OUT of it...
Dim myinteger As Int32
myinteger = CInt(Int(1000 * Rnd()))
txtClient.Text = CType(myinteger,String)
ASKER
thats exactly what i want. My textbox is empty when the add button is pressed and i want fill it with the randomly generated code... what the add button does is it activates the textfileds, as they are read only or deactivated before that. Then upon pressing the save button, is when i want the data should be sent to the database (that i have already done). So all i wanna do is populate the text field with the randomly generated code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sweeeeet. Works like a charm. But it also generates numbers like 54, 14... can i make it atleast 3 digits... or 014 instead of 14... ??????
ASKER
actually that doesnt make much of a difference. nevermind that...
ASKER
thanks for ur help on this one. I have another question to the follow up of my current question. So i am posting it as a new question, thanks for ur help. The topic should be "Randomly generating number take 2"
ASKER