Solved

How to set the selected text in a combo box in vb.net

Posted on 2014-07-29
2
1,006 Views
Last Modified: 2014-07-29
Hi all.

I would like to know how to select the text in a combo box that has just been entered in an input box using vb.net in Visual Studio 2010. As you can see towards the end of the code, I tried to set the selected record (cmbVendorName.SelectedIndex = cmbVendorName.FindString(newItem.ToString)) but when I run the windows form the combo box is still blank.

Code for loading combo box:
Private Sub cmbVendorName_Enter(sender As System.Object, e As System.EventArgs) Handles cmbVendorName.Enter

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim adapter As New SqlDataAdapter()
        Dim ds As New DataSet()

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        cmd.CommandText = "SELECT  VendorName FROM Vendor WHERE Status = 'Active' AND Division = @Division ORDER BY VendorName"
        cmd.Parameters.AddWithValue("@Division", "Ticket")

        adapter.SelectCommand = cmd
        adapter.Fill(ds)
        adapter.Dispose()
        cmd.Dispose()

        con.Close()

        cmbVendorName.DataSource = ds.Tables(0)
        cmbVendorName.DisplayMember = "VendorName"

        cmbVendorName.SelectedIndex = -1

    End Sub

Open in new window


Code for clicking the add vendor button
Private Sub btnAddVendor_Click(sender As System.Object, e As System.EventArgs) Handles btnAddVendor.Click

        Dim itm As String
        itm = InputBox("Enter New vendor", "New Vendor")
        If itm.Trim <> "" Then AddVendor(itm)

    End Sub

    Sub AddVendor(ByVal newItem As String)


        Dim con1 As New SqlConnection
        Dim cmd1 As New SqlCommand

        con1.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con1.Open()

        cmd1.Connection = con1

        cmd1.CommandText = "SELECT * FROM Vendor WHERE VendorName=@VendorName AND Division = @Division"
        cmd1.Parameters.AddWithValue("@VendorName", newItem)
        cmd1.Parameters.AddWithValue("@Division", "Ticket")

        Using reader As SqlDataReader = cmd1.ExecuteReader()
            If reader.HasRows Then
                MsgBox("Vendor name already exists", MsgBoxStyle.Exclamation, "Child Item Data Entry")
                Exit Sub
            End If
        End Using

        con1.Close()

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        cmd.CommandText = "INSERT INTO Vendor(VendorName, Division)  VALUES (@VendorName,@Division)"

        cmd.Parameters.AddWithValue("@VendorName", newItem)
        cmd.Parameters.AddWithValue("@Division", "Ticket")

        cmd.ExecuteNonQuery()

        con.Close()

cmbVendorName.SelectedIndex = cmbVendorName.FindString(newItem.ToString)
End Sub

Open in new window

0
Comment
Question by:printmedia
2 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
but you are never adding the new value to the combo!

cmbVendorName.Items.Add(newItem.ToString)
cmbVendorName.Text = newItem.ToString()
0
 

Author Closing Comment

by:printmedia
Comment Utility
Wow. Can't believe I missed that.

Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now