| | |
Display data in drop down
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
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.
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.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.
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.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
The way I would do it would be like this:
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
ASP.NET Syntax (Toggle Plain Text)
<form runat="server"> <asp:DropDownList ID="ddl1" onselectedindexchanged="ddl1_change" autopostback="true" runat="server" /> <asp:DropDownList ID="ddl2" runat="server" /> ... ... </form> <script runat="server" language="vb"> Sub Page_Load(ByVal S As Object, ByVal E As EventArgs) Dim conn As New SqlConnection( connectionstring ) Dim cmd As New SqlCommand ("SELECT company, mfgID FROM Manufacturers", conn) Dim dtrReader As SqlDataReader Try conn.Open() dtrReader = cmd.ExecuteReader() if dtrReader.HasRows then ddl1.DataSource = dtrReader ddl1.DataTextField = "company" ddl1.DataValueField = "mfgID" ddl1.DataBind() end if dtrReader.Close() conn.Close() Catch End Try End Sub Sub ddl1_change(ByVal S As Object, ByVal E As EventArgs) Dim conn As New SqlConnection( connectionstring ) Dim cmd As New SqlCommand ("SELECT description, eqpID FROM Equipment WHERE mfgID=@mfgID", conn) '' ''However, you should change description to a name of the equipment, if there is a field for it. '' cmd.Parameters.AddWithValue("@mfgID", Trim(ddl1.SelectedItem.Value)) Dim dtrReader As SqlDataReader Try conn.Open() dtrReader = cmd.ExecuteReader() if dtrReader.HasRows then ddl2.DataSource = dtrReader ddl2.DataTextField = "equipment" ddl2.DataValueField = "equipID" ddl2.DataBind() end if dtrReader.Close() conn.Close() Catch End Try End Sub </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.
Also, this is actually the second ddl being populated by the first one.
I have this working already for a contacts ddl:
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.
I have this working already for a contacts ddl:
ASP.NET Syntax (Toggle Plain Text)
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.
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.
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.
Here is my working code for the ddl:
ASP.NET Syntax (Toggle Plain Text)
Partial Class AddCall Inherits System.Web.UI.Page Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged DropDownList2.SelectedIndex = 0 End Sub Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged 'labelSelection.Text = DropDownList1.SelectedItem.Text & " " & DropDownList2.SelectedItem.Text Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox) Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox) Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox) Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox) Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox) Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox) Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox) Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox) Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox) Dim str_sql As String = "SELECT * FROM Customers WHERE cusID = " & DropDownList1.SelectedValue Using connection As New System.Data.SqlClient.SqlConnection("Data Source=IT-P02\SQLEXPRESS;Initial Catalog=HRIService;Integrated Security=True") Dim command As New System.Data.SqlClient.SqlCommand(str_sql, connection) connection.Open() Dim reader As System.Data.SqlClient.SqlDataReader = command.ExecuteReader() Try If reader.Read Then Company.Text = reader.Item("Company").ToString Address1.Text = reader.Item("Address1").ToString Address2.Text = reader.Item("Address2").ToString Phone.Text = reader.Item("Phone").ToString End If Finally ' Always call Close when done reading. reader.Close() End Try str_sql = "SELECT * FROM CONTACTS WHERE contactID = " & DropDownList2.SelectedValue command = New System.Data.SqlClient.SqlCommand(str_sql, connection) reader = command.ExecuteReader() Try If reader.Read Then FirstName.Text = reader.Item("FirstName").ToString() LastName.Text = reader.Item("LastName").ToString() End If Catch ex As Exception reader.Close() End Try reader.Close() str_sql = "SELECT * FROM Zip WHERE zipID = " & DropDownList1.SelectedValue command = New System.Data.SqlClient.SqlCommand(str_sql, connection) reader = command.ExecuteReader() Try If reader.Read Then City.Text = reader.Item("City").ToString() State.Text = reader.Item("State").ToString() Zip.Text = reader.Item("Zip").ToString() End If Catch ex As Exception reader.Close() End Try End Using End Sub Protected Sub DropDownListEquip_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) End Sub End Class
![]() |
Similar Threads
- Winsock Multi-Client Servers (C++)
- Make data from a pull down list mandatory (Visual Basic 4 / 5 / 6)
- Retrieve data from a table in mysql and edit the contents (PHP)
- Please help me :"login form use drag and drop toolbox" ! (C#)
- PHP mysql drop down populates another drop down (PHP)
- Reading from a text file and using it as a database (Visual Basic 4 / 5 / 6)
- help with treeview property setting (Python)
- Filtering My sql through Php using drop down menu and text field (PHP)
- Display Search Results as Hyperlinks to Records (ASP)
- JSP and Oracle (JSP)
Other Threads in the ASP.NET Forum
- Previous Thread: asp.net
- Next Thread: LiveCD and ASP.NET
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 activexcontrol advice ajax alltypeofvideos anathor appliances application asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions dataaccesslayer database datagridview datagridviewcheckbox datalist development dgv dialog dropdownlist dynamically edit expose feedback fileuploader fill flash form formatdecimal formview google gridview gudi iframe iis image javascript list listbox login microsoft mono mouse mssql multistepregistration news numerical opera panelmasterpagebuttoncontrols parent project radio redirect registration relationaldatabases reportemail richtextbox save schoolproject search security select sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos view vista visualstudio web webapplications webdevelopemnt webprogramming webservice xsl youareanotmemberofthedebuggerusers






