Error in logic populate Listbox if record does not exist in my Datagrid

Joined
Jun 26, 2008
Messages
1
Reaction score
0
I have a Distribution list table who's members I display using a Datagrid. When I display the list of possible user to be added to the Distribution list, I use a ListBox but I want to remove the listing of any user who is already a member.

I tried doing an SQL statement that excludes any user who is already a member of this DistList, but because there are many more DistLists, my user could be a member of another DistList and therefore show up anyhow.

Code:
     strSQL = ""
        strSQL = " SELECT DISTINCT tblNames_distLists.names_id, tblNames.lname, tblNames.fname"
        strSQL = strSQL & " FROM tblNames INNER JOIN"
        strSQL = strSQL & " tblNames_distLists ON tblNames.names_id = tblNames_distLists.names_id"
        'strSQL = strSQL & " WHERE (tblNames_distLists.distlist_id <> '" & iDistList_id & "')"

I then tried to Iterate through the Datagrid and exclude any names who's ID match that in the Database, but I'm still missing something again..
Code:
       Try
            If objConnection.State = ConnectionState.Closed Then
                objConnection.Open()

            End If
            objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

            Do While objDataReader.Read
                For Each myDataGridItem In DataGridMembers.Items
                    Dim names_ID As Label = DirectCast(myDataGridItem.FindControl("names_ID"), Label)

                    If objDataReader("names_id") = names_ID.Text Then
                        Exit For
                    Else
                        Dim lItem As New ListItem
                        With lItem
                            .Value = objDataReader("names_id")
                            .Text = objDataReader("fname") & " " & objDataReader("lname")
                        End With
                        All_Users.Items.Add(lItem)
                    End If
                Next
            Loop

        Catch ex As Exception
            lblMessage.Text = ex.Message
        Finally

            objConnection.Close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top