Solved

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

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

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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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