Windows Develop Bookmark and Share   
 index > Windows Forms General > Visual Studio 2005- help required linking data
 

Visual Studio 2005- help required linking data

Hi all I am currently creating a form using Visual Studio 2005. The problem is no doubt very simple and easy to solve, however ihave only basic knowledge of the software and need help urgently.My problem is the following.

  • I have created a Query which looks up data from two different tables and links them i.e. when the query is run a first column of data appears showing information of "car manufacturers" from Table A. The second column then produces data of the models i.e. "Mondeo" from Table B.
  • The data which appears in this query is linked correctly between the two different tables and all manufacturers and models match up correctly.
  • On my form I have produced a Listbox which shows all the manufacturersfrom "table A" in a list for the user to select.
  • I have also created another Listbox which shows the model data. However what I need to do is link the two Listboxes so that when the selection of car manufacturer is made the Listbox below will only show data relating to the manufacturer chosen in the above Listbox.
  • For example if Ford was selected in the first Listbox then I would need the next Listbox to only show Ford models I.e. Mondeo, Fiesta, Escort etc.
  • Currently there is no link between the two and the second Listbox always shows data on every model in the country by every manufacturer.

So in short I need the two Listboxes to relate so that the selection in the first box reduces the available results in the second box so that only relevant data appears.

As i said I know the solution is no doubt very simple and easy but I cannot find the solution myself. If any can give me a quick explanation of how to do it without linking me to other forums and websites or places of reference it would be very much appreciated. Thank you for any help in advance.

thisiswhere  Saturday, September 02, 2006 11:27 AM

you would need to do some filtering either by executing another query on your database, or if you filled a dataset, query it (better)

an example would be, if you choose an item in the listbox, on its selectedindexchanged event or something, you may want to query your dataset (assuming we have data in it) by using the Select method in the dataset Table.

looks like, if you havent, you need to create a relationship between the 2 tables if you have not done already.

ahmedilyas  Saturday, September 02, 2006 3:00 PM

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

CSharpFreak  Saturday, September 02, 2006 3:10 PM
hehe no worries at all, good explanation however and my hats off to you! I would have explained the same thing but needed a bit more info - but yes, good explanation - 10/10
ahmedilyas  Saturday, September 02, 2006 3:21 PM

WOW!

Thanks a lot! I'm proud the top answerer says that about me! Thanx again!

CSharpFreak  Saturday, September 02, 2006 4:05 PM

I'm still a bit lost as i feel i'm probably not using the code correctly. I am using awizard to help create the query and before clicking preview data I can create relationships between the tables and select the specific fields i wish to lookup data from. When i do this each of the fields comes up in a list where a column appears saying "Filter" I assume that by entering the right command into here that I can create a filter which will solve my problem, hopefully? The thing is, being a beginner i have no idea what text i need to write in.

So could someone tell me firstly, if i am correct to use this filter option and also what data to put in. I understand that i am something of a beginner compared to most, if not all the people on this forum and that my problems are no doubt very simplistic to the majority here, but hey i'm trying aren't i, and wheres the need for sarcastic comments. I ask my questions in a polite manner and i am very appreciative of all the help however useful people can offer me.

Anyhoo if anyone has any further help they can offer please fo so asap as it is very much appreciated and time is running out for me.

Thanks in advance, David

thisiswhere  Saturday, September 23, 2006 10:36 AM

You can use google to search for other answers

Custom Search

More Threads

• MenuItem...
• Treeview and focus
• how Call Thread to Updata datagridview?
• Storing object in Message.LParam field
• select row in datagridview without the index
• Transparent ASPX page (WebForm)
• Hiding a tab in a Tab Control
• Create a settings dialog box
• ComboBox
• newbie seeks help adding Progressbar to web browser