DropDownList

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 2
Reputation: yatt is an unknown quantity at this point 
Solved Threads: 0
yatt yatt is offline Offline
Newbie Poster

DropDownList

 
0
  #1
Sep 5th, 2006
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:
  1. Protected Sub cboJbtn_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboJbtn.SelectedIndexChanged
  2. ConnDb(cboJbtn.SelectedItem.Text)
  3. End Sub
  4.  
  5. Private Sub ConnDb(ByVal jbtn As String)
  6. Dim MyConn As SqlConnection = New SqlConnection("Data Source=WIN200;Initial Catalog=***;User ID=***;Password=***")
  7. 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"
  8.  
  9. Dim MyCmd As New SqlCommand(cmd, MyConn)
  10. 'MyCmd.CommandType = CommandType.Text
  11.  
  12. Try
  13. If MyConn.State = ConnectionState.Closed Then
  14. MyConn.Open()
  15.  
  16. End If
  17.  
  18. cboNama.DataSource = MyCmd.ExecuteReader()
  19. cboNama.DataTextField = "nama"
  20. cboNama.DataValueField = "nama"
  21. cboNama.DataBind()
  22. MyConn.Close()
  23.  
  24. Catch ex As Exception
  25. lblMsg.Text = "Error connecting to db"
  26. End Try
  27.  
  28. End Sub
  29. End Class
  30.  
The 1st DropDownList= cboJbtn
2nd DropDownList= cboNama
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 57
Reputation: Exelio is an unknown quantity at this point 
Solved Threads: 0
Exelio Exelio is offline Offline
Junior Poster in Training

Re: DropDownList

 
0
  #2
Sep 5th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2
Reputation: yatt is an unknown quantity at this point 
Solved Threads: 0
yatt yatt is offline Offline
Newbie Poster

Re: DropDownList

 
0
  #3
Sep 5th, 2006
Yes. I did set the autopostback of both the dropdownlist to true. It seem that my program will run the cboJbtn_SelectedItemIndex twice.

Originally Posted by Exelio View Post
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
Last edited by yatt; Sep 5th, 2006 at 10:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 95
Reputation: ManicCW is an unknown quantity at this point 
Solved Threads: 11
ManicCW's Avatar
ManicCW ManicCW is offline Offline
Junior Poster in Training

Re: DropDownList

 
0
  #4
Sep 8th, 2006
Add this:
cboNama.Items.Clear()
cboNama.DataSource = MyCmd.ExecuteReader()
Last edited by ManicCW; Sep 8th, 2006 at 5:55 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC