|
-
Jul 21st, 2004, 11:51 AM
#1
Thread Starter
New Member
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
-
Jul 21st, 2004, 07:31 PM
#2
Fanatic Member
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?
-
Jul 21st, 2004, 09:07 PM
#3
Fanatic Member
this?
VB Code:
Dim cn As New SqlConnection()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "user id=sa;password=password;initial catalog=northwind"
cn.Open()
Dim cmdtext As String = "select * from territories"
Dim cm As New SqlCommand(cmdtext, cn)
Dim dr As SqlDataReader = cm.ExecuteReader
While dr.Read
ComboBox1.Items.Add(dr(0) & ", " & dr(1) & ", " & dr(2))
End While
IDontKnow()
End Sub
Sub IDontKnow()
Dim s As String
Dim s2 As String
For Each s2 In ComboBox1.Items
s &= s2 & Constants.vbCrLf
Next
MessageBox.Show(s)
End Sub
-
Jul 22nd, 2004, 07:52 AM
#4
Thread Starter
New Member
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.
-
Jul 22nd, 2004, 09:14 PM
#5
Fanatic Member
like this?
VB Code:
Dim cn As New SqlConnection()
Dim da As New SqlDataAdapter()
Dim ds As New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "user id=sa;password=password;initial catalog=pubs"
cn.Open()
da.SelectCommand = New SqlCommand("select * from publishers", cn)
ds.Tables.Add(New DataTable("publishers"))
ds.Tables.Add(New DataTable("titles"))
da.Fill(ds.Tables("publishers"))
ComboBox1.DataSource = ds.Tables("publishers")
ComboBox1.DisplayMember = "pub_name"
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim o As Object = ComboBox1.Items(ComboBox1.SelectedIndex)
da.SelectCommand = New SqlCommand("select * from titles where pub_id='" & o(0) & "'", cn)
ds.Tables("titles").Clear()
da.Fill(ds.Tables("titles"))
ComboBox2.DataSource = ds.Tables("titles")
ComboBox2.DisplayMember = "title"
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|