By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

combobox and dbkey reference

P: 122
I have may situations where I want to load into a combo box a list of products by name but I want to be able to run a query with the corresponding id in the database so when the user picks red wagon i can fire an event that will do something with the database and it knows that red wagon is idkey in the database is 12 or if they pick yellow truck it is idkey 51 etc.

I know how to load the list box with a list of descriptions from the database but i am not sure how to associate the idkey at the same time to that list item and then pull it out later.

any help would be greatly appreciated.

thanks in advance.
Jan 15 '09 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 190
For the WebControl ListBox, each added item has a Text and Value property, so your id would be the value of the selected item.
Expand|Select|Wrap|Line Numbers
  1. myListBox.Items.Add(new ListItem("FireTruck", "51"));
For the Windows.Form ListControl and derivatives you either add an object or bind to a list of objects. In this case you specify a property of the object that serves as the ListControl.DisplayMember and a property which serves as the ValueMember.

So when you pull your data from the database, you can put the values into a "business object" called Product.

Expand|Select|Wrap|Line Numbers
  1. class Product{
  2.  string productName;
  3.  int productId;
  5.  public Product(string name, int id){
  6.    this.productName = name;
  7.    this.productId = id;
  8.  }
  9.  public string ProductName{get{return this.productName;}}
  10.  public int ProductId{get{return this.productId;}}
  11. }
  13. ListControl ctl = new ListBox();
  14. // In real code, you create a List<Product> from the database
  15. // and bind the control to the list
  16. ctl.Items.Add(new Product("FireTruck", 51));
  17. // etc
  18. ctl.DisplayMember = "ProductName";
  19. ctl.ValueMember = "ProductId";
Use the SelectedValue property to get the ProductId of the Product whose name is displayed and which the user has chosen.

See ListControl.ValueMember Property (System.Windows.Forms)
Jan 16 '09 #2

P: 122
Ok that makes perfect sense thank you, maybe you can help me with one other thing.

What I am doing is trying to contain all my data access to one project and pass back items to the main application.

To clarify in the above situation i would send a query to the database such as "Select Description, productid from products" my question is should i just make use of the poduct class and make a list<product> and pass that back so that i can populate the combobox with the data.

I am basically loading data based on a selected account and the products available and allowing the client to pick that item from a drop down list then go and do pricing and update inventory.

so is that the best way to d it, pass the list<product> back then itterate through it to populate the combo box?
Jan 16 '09 #3

Expert 100+
P: 190
You are thinking in the right direction - keep it up!
What you are driving for is an "n-tiered" application implementing Object Oriented design principles.
At a very high level, your app would consist of:
  • a strict "database layer" - talks only to the database, knows nothing about "Product"
  • a "business layer" - knows about Product, but doesn't care about what database it comes from
  • a "presentation layer" - knows how to display a list of Products, doesn't care where they came from.

So one way to do this is to
  • have your database layer simply return a query result (such as a DataSet or DataReader) with the results of "SELECT * FROM Products" or whatever.
  • Your business layer would call the database layer, specifying the sql or stored procedure to execute, and loop through the DataReader result to create a list of Products.
  • Your UI layer would call the business layer and get the List<Products> as a return value, and simply display them.

You can google any of the above key words to get some great tutorials and explanations. Also look up "Model-View-Controller." Here is a link that I find very simple to follow and has a simple but realistic example (The article has three parts)
CodeProject: ADO.NET for the Object-Oriented Programmer – Part One. Free source code and programming help
Jan 16 '09 #4

P: 122
Ok having a bit of a problem, need some more help. I created a list of items

Expand|Select|Wrap|Line Numbers
  1. public List<DBHelperDescValue> getListDescValue(String cmdString)
  2.         {
  3.             myConnection.Open();
  4.             List<DBHelperDescValue> myList = new List<DBHelperDescValue>();
  5.             SqlCommand myReaderCMD = new SqlCommand(cmdString, myConnection);
  6.             SqlDataReader _ListDataReader = myReaderCMD.ExecuteReader();
  7.             while (_ListDataReader.Read())
  8.             {
  9.                 myList.Add( new DBHelperDescValue (_ListDataReader[0].ToString(),Convert.ToInt32(_ListDataReader[1])));
  11.             }
  12.             myConnection.Close();
  13.             return myList;
  14.         }
then I pass that back and I would like to ad this to the combobox now.

Expand|Select|Wrap|Line Numbers
  1.  loadPackages.ForEach(delegate(DBHelperDescValue dbhDV)
  2.            {
  3.                cboPackages.Items.Add(dbhDV.ItemDescription);
  4.                cboPackages.ValueMember = dbhDV.ItemValue.ToString();
  5.            });
the idea here is i want to us a combobox due to space and gui design and when i select box it knows the database id. however when i test this i always see 9 wich is the id for the item i added last.

Any thoughts would be appreciated.
Jan 17 '09 #5

Expert 100+
P: 190
First of all, ValueMember should be set only once. It means "Use the property of my object with this name("ItemValue") as the value for each item I add to the Items collection." (Same for DisplayMember).
Secondly, using this pattern, you add the entire object to the Items collection, not just the display string.
Expand|Select|Wrap|Line Numbers
  1. cboPackages.ValueMember = "ItemValue";
  2.  loadPackages.ForEach(delegate(DBHelperDescValue dbhDV)
  3.            {cboPackages.Items.Add(dbhDV);});
...using your syntax to illustrate...but this still will not work correctly.

Apparently you can only set ValueMember and DisplayMember if you actually bind a source collection to the ListBox.
Expand|Select|Wrap|Line Numbers
  1. cboPackages.DataSource = loadPackages; // (the List, I assume)
  2. cboPackages.ValueMember = "ItemValue";
  3. cboPackages.DisplayMember = "ItemDescription"; 
Then, you don't even need to loop through and add each item.

I almost always bind to a collection as a DataSource, so it did not occur to me in my original post that those two properties will not work correctly if you are just adding items to the ListBox without data binding. Apologies.
If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object.
Jan 18 '09 #6

P: 122
Thanks this worked Great, even aloud me to get rid of some redundant code that cleared the list from the combobox. It is also going to make it so much easier to take make use of multiple linked comboboxes.

Thanks ever so much.
Jan 18 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.