VbStudent2008

New member
Joined
Apr 24, 2009
Messages
2
Programming Experience
Beginner
Hello,

I'm pretty new to VB, and need some help with a project I'm working on. I'm trying to create a random name generator, and in order to store all the names I'm using an SQL database. I've got the database set up (simple version just until I get it working) and here's basically what I want the program to do...

My database consists of three main fields: Name, Gender, and Origin (along with corresponding ID's). I want the user to be able to use a drop down combo box to select the gender (m or f) and the origin (i.e. English, German, Chinese, etc.), have the program filter out any data not within these parameters, randomly choose one of the remaining names, and display it on the form in a label.

Does all that make sense?

I'm not quite sure how to get the combo boxes linked to the data set so that, when running, the chosen item filters the data.

Thanks for any help!

SR
 
What are the steps you want this to go through? Are you retrieving all the data first or do you want to wait until the user has selected gender and origin before just getting a single name from the database? Also, when you say that you have a SQL database do you actually mean SQL Server? All databases support SQL. It's a language for writing queries, not a type of database.
 
What I mean to say is that I created the database within Visual Studio...rather than with say, Microsoft Access...

I'm not exactly sure the best way to design it, but (hopefully answering your question), I don't need any data returned until after the selections are made.
Basically, instead of randomly choosing a name from the entire database, randomly choosing one from only those connected to, say, male - English...

The code to filter, randomly select a name, and display will be in the Button_Click event.

Hope that clears it up...it's a little hard for me to explain.



SR
 
I've never done it myself but I believe that the NEWID function can be used in SQL Server to enable you to select a random record. In your case your query would look something like this:
VB.NET:
SELECT TOP 1 Name
FROM Names
WHERE GenderID = @GenderID
AND OriginID = @OriginID
ORDER BY NEWID()
So you execute that using a SqlCommand by calling ExecuteScalar. You set the two parameter values based on the choices made by the user.

What happens is that the database will get all the records from the Names table where the GenderID and OriginID match the values you specify. It will then generate a globally unique identifier for each record and sort the records by that value, putting them in random order. It will then select the Name value from the first record. Tada! You have selected a random name.
 
Back
Top