Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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(''');
dropdownlist1.Text=height[0].ToString();
dropdownlist2.Text=height[1].ToString();

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

Hope it helps to you, If not reply me

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 "|"

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


so you will get array of string

C#
var heightInFeet = data[0];
var heightInInch = data[1];
 
Share this answer
 
XML
<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>
    <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:DropDownList>
    <asp:Button runat="server" ID="save" Text="Save" onclick="save_Click"/>
    <asp:Button runat="server" ID="retrive" Text="Retrive" onclick="retrive_Click"/>






C#
protected void save_Click(object sender, EventArgs e)
    {
        string ddlValue = drop1.SelectedValue + "-" + drop2.SelectedValue;
        //database.save(ddlValue)

    }
    protected void retrive_Click(object sender, EventArgs e)
    {
        //retriveValue=database.retriveValue;
        string retriveValue = "7-12";
        string[] arrRetrive = retriveValue.Split('-');
        drop1.SelectedValue = arrRetrive[0];
        drop2.SelectedValue = arrRetrive[1];
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900