?
Solved

Multiple values in db generated dropdownlist

Posted on 2007-10-16
4
Medium Priority
?
311 Views
Last Modified: 2008-01-09
I have a stored procedure which returns data to a dropdownlist in asp.net VB the back end looks like this:

Dim aDataAdapter As Data.SqlClient.SqlDataAdapter
        Dim aDataset As New Data.DataSet("MyDataSet")

        aDataAdapter = New Data.SqlClient.SqlDataAdapter("dbo.[listing]", strConn)
        aDataAdapter.SelectCommand.CommandType = Data.CommandType.StoredProcedure
        aDataAdapter.Fill(aDataset, "Groups")

        Dim Table As Data.DataTable = aDataset.Tables("Groups")

        DropDownList1.DataSource = Table
        DropDownList1.DataBind()

And the front end looks like this:
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="textstyle" AppendDataBoundItems="True" DataTextField="name" DataValueField="id">
<asp:ListItem Value="--">--Please Select--</asp:ListItem>
</asp:DropDownList>

This works fine but how can i get not only the name in the dropdown list but also other data as well, not in the value field but in the text part that the user sees: example:

Name | Country ID

Thanks in advance
0
Comment
Question by:david_evans_work
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Abu Hamdan
ID: 20084069
I assume that u need the text property for item?
DropDownList1.SelectedItem.Text

or something else?

0
 

Author Comment

by:david_evans_work
ID: 20084117
i think so, on the page source i want it to look like this:

<option value="1218066267">Name (12345)</option>
0
 
LVL 7

Accepted Solution

by:
skiltz earned 1000 total points
ID: 20084146
Your best to do this in your stored procedure

eg select index, field1 + '(' + field2 + ')' as TextValue
0
 
LVL 7

Assisted Solution

by:Abu Hamdan
Abu Hamdan earned 1000 total points
ID: 20085586
You have 2 properties that can be used to control the binding of text & value for dropdown list:

        DropDownList1.DataTextField = "";
        DropDownList1.DataValueField = ""

so in    
        Dim aDataAdapter As Data.SqlClient.SqlDataAdapter
        Dim aDataset As New Data.DataSet("MyDataSet")

        aDataAdapter = New Data.SqlClient.SqlDataAdapter("dbo.[listing]", strConn)
        aDataAdapter.SelectCommand.CommandType = Data.CommandType.StoredProcedure
        aDataAdapter.Fill(aDataset, "Groups")

        Dim Table As Data.DataTable = aDataset.Tables("Groups")

DropDownList1.DataTextField = myDataSetValue1 +"(" + myDataSetValue2 +")" ;

        DropDownList1.DataSource = Table
        DropDownList1.DataBind()


in DataTextField put the column names of the desired output from Dataset
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question