First of all, a little piece of the painful truth: if you're begining a career or a study in programming, you will eventually have to read a lot of forum threads, books, websites ...etc. So the best solution is either drop the whole career (and loose all the fun you get out of being a programmer), or begin to cope with this fact early so by the time you really need it, you can do so easily!
(Don't mind! I'm just being sarcastic!)
Now, to your problem. Actually, there is a major decesion you'll have to make before seeking the solution: are you going to use a connected model (where whenever you need data from the database, you create a connection to the database, execute a query and get the data) or a disconnected model (where you cache all data relevant to your application into a buffer in memory that can be accessed easily whenever you need data)
Usually, if your database is using a file-based engine (such as MS Access or Paradox and DB in file mode), or if your application will run on the same computer as the database (or any other situation where the connection to the database does not require costly resources and time), you use a connected model (or a disconnected, it won't be a big difference). For Client/Server based applications, disconnected model is gaining much praise (actually because it balances the load between the server and the client)
Now back again to your issue: let's suppose the two listboxes are manufacturersLst and modelsLst, the solution will be mainly something like:
1. Attach the following event handler to the SelectedIndexChanged Event of the manufacturersLst listbox:
private void manufacturersLst_SelectedIndexChanged(object sender, System.EventArgs e){
//Here you create the SQL statement for selecting models based on the user selection of manufacturer
string query = "SELECT modelName FROM Models WHERE manufacturerName=" +
manufacturersLst.SelectedValue.ToString() ;
//Then you create a (SqlCommand/OleDbCommand/Any other dbcommand suitable) to send the SQL statement to the DB
SqlCommand cmd = new SqlCommand("Connection_string_to_your_db_here", query);
//Then you create a DataReader of the appropriate type to read the result set
SqlDataReader reader = cmd.ExecuteReader();
//Now you loop through the reader to add values to the second listbox
while(reader.Read()){
modelsLst.Items.Add(reader["ModelName"].ToString());
}
}
Now the other option: Disconnected Model:
The main application's form should look something like this:
public class Form1:System.Windows.Forms.Form{
//Used as the buffer to store the data in memory
private DataSet ds;
//This method is attached to the form's Load event
private void Form1_Load(object sender, EventArgs e){
SqlDataAdapter adapter1 = new SqlDataAdapter("Select_statement_for table manufacturers", "Connection string to your database" );
SqlDataAdapter adapter2 = new SqlDataAdapter("Select_statement_for table models", "Connection string to your database" );
adapter1.Fill(ds); adapter2.Fill(ds);
DataTable m = new DataTable("ModelsSource");
m.Columns.Add("Model", typeof(string));
ds.Tables.Add(m);
manufacturersLst.DataSource = ds.Tables[0];
modelsLst.DataSource = ds.Tables["ModelsSource"];
}
//This method is attached to the SelectedIndexChanged event of manufacturersLst
private void manufacturersLst_SelectedIndexChanged(object sender, EventArgs e){
//Clear the temp table of all data
ds.Tables["ModelsSource"].Clear();
//Create your filter on the second table
DataRow[] t = ds.Tables[1].Select("manufacturerName = " + manufacturersLst.SelectedValue.ToString());
foreach(DataRow r in t){
ds.Tables["ModelsSource"].Rows.Add(r);
}
}
}
Although the second example is maybe the worst code i've ever written, but you said you're a beginner and can be interpreted into many things in programming. Of course there are much more compact ways but they include relationship handling within DataSet objects which means you should at least have good knowledge of DataSet objects.
If you are developing using another programming language than C#, the code might be "translated" easily to any .NET language
For any questions, post back here!
Hope i could help!
Note to ahmedilyas: seems we've been writing the replies at the same time, so sorry. This seems to be the second time this happened, so i don't want it to be understood any kindda bad!
Respectfully yours