Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Please, i have a table in a database where a user ID can appear more than once.
My problem is about populating a list box i.e. in the listbox, an ID should appear only once although it appeared more than once on the table in the database.

The code below works fine but and ID can appear more than once on the list box.
VB
Private Sub loadQueue()

     Try
         'open database
         conMod.openDB()

         Dim myadapter As New SqlDataAdapter("SELECT ID FROM patorder  ", conMod.Con)

         Dim dtset As New DataSet
         myadapter.Fill(dtset)
         myadapter.Dispose()

         Dim myQueue As DataTable = dtset.Tables(0)

         ListPatient.DataSource = myQueue
         ListPatient.DisplayMember = "ID"

         'close database
         conMod.closeDB()

     Catch ex As Exception


         Dim mymsg As New myMsgbox


         With mymsg

             .txtError.Text = "Error! Could not load Queue."
             .ShowDialog()

         End With


     End Try

 End Sub
Posted

First of all its quite strange that you dont have unique values column (primary key) in your table. You should have one, otherwise its not a good system design.


Change your query to this

VB
Dim myadapter As New SqlDataAdapter("SELECT DISTINCT ID FROM PatOrder (NOLOCK)   ", conMod.Con)


and then bind appropriate column with your list box

VB
ListPatient.DisplayMember = "ID"



This will also solve your problem, and much better than using Reader.
 
Share this answer
 
v2
Comments
tarhex 24-Mar-13 0:49am    
thanks big bro. I have a unique ID as the primary key for registered person.
[no name] 24-Mar-13 0:58am    
then? why are you not binding it with listbox ?
tarhex 24-Mar-13 2:11am    
i wanted to display the user ID. The user can have more than one entry in the table.
tarhex 24-Mar-13 2:19am    
OK, i will try that out. Thanks big bro
tarhex 24-Mar-13 5:43am    
it worked perfectly
I was able to solve this problem with the code below;

VB
Try

            'open connection to database
            conMod.openDB()

            'patient ID
            Dim memberID As String


            'check to see if patient ID has be given before


            Dim cmd As SqlCommand = New SqlCommand("SELECT ID FROM patorder ", conMod.Con)

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            'loop through IDs' and only add unique ID
            Do While reader.Read


                memberID = reader(0)

                If Not ListPat.Items.Contains(memberID) Then

                    ListPat.Items.Add(memberID)

                End If


            Loop



            'close the connection to database
            conMod.closeDB()


        Catch ex As Exception


            Dim mymsg As New myMsgbox


            With mymsg

                .txtError.Text = "Error! Could not load Queue."
                .ShowDialog()

            End With


        End Try
 
Share this answer
 
Comments
[no name] 24-Mar-13 1:16am    
ok. so you want the same IDs but only unique ones.
[no name] 24-Mar-13 1:18am    
Check my updated solution. Data Reader is not a good approach for this scenario
Dave Kreskowiak 24-Mar-13 11:40am    
THis is an inefficient approach. You were given a better solution to the problem in "Solution 1". Look at the SQL SELECT statement in there. It filters out all the duplicates without you having to check for them when adding to the List.
[no name] 24-Mar-13 22:33pm    
Thanks Dave

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900