?
Solved

VB.net check if item is already in ComboBox

Posted on 2012-09-04
5
Medium Priority
?
512 Views
Last Modified: 2012-09-04
Hi

Is there a function or a quick way to see if an item is in a ComboBox that is loaded using the code below

 Sub oLoadProject()

        Try

            Dim sSQL As String
            sSQL = "Select Distinct [Project] From Projects"

            Dim connection As New SqlConnection("Server=..;....")
            Dim cmd As New SqlCommand(sSQL, connection)
            connection.Open()
            Dim datareader As SqlDataReader = cmd.ExecuteReader

            Me.cmbProject.Items.Clear()
            While datareader.Read
                If Not datareader("Project").Equals(DBNull.Value) Then
                    Me.cmbProject.Items.Add(datareader("Project"))
                End If
            End While
            connection.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
0
Comment
Question by:Murray Brown
  • 3
5 Comments
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 1000 total points
ID: 38363582
if your looking for the text:  
       cmbProject.FindString()

if your looking for the value:
                cmbProject.Items.IndexOf()

In either case, it will return the index of the item or -1 if not found.

Since your doing a select distinct anyway, the typical way to do this is to just clear the combo box and then reload it.

If you want to keep the value the same value, then you would grab the selected VALUE, (not the index as the position can change if items can change) and reset it after the combo box was loaded.
0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 1000 total points
ID: 38363629
ComboBox1.Items.Contains("abc")
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38363632
Sub oLoadProject()

        Try

            Dim sSQL As String
            sSQL = "Select Distinct [Project] From Projects"

            Dim connection As New SqlConnection("Server=..;....")
            Dim cmd As New SqlCommand(sSQL, connection)
            connection.Open()
            Dim datareader As SqlDataReader = cmd.ExecuteReader

            Me.cmbProject.Items.Clear()
            While datareader.Read
                If Not datareader("Project").Equals(DBNull.Value) Then
			If Not Me.cmbProject.Items.Contains(datareader("Project")) Then
                    		Me.cmbProject.Items.Add(datareader("Project"))
			End If
                End If
            End While
            connection.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub 

Open in new window

0
 
LVL 35

Expert Comment

by:YZlat
ID: 38363666
if the above does not work, try this:

Sub oLoadProject()

        Try

            Dim sSQL As String
            sSQL = "Select Distinct [Project] From Projects"

            Dim connection As New SqlConnection("Server=..;....")
            Dim cmd As New SqlCommand(sSQL, connection)
            connection.Open()
            Dim datareader As SqlDataReader = cmd.ExecuteReader

            Me.cmbProject.Items.Clear()
            While datareader.Read
                If Not datareader("Project").Equals(DBNull.Value) Then 
            		Dim b as Boolean = ContainsItem(cmbProject, datareader("Project"))

			If Not b Then
                    		Me.cmbProject.Items.Add(datareader("Project"))
			End If
                End If
            End While
            connection.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub 


Public Function ContainsItem(ByRef cb as ComboBox, ByVal Val As String) As Boolean
	Dim result as Boolean
	result=False
            for each  drv as System.Data.DataRowView In cb.Items

                if Val.Equals(drv(cb.ValueMember)) then

                    result=True

	   Next

            return result

End Function

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 38364119
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 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