Solved

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

Posted on 2014-07-29
2
1,094 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40227241
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
ID: 40227255
Wow. Can't believe I missed that.

Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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