Results 1 to 2 of 2

Thread: combobox prob

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    7

    Angry 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!

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: combobox prob

    Quote 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:
    1. 'cn is a connection.
    2. 'in your form load event
    3. 'declare a datatable
    4.  
    5. Dim dt As New DataTable()
    6. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         dt.Columns.Add(New DataColumn("id"))
    8.     End Sub
    9.  
    10. 'it loads the first column of the table authors to the combobox
    11. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         Try
    13.             cn.Open()
    14.             Dim cm As New SqlCommand("select * from authors", cn)
    15.             Dim dr As SqlDataReader = cm.ExecuteReader
    16.             While dr.Read
    17.                 Me.ComboBox1.Items.Add(dr(0).ToString)
    18.             End While
    19.             dr.Close()
    20.             cn.Close()
    21.         Catch ex As Exception
    22.             MsgBox(ex.Message)
    23.         End Try
    24.     End Sub
    25.  
    26. 'in your combobox selectedindex change event
    27. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    28.         Dim dr As DataRow = dt.NewRow
    29.         dr(0) = ComboBox1.SelectedItem.ToString
    30.         dt.Rows.Add(dr)
    31.     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
  •  



Click Here to Expand Forum to Full Width