Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 3.5 and Visual Studio. 2008 > Visual Studio 2008
Visual Studio 2008 For discussing Visual Studio 2008. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2008 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 20th, 2008, 02:17 AM
Registered User
Join Date: Jun 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Gini
Default Adding data in a combo box from the database

I have a few questions regarding the code below.

1) In the [u]first</u> highlighted section of the code below the employee names are added into the combo box using a large string array.

    ' Declare comboBox1 as a ComboBox.    

Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

    ' This method initializes the combo box, adding a large string 
    ' array but limiting the drop-down size to six rows so the combo box
    ' doesn't cover other controls when it expands.

    Private Sub InitializeComboBox()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox

        Dim employees() As String = New String() {"Hamilton, David", _
            "Hensien, Kari", "Hammond, Maria", "Harris, Keith", _
            "Henshaw, Jeff D.", "Hanson, Mark", "Harnpadoungsataya, Sariya", _
            "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", _
            "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", _
            "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Harris, Keith", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Hasselberg, Jonas", "Harrington, Mark", _
            "Hedlund, Magnus", "Hay, Jeff", "Heidepriem, Brandon D."}


        Me.ComboBox1.Location = New System.Drawing.Point(136, 32)
        Me.ComboBox1.MaxDropDownItems = 5
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(136, 81)
        Me.ComboBox1.TabIndex = 0
    End Sub

    ' This method is called when the user changes his or her selection.
    ' It searches for all occurrences of the selected employee's
    ' name in the Items array and adds the employee's name and 
    ' the number of occurrences to TextBox1.Text.

    ' CAUTION   This code exposes a known bug: If the index passed to the 
    ' FindStringExact(searchString, index) method is the last index 
    ' of the array, the code throws an exception    .

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim comboBox As comboBox = CType(sender, comboBox)

        ' Save the selected employee's name, because we will remove
        ' the employee's name from the list.

        Dim selectedEmployee = CType(ComboBox1.SelectedItem, String)

        Dim count As Integer = 0
        Dim resultIndex As Integer = -1

        ' Call the FindStringExact method to find the first 
        ' occurrence in the list.

        resultIndex = ComboBox1.FindStringExact (ComboBox1.SelectedItem)

        ' Remove the name as it is found, and increment the found count. 
        ' Then call the FindStringExact method again, passing in the index of the
        ' current found item so the search starts there instead of 
        ' at the beginning of the list.

        While (resultIndex <> -1)
            count += 1
            resultIndex = ComboBox1.FindStringExact _
            (selectedEmployee, resultIndex)
        End While

        ' Update the text in Textbox1.
        TextBox1.Text = TextBox1.Text & Microsoft.VisualBasic.vbCrLf _
            & selectedEmployee & ": " & count

End Sub

End Class
How do I do the same for datas that are in the database(SQL Server 2000) instead? Do I have to use the below line of code? Or if not, what do I have to do to insert the data into the combo box?

Me.Inv_itemcategoryTableAdapter.Fill(Me.dsInventor y.inv_itemcategory)

Inv_itemcategory is the name of the table adapter holding the data. dsInventory is the name of the data source.

2) In the [u]second</u> highlighted section (updating the textbox), can I use the same method to update another combo box and also a text box instead of only updating a text box?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding value pairs to a combo box tebert2458 C# 2 February 25th, 2009 06:10 AM
Data type mismatch -- Combo box bound to database jamenijamjam Beginning VB 6 2 September 1st, 2006 08:36 PM
adding my own combo box in datagridview drachx Pro Visual Basic 2005 0 January 27th, 2006 03:04 AM
Combo Box Populated off Database Firehawk2006 Visual Basic 2005 Basics 1 November 28th, 2005 01:47 PM
Combo Box - Data In Use MrDannyP Beginning VB 6 1 November 15th, 2003 10:37 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.