Results 1 to 5 of 5

Thread: Comboboxes and Datasources

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Location
    tn
    Posts
    11

    Comboboxes and Datasources

    I am trying to pull all items from a combo box that is populated by a database. Is there any way to perform this function in vb.net 2003. thanks

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I am a bit confused by your question. Do you want to load items from the database into a combobox or get all the items that are in the combobox?

  3. #3
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    this?
    VB Code:
    1. Dim cn As New SqlConnection()
    2.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.       cn.ConnectionString = "user id=sa;password=password;initial catalog=northwind"
    4.       cn.Open()
    5.  
    6.       Dim cmdtext As String = "select * from territories"
    7.       Dim cm As New SqlCommand(cmdtext, cn)
    8.       Dim dr As SqlDataReader = cm.ExecuteReader
    9.       While dr.Read
    10.          ComboBox1.Items.Add(dr(0) & ", " & dr(1) & ", " & dr(2))
    11.       End While
    12.  
    13.       IDontKnow()
    14.    End Sub
    15.  
    16.    Sub IDontKnow()
    17.       Dim s As String
    18.       Dim s2 As String
    19.       For Each s2 In ComboBox1.Items
    20.          s &= s2 & Constants.vbCrLf
    21.       Next
    22.       MessageBox.Show(s)
    23.    End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Location
    tn
    Posts
    11
    What I am trying to do is pull information from dataadapter1 into a combobox, then based on the information in that combobox a sql statement will select fields from another table in the database. The data retrieved is added to a dataset, then populates into an invisible combobox or something that can hold various datareturns and can be syphoned off to the database at completion. Then based off of the information in the second combobox or whatever it may be, another sql statement is created to populate a third data set and combo box based on the selections of the previous two. I should probably not post code as it is company property.

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    like this?
    VB Code:
    1. Dim cn As New SqlConnection()
    2.    Dim da As New SqlDataAdapter()
    3.    Dim ds As New DataSet()
    4.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.       cn.ConnectionString = "user id=sa;password=password;initial catalog=pubs"
    6.       cn.Open()
    7.  
    8.       da.SelectCommand = New SqlCommand("select * from publishers", cn)
    9.       ds.Tables.Add(New DataTable("publishers"))
    10.       ds.Tables.Add(New DataTable("titles"))
    11.       da.Fill(ds.Tables("publishers"))
    12.       ComboBox1.DataSource = ds.Tables("publishers")
    13.       ComboBox1.DisplayMember = "pub_name"
    14.    End Sub
    15.  
    16.    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    17.       Dim o As Object = ComboBox1.Items(ComboBox1.SelectedIndex)
    18.       da.SelectCommand = New SqlCommand("select * from titles where pub_id='" & o(0) & "'", cn)
    19.       ds.Tables("titles").Clear()
    20.       da.Fill(ds.Tables("titles"))
    21.       ComboBox2.DataSource = ds.Tables("titles")
    22.       ComboBox2.DisplayMember = "title"
    23.    End Sub
    i don't quiet understand the information in the second combobox or whatever it may be in your post.

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