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 ?

Community
  • 1
  • 1
Raja
  • 772
  • 1
  • 15
  • 38

1 Answers1

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())
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52