Results 1 to 26 of 26

Thread: [RESOLVED] Combobox routine failure

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Resolved [RESOLVED] Combobox routine failure

    I recently replaced the first routine below that I was using for a combobox, with the second routine below. The first one worked fine for the combobox, but did not meet my need to be able to display one value of the record, while using another value of the record, which caused me to change to the second routine. I.e., I wanted the colFullName value to be displayed in the combobox, but when I made a selection I wanted the valued from colContactID.

    When the code is executed, the column names are expressed for the values of both cboOwner.DisplayMember, and cboOwner.ValueMember. e.g., "colFullName", and "colContactID"

    Since this is a new approach for me, I have been unable to figure what I might have done to cause this issue.


    Code:
            'cboOwner.Items.Clear()
            MasterBase.AddParam("@active", True)
           'MasterBase.MasterBaseQuery("SELECT colFullName,colContactID FROMsitContacts WHERE colActive=@active")
            'If RecordCount > 0 Then
            '   For Each r As DataRow In MasterBase.ListDataSet.Tables(0).Rows
            '       cboOwner.Items.Add(r("colFullName"))
            '   Next
            'End If

    Code:
        Public Sub GetOwnerComboBox()
            cboOwner.Items.Clear()
            cboOwner.Text = ""
           MasterBase.AddParam("@active", True)
            MasterBase.MasterBaseQuery("SELECTcolFullName,colContactID FROM sitContacts WHERE colActive=@active")
            cboOwner.DisplayMember ="colFullName"
            cboOwner.ValueMember ="colContactID"
            cboOwner.DataSource =MasterBase.ListDataSet.Tables(0)
            cboOwner.Text = ContactName
        End Sub

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Combobox routine failure

    When the code is executed, the column names are expressed for the values of both cboOwner.DisplayMember, and cboOwner.ValueMember. e.g., "colFullName", and "colContactID"
    How are you coming to this conclusion? Where do you see those values.

    Code:
     cboOwner.Text = ContactName
    This doesn't look right. Been a while but I think with a databound combobox, if you want to default to a specific item in the collection you need to set the selectedindex to that item. I could be wrong on that.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,372

    Re: Combobox routine failure

    SelectedItem is the displayed item
    SelectedValue is the hidden id that you set

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox routine failure

    How are you coming to this conclusion? Where do you see those values.
    I see this in two places. The first is that is what is displayed on the form as cbo.Owner.Text. Additionally, I can see this as I step through the code.
    Everything I can detect suggests to me that cboOwner.DisplayMember is expressed as the string "colFullName", and that cboOwner.ValueMember is expressed as the string "colContactID". However, I have been frequently wrong in my assessments.

    This doesn't look right.
    That is true. I missed that as a leftover from something else I tried. Having said that, I took that out and nothing changes.

    SelectedItem is the displayed item
    SelectedValue is the hidden id that you set
    I assumed that I should Exchange those values in the code to be as seen below
    cboOwner.SelectedItem = "colFullName"
    cboOwner.SelectedValue = "colContactID"

    That change got me to a little bit different result. The combox displayed a list repating, "System.Data.DataR...", I was unable to read the rest of the line. I would guess that the combobox list is the same length as the dataset from the query.
    I have seen this before, quite some time ago, and do not recall the significantce.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,896

    Re: Combobox routine failure

    No, you had it right the first time. You should be setting the .DisplayMember and .ValueMember properties just as you were initially.

    I'm fairly certain that the issue is arising from what you are assigning to the .DataSource property. Given your "MasterBase" database interaction approach, I'm not sure what the correct object is that you would assign to .DataSource, but what you are assigning to it in the code in your first post just doesn't seem right to me.

    Edit to add:

    ...Or maybe it is correct. Note that the code in your opening post is missing a space after SELECT. I'm assuming that is just a typo in your posted code. If that space is actually missing in your production code, then your query can't be working at all in the first place.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox routine failure

    No, you had it right the first time.
    I just found that out and have already changed it back. I do have some more information.

    I found that the combobox does indeed have the list of contact names (colFullName).
    1. The combobox is displaying, "colFullName" as cboOwner.txt. Having said that, I could not find where cboOwner.Text was assigned a value when the controls were bound. However, whatever I selected from the combobox list was saved (if I save it) as the string "colFullName".

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,896

    Re: Combobox routine failure

    Quote Originally Posted by gwboolean View Post
    I just found that out and have already changed it back. I do have some more information.

    I found that the combobox does indeed have the list of contact names (colFullName).
    1. The combobox is displaying, "colFullName" as cboOwner.txt. Having said that, I could not find where cboOwner.Text was assigned a value when the controls were bound. However, whatever I selected from the combobox list was saved (if I save it) as the string "colFullName".
    I'm not following what you are describing. Are you able to post a screenshot of the combobox while the dropdown list is displayed?

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Combobox routine failure

    I'm curious if the combobox is also bound? Not talking about the item list.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox routine failure

    Are you able to post a screenshot
    Yeah, that wasn't too clear to me either, and I wrote it. Probably because I am not easily getting what is going on. So what you can see is the string colFullName where the value for cboOwner.txt should display. Everything below that is the list that meets the criteria and I would say conforms to cboOwner.DisplayMember.

    I can see a couple of possibilities as to why the colFullName might be there, but nothing that I can clearly describe. But what will occur, should I select any of the names on the list, is that the value displayed as colFullName will be overwritten by the selected name from the list. If I save this change, the value saved to the record will be the string colFullName instead of the the selected value. I am going through my update agains to make sure that I am not doing something of dubious nature there.

    I do have a couple of other methods I could use to do the same thing as I want to here, but this method, if I can get it right, is direct and much easier.

    Attachment 188589

  10. #10
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,896

    Re: Combobox routine failure

    The screenshot didn't attach properly. Never done one myself, but I've seen numerous other people say that attachments are broken when posting a quick reply and you have to click "Go Advanced" and attach it there.

    I'm pretty sure part of what you are describing (and has already been pointed out earlier in this thread) is because of the line:

    Code:
    cboOwner.Text = ContactName
    Comment that line out.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,372

    Re: Combobox routine failure

    To set the display value, and the hidden value…

    ComboBox1.DisplayMember = “a field name”
    ComboBox1.ValueMember = “a different field name”

    To change the selectedIndex of the bound ComboBox1…

    You could use ComboBox1.SelectedValue = “01”

    Notice I used a string in accordance with your fields datatype

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Combobox routine failure

    A couple of questions.

    Again,, is the combobox itself bound? If so, please show how it's setup.

    Are you trying to display a new record or and existing one?

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,385

    Re: Combobox routine failure

    Quote Originally Posted by gwboolean View Post
    I just found that out and have already changed it back. I do have some more information.

    I found that the combobox does indeed have the list of contact names (colFullName).
    1. The combobox is displaying, "colFullName" as cboOwner.txt. Having said that, I could not find where cboOwner.Text was assigned a value when the controls were bound. However, whatever I selected from the combobox list was saved (if I save it) as the string "colFullName".
    cboOwner.Text will be the displayed value that's sselected... if your'e looking for the "hidden" value.... then use cboOwner.Value ... that will return the value that's bound to the column spacified by the .ValueMember setting ...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Combobox routine failure

    Quote Originally Posted by techgnome View Post
    if your'e looking for the "hidden" value.... then use cboOwner.Value
    SelectedValue, not Value.

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

    Re: Combobox routine failure

    Quote Originally Posted by gwboolean View Post
    When the code is executed, the column names are expressed for the values of both cboOwner.DisplayMember, and cboOwner.ValueMember. e.g., "colFullName", and "colContactID"

    Since this is a new approach for me, I have been unable to figure what I might have done to cause this issue.
    There is no issue.

    The DisplayMember is the NAME of a column or property from which the control draws the data to display. The VALUES from that column or property are accessed via the Text property of the control. If the user selects an item, the Text property contains the value from the column or property of the SelectedItem named in the DisplayMember. If you set the Text property in code, assuming that the DropDownStyle is set to DropDownList, then the item that contains that value in the column or property named in the DisplayMember will be selected.
    There is no issue.

    The ValueMember is the NAME of a column or property from which the control draws the data to identify the selected item. The VALUES from that column or property are accessed via the SelectedValue property of the control. If the user selects an item, the SelectedValue property contains the value from the column or property of the SelectedItem named in the ValueMember. If you set the SelectedValue property in code then the item that contains that value in the column or property named in the ValueMember will be selected.

    Here's a code example that demonstrates the above:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Dim table As New DataTable
    3.  
    4.     table.Columns.Add("Id", GetType(Integer))
    5.     table.Columns.Add("Name", GetType(String))
    6.  
    7.     table.Rows.Add(1, "First")
    8.     table.Rows.Add(2, "Second")
    9.  
    10.     With ComboBox1
    11.         .DisplayMember = "Name"
    12.         .ValueMember = "Id"
    13.         .DataSource = table
    14.     End With
    15. End Sub
    16.  
    17. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    18.     ComboBox1.Text = "First"
    19. End Sub
    20.  
    21. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    22.     ComboBox1.SelectedValue = 2
    23. End Sub
    24.  
    25. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    26.     MessageBox.Show($"{ComboBox1.SelectedValue} {ComboBox1.Text}")
    27. End Sub
    You can make selections via the UI or via those Buttons and see how the item selection changes.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox routine failure

    I'm curious if the combobox is also bound?
    The combobox is not bound. I generally do not do that with a combobox. The original method I was using (see original post), does not do that.

    The screenshot didn't attach properly.
    I will try it again.

    Attachment 188590

    To set the display value, and the hidden value…
    OK. Could I then do this?
    cbo.Owner.ValueMember = "FieldName"
    cbo.Owner.SelectedValue = cbo.Owner.ValueMember

    That seems logical to me, but I have not tried that yet.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox routine failure

    Attachment 188591

    Still trying to get that screenshot to show. I never had this problem before.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Combobox routine failure

    Quote Originally Posted by gwboolean View Post
    Still trying to get that screenshot to show. I never had this problem before.
    The Insert Image tool on the basic editor doesn't work. You have to click Go Advanced and then Manage Attachments.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Combobox routine failure

    Quote Originally Posted by gwboolean View Post
    The combobox is not bound.
    If you set the DataSource property then it's bound. That's what data binding is.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Combobox routine failure

    Quote Originally Posted by gwboolean View Post
    OK. Could I then do this?
    cbo.Owner.ValueMember = "FieldName"
    cbo.Owner.SelectedValue = cbo.Owner.ValueMember

    That seems logical to me, but I have not tried that yet.
    No, because that makes absolutely no sense at all. Did you read what I posted? The value you assign to ValueMember is the NAME of a column or property and the SelectedValue is a value from the column or property with that name. Look at the example I provided in post #15. I assigned "Id" to ValueMember because the values were supposed to come from the 'Id' column. Line 22 assigns the value 2, i.e. one of the values from the 'Id' column, to the SelectedValue and that selects the item with that 'Id'. Line 26 displays the current SelectedValue which, had you tested the example, you would have seen was either 1 or 2, i.e. the values in the 'Id' column.

  21. #21
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Combobox routine failure

    The combobox is not bound. I generally do not do that with a combobox. The original method I was using (see original post), does not do that.
    Your original post is not the type of binding I'm talking about (as I said in post #8). I'm not talking about the item collection.

    When you display a record from the database, how do you assign the value to the combobox? Or do you?

    Are you binding the combobox?
    Code:
    Combobox1.DataBindings.Add("Text", somebindingsource, "fieldName")
    What I'm trying to find out is what other places in your code impact the combobox.
    Last edited by wes4dbt; Aug 29th, 2023 at 02:37 PM.

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox routine failure

    I have gotten everything to work as intended. There were a lot more functions/routines effected by this change than I anticipated.

    I'm curious if the combobox is also bound? Not talking about the item list.
    I was wrong. It was indeed bound. However, it had been Rem'd out and I had missed it.

    Comment that line out.
    I did, thanks.

    I appreciate the suggestions, they all applied. I think the lesson learned though, is that there are NO small changes. Damn, I just couldn't believe how many things were affected by that seemingly simple change in one method.

    I am a believer now.

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: Combobox routine failure

    Quote Originally Posted by wes4dbt View Post
    Your original post is not the type of binding I'm talking about (as I said in post #8). I'm not talking about the item collection.

    When you display a record from the database, how do you assign the value to the combobox? Or do you?

    Are you binding the combobox?
    Code:
    Combobox1.DataBindings.Add("Text", somebindingsource, "fieldName")
    What I'm trying to find out is what other places in your code impact the combobox.
    I misunderstood what you were getting at there too. I thought you were referring to using DataSource rather than Items. With regards to what you are referring to, it would almost certainly be the SelectedValue that you bound if you have set the ValueMember, as it would generally be a representation of a foreign key relation.

  24. #24
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: Combobox routine failure

    Quote Originally Posted by jmcilhinney View Post
    I misunderstood what you were getting at there too. I thought you were referring to using DataSource rather than Items. With regards to what you are referring to, it would almost certainly be the SelectedValue that you bound if you have set the ValueMember, as it would generally be a representation of a foreign key relation.
    Your right. I chose the "Text" property in my example on purpose, I was guessing that the combobox was improperly bound. I had made that mistake in the past.

  25. #25
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,148

    Re: [RESOLVED] Combobox routine failure

    I appreciate the suggestions, they all applied. I think the lesson learned though, is that there are NO small changes. Damn, I just couldn't believe how many things were affected by that seemingly simple change in one method.
    Yes changes can be tricky. As tg has pointed out your data access technique can make problems more likely.

    Try to localize your data for something like this, there is no reason to use a program wide datatable.
    Code:
        Private Sub LoadCBItems()
            Dim da As New OleDbDataAdapter("Select UserId, [name] from Users", con)
            Dim dt As New DataTable
    
            da.Fill(dt)
    
            Me.UserIdComboBox.DisplayMember = "name"
            Me.UserIdComboBox.ValueMember = "userId"
            Me.UserIdComboBox.DataSource = dt
        End Sub

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: [RESOLVED] Combobox routine failure

    Quote Originally Posted by wes4dbt View Post
    As tg has pointed out your data access technique can make problems more likely.
    The fact that this rather dodgy DAL has formed part of so many questions lately is part of the issue with trying to solve them. No question should include both that DAL and UI. If there's an issue getting the data then we should be shown the data access code only, so we can see the relevant inner workings of that DAL. If the DAL is getting the correct data it is irrelevant to the question and shouldn't be included. If the issue is getting data from a DataTable into a ComboBox then all we should see is a DataTable and a ComboBox. The opaque method by which the DataTable is populated only serves to obfuscate the actual issue. Breaking down a problem into parts to determine where the actual issue is a critical part of software development and that is something that needs to be worked on here. This DAL should be fully tested independently to confirm it works as expected and then it becomes irrelevant to any questions about how the data it returns is used. If you're trying to diagnose issues with a DAL and a UI at the same time, you're doing it wrong from the outset.

Tags for this Thread

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