Results 1 to 15 of 15

Thread: [RESOLVED] Filling combo, list, and/or textboxes.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Resolved [RESOLVED] Filling combo, list, and/or textboxes.

    Scenario: VS2008

    The application has one primary form (frmMain) and a few dialogs. The only dialog that matters in this scenario is frmAddToHumidor. frmMain contains a tab control which includes two tabs (that matter in this scenario...) called TabMyCigars and TabHumidor.

    TabMyCigars contains a DGV with textboxes that are populated when an entry in the DGV is selected. TabHumidor simply contains the items sent from TabMyCigars with a little further information. This additional information is entered when someone clicks cmdAddToHumidor on TabMyCigars, which brings up the dialog frmAddToHumidor. This form basically takes all of the information from TabMyCigars and gives the user a dialog to add additional information (in this case, "humidor" and "Vendor").

    The shorter version in english is this...

    When someone chooses a record (by clicking on it in the DGV) in TabMyCigars then clicks cmdAddToHumidor, they get a small dialog box. This dialog box is ask a few questions, two of which are "What humidor?" and "Which vendor?".

    What I need is this:

    I need to allow the user to select from a list of humidors and/or vendors. I have tried binding a combobox, textbox (autocomplete), and a listbox to the database to no success.

    Perhaps someone has a better idea or could tell me what I'm doing wrong. I have no code because I was using databinding through the properties.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Filling combo, list, and/or textboxes.

    What type of database is it? You should be able to find the connection string here: www.connectionstrings.com and then use either the OLEDB classes or the SQL classes (depending on what type of database it is) to create a connection and build a command (such as "SELECT VendorNames FROM tblVendors" etc etc). Then you have a few options but one would be to use the SQLDataReader class to read through the results of your SQL query and populate the combobox's etc with the information you need.
    Before you come back and just ask for an example, try searching MSDN (as well as google and these forums) for info on the SQLClient namespace - in particular the SQLConnection, SQLCommand and SQLDataReader classes... assuming your using an SQL database anyway.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filling combo, list, and/or textboxes.

    Binding to a ComboBox or ListBox is exactly what you should do. Follow the Data Access link in my signature to see how to get the data from the database. Your case is the one where you want a populated DataTable with no need to save changes.

    As for binding the data, it's exactly the same for both the ComboBox and Listbox because they're both ListControls:
    vb.net Code:
    1. myListControl.DisplayMember = "NameOfColumnToDisplay"
    2. myListControl.ValueMember = "NameOfIDColumn"
    3. myListControl.DataSource = myDataTable
    When the user has made a selection you get the ID of the selected record from the ListControl's SelectedValue property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Filling combo, list, and/or textboxes.

    I thought most people on this site always said that you should stay away from data bindings as it doesnt give you as much flexibility as if you were doing it all manually and if it doesnt work you have no way of seeing why.
    Or am I thinking of something else?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filling combo, list, and/or textboxes.

    Quote Originally Posted by chris128
    I thought most people on this site always said that you should stay away from data bindings as it doesnt give you as much flexibility as if you were doing it all manually and if it doesnt work you have no way of seeing why.
    Or am I thinking of something else?
    Data-binding in VB6 had its issues and some people have carried over a bias to VB.NET. VB.NET data-binding is a different animal though.

    Saying that you shouldn't use data-binding because it doesn't provide enough control is like saying that you shouldn't drive a car because you won't be able to go to the toilet. If you need to go to the toilet then get out of the car and go. If you need to do something that can't be done with data-binding then don't use it in that specific instance. If you don't have a specific need of this alleged control then why write reams of code to reinvent the wheel? I use data-binding at every opportunity and I've never had a problem diagnosing an issue that I wouldn't have without it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Filling combo, list, and/or textboxes.

    As I think I mentioned in my original post, I have tried binding the listbox by setting the three properties you've mentioned in the Design mode and now in code as you've suggested like so:
    Code:
    lstHumidor.DisplayMember = "Location"
    lstHumidor.ValueMember = "ID"
    lstHumidor.DataSource = LocationListBindingSource
    I get nothing, the listbox remains unpopulated. I have verified names of both column and bindingsource. I've tried the same with the the combobox with the same results. I even tried deleting everything off the form including all the code (saving it elsewhere) and same result, nothing. Not making much sense to me, since as far as I can tell, all should be well with the code you've offered.

    That is, unless I've misunderstood what you meant by "mydatatable". If it matters, there are two tables that are linked two in this form. One called "Location" and the other "Vendor", both of which will have their own respective listbox or combobox.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Filling combo, list, and/or textboxes.

    Forgot to mention that I tried:

    Code:
            lstHumidor.DisplayMember = "Location"
            lstHumidor.ValueMember = "ID"
            lstHumidor.DataSource = MyCigarDS
    as well and the listbox was populated with one line as follows:

    System.Data.DataViewManager
    and I've also tried this after reading some of MSDN:

    Code:
            lstHumidor.DisplayMember = "Location"
            lstHumidor.ValueMember = "ID"
            lstHumidor.DataSource = MyCigarDS.Tables("LocationList")
    but the listbox was blank. For reference:

    Table = LocationList
    Column = Location
    ID = ID (Which is the PK if that matters)
    Last edited by Minolwen; May 28th, 2008 at 03:36 PM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filling combo, list, and/or textboxes.

    You can loop through the rows in that table in code and see Location and ID values?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Filling combo, list, and/or textboxes.

    If you mean by doing something like this:

    Code:
    SELECT        ID, Location
    FROM            LocationList
    Does it show this?:

    1 Box
    2 Car
    3 Roof
    Then yes... I typed the above SQL SELECT command into a query builder for that table in the dataset.xsd and it worked fine.



    I'm not sure if it is of any relevance but I just noticed when in the dataset design mode, that both "LocationList" and "VendorList" tables apparently have two PK's assigned. Not sure why this is, unless it's a direct result of each of them having their own individual relations with other tables. Locationlist has both, "ID" and "Location" as a PK in the dataset, and VendorList has "ID" and "Vendor". ID is the only PK field in Access which is the same across all tables in this database.
    Last edited by Minolwen; May 29th, 2008 at 07:10 AM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filling combo, list, and/or textboxes.

    It sounds to me like you need to regenerate your DataSet, which you do by reconfiguring your Data Source in the Data Sources window.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Filling combo, list, and/or textboxes.

    Unfortunately, I have done that already. Just to make sure though, I just did it again "configure with wizard..." and other methods as well. No results... I'm lost.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Filling combo, list, and/or textboxes.

    The two lists that I need can also be found on the primary form (not the form I'm currently working on) through a DGV that is populated on load. Would it be easier to extract the list from the DGV to populate the listbox/combobox?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Filling combo, list, and/or textboxes.

    Fixed, and the problem was due to my own ignorance. I was filling the datatable on the primary form, but not on the form that I was trying to load the listbox/combobox on.

    Now the form load event has:

    Code:
            Me.VendorListTableAdapter.Fill(Me.MyCigarDS.VendorList)
            Me.LocationListTableAdapter.Fill(Me.MyCigarDS.LocationList)
    to fill the datatable and:

    Code:
            Me.lstHumidor.DisplayMember = "Location"
            Me.lstHumidor.ValueMember = "ID"
            Me.lstHumidor.DataSource = Me.MyCigarDS.Tables("LocationList")
    
            Me.cmbVendor.DisplayMember = "Vendor"
            Me.cmbVendor.ValueMember = "ID"
            Me.cmbVendor.DataSource = Me.MyCigarDS.Tables("VendorList")
    to bind the data to the controls using the format suggested by MSDN. I tried your format and it works just as well.

    Thank you jmcilhinney for your help once again. I appreciate your effort as well chris128, but if I may suggest, try not to be so critical of those who ask questions. Not everyone is out for a quick answer with a complete example. Some of us still enjoy learning with the right direction...

  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Filling combo, list, and/or textboxes.

    Quote Originally Posted by Minolwen
    Thank you jmcilhinney for your help once again. I appreciate your effort as well chris128, but if I may suggest, try not to be so critical of those who ask questions. Not everyone is out for a quick answer with a complete example. Some of us still enjoy learning with the right direction...
    Who me? I didnt criticise your question, I was just saying that I thought a lot of people often advised against using data bindings. Im sure there is a link in one of the very knowledgeable user's signatures on here that says "Why Data Bound Controls Are Evil".
    Having never tried to use bound controls myself, I cannot comment on whether this is true or not but I guess if its good enough for jmcilhinney then its good enough for me.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filling combo, list, and/or textboxes.

    Quote Originally Posted by Minolwen
    Fixed, and the problem was due to my own ignorance. I was filling the datatable on the primary form, but not on the form that I was trying to load the listbox/combobox on.
    That's exactly why I specifically asked you whether you could loop through that DataTable and see the data. If you had attempted to loop through the actual DataTable that you were binding then you'd have seen immediately that it didn't have any data in it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width