Solved

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

Posted on 2014-07-29
2
1,068 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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