I have 2 DropDownList in a single page.The 1st contain department name and the 2nd contain employee name.When I click the 1st DropDownList , the 2nd will display the employee name which are under that department.The problem is the 2nd DropDownList will display redundant data and when I choose another department, the new employee name will add to the previous employee list in the 2nd DropDownList.

My code:

Protected Sub cboJbtn_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboJbtn.SelectedIndexChanged        
        ConnDb(cboJbtn.SelectedItem.Text)
End Sub

    Private Sub ConnDb(ByVal jbtn As String)
        Dim MyConn As SqlConnection = New SqlConnection("Data Source=WIN200;Initial Catalog=***;User ID=***;Password=***")
        Dim cmd As String = "SELECT DISTINCT dataStaf.nama FROM dataStaf INNER JOIN service ON dataStaf.idStaf = service.idStaf INNER JOIN databahagian ON service.kdjbts = databahagian.kodBahagian WHERE (dataStaf.status = '1') AND (databahagian.namaBahagian ='" & jbtn & "') ORDER BY dataStaf.nama"
       
        Dim MyCmd As New SqlCommand(cmd, MyConn)
        'MyCmd.CommandType = CommandType.Text

        Try
            If MyConn.State = ConnectionState.Closed Then
                MyConn.Open()
               
            End If

            cboNama.DataSource = MyCmd.ExecuteReader()
            cboNama.DataTextField = "nama"
            cboNama.DataValueField = "nama"
            cboNama.DataBind()
            MyConn.Close()

        Catch ex As Exception
            lblMsg.Text = "Error connecting to db"
        End Try

    End Sub
End Class

The 1st DropDownList= cboJbtn
2nd DropDownList= cboNama

hi yatt,

The coding which u sent seems to be ok.Did you set the autopostback property of both the dropdownlists to be true.

Hope this solved ur problem.

Thanks

Regards

Exelio

Yes. I did set the autopostback of both the dropdownlist to true. It seem that my program will run the cboJbtn_SelectedItemIndex twice.

The coding which u sent seems to be ok.Did you set the autopostback property of both the dropdownlists to be true.

I have put a control in cboJbtn_SelectedItemIndex so that it will not call the ConnDb function twice. But I appreciate it if anyone can tell me how to prevent it without using any control.

Thanks in advance, and also thanks to Exelio for your reply

Add this:

[B]cboNama.Items.Clear()[/B]
cboNama.DataSource = MyCmd.ExecuteReader()