Results 1 to 11 of 11

Thread: [RESOLVED] Populating combo box where a specfic value is met?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Resolved [RESOLVED] Populating combo box where a specfic value is met?

    Hey everyone

    I was wondering whether someone could show me a way of populating a combo box where a specific value is met.

    Name:  vb.png
Views: 643
Size:  22.4 KB

    I want to fill the combo box with the MDModel_Name value Where the UserID is equal to MDUser_ID. That way this means that only the models that have been uploaded by the owner are shown. Below is the form:

    Name:  vb1.png
Views: 701
Size:  105.8 KB

    Thank you for reading

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Populating combo box where a specfic value is met?

    Are you asking for the proper SELECT statement or how to bind your combobox to a datatable?

  3. #3
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Populating combo box where a specfic value is met?

    It might be easier if you show us the code you have so far, and we can help you make it work.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Populating combo box where a specfic value is met?

    Code:
     Public Sub fillModelName()
            'links to database
            link_to_database()
    
    
            Dim cmd As SqlCommand = New SqlCommand("SELECT MDUser_ID FROM MDModels WHERE MDUser_ID='" & DatabaseModule.UserID & "'", DatabaseModule.con)
            DatabaseModule.con.Open()
            Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim myDataSet As DataSet = New DataSet()
            myDA.Fill(myDataSet, "MDModels")
    
            ddbCurrentModel_EditModel.DataSource = myDataSet.Tables("MDModels").DefaultView
            ddbCurrentModel_EditModel.ValueMember = "MDModel_Name"
            ddbCurrentModel_EditModel.DisplayMember = "MDUser_ID"
    
            DatabaseModule.con.Close()
    This is my code so far, but all it returns in the combo box is "System.Data.DataRowView". If I try and catch it I get the error message "Cannot bind to the new display member. Parameter name: newDisplayMember". If the SQL statement is:

    Code:
      Dim cmd As SqlCommand = New SqlCommand("SELECT MDUser_ID FROM MDModels, DatabaseModule.con)
    as expected it populates the combo box, however it is populated with every item (MDModel_Name) in MDModels instead of just the ones that are owned by the user (which is decided where the UserID = MDUser_ID)

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Populating combo box where a specfic value is met?

    You're comparing an integer value with a string value in MDUser_ID='" & DatabaseModule.UserID & "'"
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Populating combo box where a specfic value is met?

    First, you need to adjust your Display Member. Since you are only interested in the MDModel_Name, Then you should do this:

    vb.net Code:
    1. ddbCurrentModel_EditModel.ValueMember = "MDModel_Name"
    2. ddbCurrentModel_EditModel.DisplayMember = "MDModel_Name"

    As for your SELECT statement, you are actually returning all Models because you cannot concatenate parameters into a SELECT statement. You have to add them to the command:


    vb.net Code:
    1. Dim cmd As New SqlCommand
    2. cmd.CommandText = "SELECT MDModel_Name FROM MDModels WHERE MDUser_ID = @user"
    3. cmd.Parameters.Add("@user", SqlDbType.Integer)
    4. cmd.Parameters("@user").Value = DatabaseModule.UserID
    5. cmd.Connection = DatabaseModule.con

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Populating combo box where a specfic value is met?

    Thank you, it worked! The only thing I had to change was the "SqlDbType.Integer" to "SqlDbType.Int". Great code and I understand how it works. I wanted to try and use parameters before but I've never used them before. Thank you 'Circuits2'

    and thank you 'dunfiddlin', I haven't slept much because I've been trying to get this finished, and I miss stupid mistakes like that.


  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Populating combo box where a specfic value is met?

    Out of curiosity could you show me some sample code on how you would fill the other text boxes/drop down lists where the Model name AND UserID are met please?

  9. #9
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [RESOLVED] Populating combo box where a specfic value is met?

    You would need to change your SELECT statement to SELECT * instead of SELECT MDModel_Name so the other information is available in your dataset.

    vb.net Code:
    1. Private Sub ddbCurrentModel_EditModel_SelectedIndexChanged (sender As Object, e As EventArgs) Handles ddbCurrentModel_EditModel.SelectedIndexChanged
    2.  
    3. If Not Me.ddbCurrentModel_EditModel.SelectedIndex = -1 Then
    4.  
    5.  For Each dr As DataRow In MyDataSet.Table(0).Rows()
    6.  
    7.     If dr.Item("MDModel_Name") = ddbCurrentModel_EditModel.SelectedValue Then
    8.  
    9.         Me.AnotherFieldTextBox.Text = dr.Item("AnotherField").ToString()
    10.  
    11.     End If
    12.  
    13.  Next
    14.  
    15. End If
    16.  
    17. End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: [RESOLVED] Populating combo box where a specfic value is met?

    Is it possible to update using parameters as well? I would like to be able to update the database with the new data "Where MDModel_Name = Old name AND MDUser_ID = DatabaseModule.UserID"?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: [RESOLVED] Populating combo box where a specfic value is met?

    Since I set this as resolved, I've set up a new post here (http://www.vbforums.com/showthread.p...59#post4382259), could you check it out please, and thank so much for helping me!

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