Click here to Skip to main content
13,042,166 members (92,141 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How do I concatenate two dropdown list values into a single value for entry into a database field? Also i want to split them into the two dropdown list accordingly when displaying in update mode.

For example: I want to enter the height of the person as 5'3" in height field of my database table. But i collect them seperately as feet(5) and inches(3) in two dropdownlists. How do i concatenate and send them to my database table's height field? Also when I retrieve them in update mode it should split and display accordingly as 5 in feet dropdownlist box and 3 in inches dropdownlist. Any ideas on how do I do this? Please help....i've been trying this for a while, still no idea.
Posted 29-Apr-13 18:18pm
Usha Muhunthan 30-Apr-13 0:37am
First store them in a string. Cosider 2 dropdownlist(5'3") names dropdownlist1(for 5) and dropdownlist2 (for 3)

string s = dropdownlist1.Text + " ' " + dropdownlist2.Text;

insert s into table

-------> In update mode just retrieve value from table split it where it find '
string[] height= str.Split(''');

----------> str is table value

Hope it helps to you, If not reply me
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can store these values in database concatenating with any separator (|)
Ex. store "5|3" in database

Now when you need to display it in edit mode then split these values by "|"

var data = heightValue.Split('|');

so you will get array of string

var heightInFeet = data[0];
var heightInInch = data[1];
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

<asp:DropDownList runat="server" ID="drop1">
        <asp:ListItem runat="server" Value="5">5</asp:ListItem>
        <asp:ListItem runat="server" Value="7">7</asp:ListItem>
    <asp:DropDownList runat="server" ID="drop2">
        <asp:ListItem runat="server" Value="10">10</asp:ListItem>
        <asp:ListItem runat="server" Value="12">12</asp:ListItem>
    <asp:Button runat="server" ID="save" Text="Save" onclick="save_Click"/>
    <asp:Button runat="server" ID="retrive" Text="Retrive" onclick="retrive_Click"/>

protected void save_Click(object sender, EventArgs e)
        string ddlValue = drop1.SelectedValue + "-" + drop2.SelectedValue;
    protected void retrive_Click(object sender, EventArgs e)
        string retriveValue = "7-12";
        string[] arrRetrive = retriveValue.Split('-');
        drop1.SelectedValue = arrRetrive[0];
        drop2.SelectedValue = arrRetrive[1];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100