Display data in drop down

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

Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Display data in drop down

 
0
  #11
Feb 15th, 2008
Basically, what is happening here is that a ticket is being created for a company. Whenever the company is selected from a ddl, then their associated equipment will populate another ddl. At this point the user selects the appropriate equip and fills the rest of the ticket out.

I don't think I need two Select statements though. I have another table called cus_equip related to equipment and manufacturers that identifies the specific equipment with the right company. Tying all of that together to show in the ddl is where I'm falling short in my knowledge.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Display data in drop down

 
0
  #12
Feb 15th, 2008
Appendix A to my last post.

My table structure:

Manufacturer:
mfgID
Company
etc...

Equipment
eqpID
mfgID
Description
etc...

Cus_Equip
cuseqpID
SerialNumber
cusID
eqpID
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Display data in drop down

 
0
  #13
Feb 15th, 2008
Well, you either need two queries, or one query and a dataset stored in memory. Basically, for the price of that dataset, you're better off with the second query.

However, you want two separate DDL's, then two queries is the best way to go.

After the first DDL is populated, an onSelectedIndexChange should strike a sub that populates the second DDL. However, you cannot populate the second DDL before the first one is chosen, hence you need two separate connections to the server. You can do this all with 1 query, but there is no need to pull a thousand records when you only need 10. Since you are requesting them at two different times, make those queries as best as possible to grab only the information you need.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Display data in drop down

 
0
  #14
Feb 15th, 2008
The way I would do it would be like this:
  1. <form runat="server">
  2. <asp:DropDownList ID="ddl1" onselectedindexchanged="ddl1_change" autopostback="true" runat="server" />
  3. <asp:DropDownList ID="ddl2" runat="server" />
  4. ...
  5. ...
  6. </form>
  7.  
  8. <script runat="server" language="vb">
  9. Sub Page_Load(ByVal S As Object, ByVal E As EventArgs)
  10. Dim conn As New SqlConnection( connectionstring )
  11. Dim cmd As New SqlCommand ("SELECT company, mfgID FROM Manufacturers", conn)
  12. Dim dtrReader As SqlDataReader
  13.  
  14. Try
  15. conn.Open()
  16. dtrReader = cmd.ExecuteReader()
  17. if dtrReader.HasRows then
  18. ddl1.DataSource = dtrReader
  19. ddl1.DataTextField = "company"
  20. ddl1.DataValueField = "mfgID"
  21. ddl1.DataBind()
  22. end if
  23. dtrReader.Close()
  24. conn.Close()
  25. Catch
  26. End Try
  27. End Sub
  28.  
  29. Sub ddl1_change(ByVal S As Object, ByVal E As EventArgs)
  30. Dim conn As New SqlConnection( connectionstring )
  31. Dim cmd As New SqlCommand ("SELECT description, eqpID FROM Equipment WHERE mfgID=@mfgID", conn)
  32. ''
  33. ''However, you should change description to a name of the equipment, if there is a field for it.
  34. ''
  35. cmd.Parameters.AddWithValue("@mfgID", Trim(ddl1.SelectedItem.Value))
  36. Dim dtrReader As SqlDataReader
  37.  
  38. Try
  39. conn.Open()
  40. dtrReader = cmd.ExecuteReader()
  41. if dtrReader.HasRows then
  42. ddl2.DataSource = dtrReader
  43. ddl2.DataTextField = "equipment"
  44. ddl2.DataValueField = "equipID"
  45. ddl2.DataBind()
  46. end if
  47. dtrReader.Close()
  48. conn.Close()
  49. Catch
  50. End Try
  51. End Sub
  52. </script>

Wow I forgot how much code goes into a simple bind. I Created myself a "cheat sheet" where I just reference functions that do all my binding for me. I could have done these in 4 lines each
Last edited by SheSaidImaPregy; Feb 15th, 2008 at 4:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Display data in drop down

 
0
  #15
Feb 15th, 2008
Also, this is actually the second ddl being populated by the first one.

I have this working already for a contacts ddl:

  1. Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged

