Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Randomly generating number

Posted on 2003-12-10
16
Medium Priority
?
327 Views
Last Modified: 2010-05-01
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...  
0
Comment
Question by:intox1221
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 

Author Comment

by:intox1221
ID: 9912216
Also im using .NET environment...
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9912284
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
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9912309
If you do go w/ the random approach, here's the random number code:

MyInteger = CInt(Int(1000*Rnd()))

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:intox1221
ID: 9912453
i declared myinteger as int32 and then equated it with txtClient.text but the application pukes it...
something im missing ?

0
 
LVL 42

Expert Comment

by:frodoman
ID: 9912485
Did you use tostring?

txtClient.text = YourInt32.ToString
0
 

Author Comment

by:intox1221
ID: 9912525
i did..

MyInteger = Cint (txtclient.text)

Also i need to have option strict on which disallows implicit conversions.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9912600
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)
0
 

Author Comment

by:intox1221
ID: 9912828
Still pukin it... this is what i got....

        Dim myinteger As Int32

        myinteger = CInt(Int(1000 * Rnd()))

myinteger = CType(Trim(txtClient.Text), Int32)
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9912886
Where is the error and what is the description of the error?  
0
 

Author Comment

by:intox1221
ID: 9912929
thats the error message.

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from string "" to type 'Integer' is not valid.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9913018
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)
0
 

Author Comment

by:intox1221
ID: 9913334
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.
0
 
LVL 42

Accepted Solution

by:
frodoman earned 1800 total points
ID: 9913408
Okay, so when the add button is pressed you want to do this:

  Dim myinteger As Int32
  myinteger = CInt(Int(1000 * Rnd()))
  txtClient.Text = CType(myinteger,String)

This creates a random number and places it into the txtClient textbox.  Is this not working?
0
 

Author Comment

by:intox1221
ID: 9913669
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... ??????
0
 

Author Comment

by:intox1221
ID: 9913731
actually that doesnt make much of a difference. nevermind that...
0
 

Author Comment

by:intox1221
ID: 9913769
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"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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