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