Results 1 to 4 of 4

Thread: [RESOLVED] Clear Combo Box before Load

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Resolved [RESOLVED] Clear Combo Box before Load

    Hi,

    I am having a function which populates a combobox (cmbProjtype) as shown below,

    I am trying to clear cmbProjtype before I populate again to remove duplicates whenever the function is called, but the line of code added (highlighted) is not doing it.. Any help pls

    Code:
     Private Sub populatecmbprojtype()
    
            Dim cmd As New SqlCommand
            Dim conn As SqlConnection = GetDbConnection()
    
    
            cmbProjtype.SelectedIndex = -1
    
            Try
                Dim Projtype As New System.Data.SqlClient.SqlCommand(("Select ProjectType From dbo.tblProjectTypes"), conn)
    
                Using reader As System.Data.SqlClient.SqlDataReader = Projtype.ExecuteReader()
    
                    While reader.Read()
    
                        Dim ProjectType As String = FixNull(reader.GetValue(0))
    
                        cmbProjtype.Items.Add(ProjectType)
    
                    End While
    
                End Using
    
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, "VeriSIS")
            End Try
        End Sub
    Thanks

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Clear Combo Box before Load

    Instead of this:
    Code:
                    While reader.Read()
    
                        Dim ProjectType As String = FixNull(reader.GetValue(0))
    
                        cmbProjtype.Items.Add(ProjectType)
    
                    End While
    do this:
    Code:
    Dim table As New DataTable
    
    table.Load(reader)
    cmbProjtype.DataSource = table
    You would set the ComboBox's DisplayMember to "ProjectType" in the designer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Clear Combo Box before Load

    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width