Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-29
2
Medium Priority
?
1,278 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 2000 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

Industry Leaders: 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month6 days, 8 hours left to enroll

783 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