I'm still unsure how I can get the correct equipment tied to the ddl. I'm assuming is has to reference the cuseqpID in the Cus_Equip table.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Display data in drop down

 
0
  #16
Feb 15th, 2008
please post to me in words exactly what you want (all ddl's, how you want them populated, what they contain, etc.)

And ill pump you out the code for it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Display data in drop down

 
0
  #17
Feb 15th, 2008
I attached a visual of what I'm trying to accomplish. I didn't annotate that the Equipment ddl would be populated by the company. By the way the company ddl refers to the owner of the equipment not the manufacturer.
Attached Thumbnails
dropdown.gif  
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Display data in drop down

 
0
  #18
Feb 15th, 2008
Everything is working with the ddl population outside of the Equipment ddl (DropDownListEquip) being populated when the Company ddl (DropDownList1). As I said before, there is a Customer Equipment table (Cus_Equip) with these fields:

cuseqpID
eqpID
cusID
SerialNumber (doesn't need to be selected)

Manufacturer Table (Manufacturers)

mfgID
Company (this will be the first piece of text in the Equipment ddl)

Equipment Table (Equipment)

eqpID
mfgID
Description (This will be the second piece of text to show in the equipment ddl)


If something seems to be missing or doesn't jive with the db, let me know. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Display data in drop down

 
0
  #19
Feb 15th, 2008
Here is my working code for the ddl:

  1.  
  2.  
  3. Partial Class AddCall
  4. Inherits System.Web.UI.Page
  5. Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
  6. DropDownList2.SelectedIndex = 0
  7. End Sub
  8. Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
  9. 'labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text
  10.  
  11. Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox)
  12. Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox)
  13. Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox)
  14. Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox)
  15. Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
  16. Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
  17. Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox)
  18. Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox)
  19. Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox)
  20.  
  21. Dim str_sql As String = "SELECT * FROM Customers WHERE cusID = " & DropDownList1.SelectedValue
  22. Using connection As New System.Data.SqlClient.SqlConnection("Data Source=IT-P02\SQLEXPRESS;Initial Catalog=HRIService;Integrated Security=True")
  23. Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection)
  24. connection.Open()
  25. Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader()
  26. Try
  27. If reader.Read Then
  28. Company.Text = reader.Item("Company").ToString
  29. Address1.Text = reader.Item("Address1").ToString
  30. Address2.Text = reader.Item("Address2").ToString
  31. Phone.Text = reader.Item("Phone").ToString
  32. End If
  33. Finally
  34. ' Always call Close when done reading.
  35. reader.Close()
  36. End Try
  37. str_sql = "SELECT * FROM CONTACTS WHERE contactID = " & DropDownList2.SelectedValue
  38. command = New System.Data.SqlClient.SqlCommand(str_sql, connection)
  39. reader = command.ExecuteReader()
  40. Try
  41. If reader.Read Then
  42. FirstName.Text = reader.Item("FirstName").ToString()
  43. LastName.Text = reader.Item("LastName").ToString()
  44. End If
  45. Catch ex As Exception
  46. reader.Close()
  47. End Try
  48.  
  49. reader.Close()
  50. str_sql = "SELECT * FROM Zip WHERE zipID = " & DropDownList1.SelectedValue
  51. command = New System.Data.SqlClient.SqlCommand(str_sql, connection)
  52. reader = command.ExecuteReader()
  53. Try
  54. If reader.Read Then
  55. City.Text = reader.Item("City").ToString()
  56. State.Text = reader.Item("State").ToString()
  57. Zip.Text = reader.Item("Zip").ToString()
  58. End If
  59. Catch ex As Exception
  60. reader.Close()
  61. End Try
  62. End Using
  63.  
  64. End Sub
  65. Protected Sub DropDownListEquip_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  66.  
  67. End Sub
  68.  
  69. End Class
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Display data in drop down

 
0
  #20
Feb 15th, 2008
Well that depends on whether or not each manufacturer has different equipment. So once the first ddl is populated (Company), you can then populate the third ddl (equipment). Unless for some reason equipment is the same to all companies? If so, why do you have the mfgID in the quipment table? ^^
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