|
-
Mar 4th, 2005, 01:38 AM
#1
Thread Starter
New Member
combobox prob
hi,
i want to know how i can populate my combobox using data directly from database (sql server ) and after how can i store the selectedindex (ie item selected by user) in another table. if u have any codes, post it here for me. i badly need help. HELP ME PLTZZZZZ!
-
Mar 4th, 2005, 02:13 AM
#2
Re: combobox prob
 Originally Posted by shei786
hi,
i want to know how i can populate my combobox using data directly from database (sql server ) and after how can i store the selectedindex (ie item selected by user) in another table. if u have any codes, post it here for me. i badly need help. HELP ME PLTZZZZZ!
try this one.
VB Code:
'cn is a connection.
'in your form load event
'declare a datatable
Dim dt As New DataTable()
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add(New DataColumn("id"))
End Sub
'it loads the first column of the table authors to the combobox
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
cn.Open()
Dim cm As New SqlCommand("select * from authors", cn)
Dim dr As SqlDataReader = cm.ExecuteReader
While dr.Read
Me.ComboBox1.Items.Add(dr(0).ToString)
End While
dr.Close()
cn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'in your combobox selectedindex change event
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim dr As DataRow = dt.NewRow
dr(0) = ComboBox1.SelectedItem.ToString
dt.Rows.Add(dr)
End Sub
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
|