0

I want to add items in a ComboBox. I have two array().

Dim purpose_list() As String = {"ADMISSION", "1ST", "2ND", "3RD", "4TH", "5TH", "6TH", "7TH", "8TH", "9TH", "10TH", "11TH", "12TH"}

and

Dim find_purpose = GetListOfPurpose()

The function GetListOfPurpose() is like below..

Private Function GetListOfPurpose() As List(Of String)
    Dim Output As New List(Of String)()
    Try
        OpenConnection()
        Dim st_roll As String = TbRoll.Text
        Dim c_id As String = CmbCourse.SelectedValue

        Dim cmd As New MySqlCommand
        Dim qry As String = "SELECT purpose FROM fee_payment WHERE roll_no='" + st_roll + "' AND course='" + c_id + "'"
        cmd.Connection = conn
        cmd.CommandText = qry
        Dim dr As MySqlDataReader = cmd.ExecuteReader
        While dr.Read
            Output.Add(dr("purpose").ToString())
        End While
        dr.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        CloseConnection()
    End Try
    Return Output
End Function

Now I want to find strings in find_purpose and if exists, remove those strings and add the rest into a ComboBox.

Like if find_purpose contains "1ST" and "3RD", the ComboBox will add the rest items

"ADMISSION", "2ND", "4TH", "5TH", "6TH", "7TH", "8TH", "9TH", "10TH", "11TH", "12TH"

I have found This thread but it is in php.

What should I do it in VB.NET ?

3
  • ComboBox1.Items.AddRange(purpose_list.Except(find_purpose).ToArray()) Commented Oct 3, 2015 at 10:31
  • @HansPassant Thanks for your comment. But your code is skipping the "2ND". Commented Oct 3, 2015 at 12:55
  • The LIST.FIND can help you in a loop For Each... Try this: stackoverflow.com/questions/9854917/… Commented Oct 3, 2015 at 14:31

1 Answer 1

1

The code you posted:

Dim purpose_list() As String = {"ADMISSION", "1ST", "2ND", "3RD", "4TH", "5TH", "6TH", "7TH", "8TH", "9TH", "10TH", "11TH", "12TH"}
Dim find_purpose = GetListOfPurpose()

Private Function GetListOfPurpose() As List(Of String)
Dim Output As New List(Of String)()
Try
    OpenConnection()
    Dim st_roll As String = TbRoll.Text
    Dim c_id As String = CmbCourse.SelectedValue

    Dim cmd As New MySqlCommand
    Dim qry As String = "SELECT purpose FROM fee_payment WHERE roll_no='" + st_roll + "' AND course='" + c_id + "'"
    cmd.Connection = conn
    cmd.CommandText = qry
    Dim dr As MySqlDataReader = cmd.ExecuteReader
    While dr.Read
        Output.Add(dr("purpose").ToString())
    End While
    dr.Close()
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    CloseConnection()
End Try
Return Output
End Function

Now, in an Event-Handling Sub (or wherever you would like to do the checking) put this code:

Dim final_list As New List(Of String)
For Each item In purpose_list
    If Not(find_purpose.Contains(item)) Then
        final_list.Add(item)
    End If
Next
ComboBox1.Items.AddRange(final_list.ToArray())
Sign up to request clarification or add additional context in comments.

1 Comment

Wait i'm updating the answer. I just misunderstood the whole thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.