locked
Dropdownlist int to text RRS feed

  • Question

  • User1298215938 posted
    <form id="form1" runat="server">  
        <table style="width: 100%; height: 86px;">  
            <tr>  
                <td class="style1">  
                    <asp:Label ID="Label1" runat="server" Text="Choose Your Country :"></asp:Label>  
                </td>  
                <td>  
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"   
                        DataTextField="country_name" DataValueField="country_id" AppendDataBoundItems="true"   
                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">  
                        <asp:ListItem Value="0">--Select Country--</asp:ListItem>  
                    </asp:DropDownList>  
                </td>  
                <td>  
                     </td>  
            </tr>  
            <tr>  
                <td class="style1">  
                    <asp:Label ID="Label2" runat="server" Text="Choose Your State :"></asp:Label>  
                </td>  
                <td>  
                    <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true" DataTextField="state_name"   
                        DataValueField="state_id" AutoPostBack="True"   
                        onselectedindexchanged="DropDownList2_SelectedIndexChanged">  
                        <asp:ListItem Value="0">-- Select State--</asp:ListItem>  
                    </asp:DropDownList>  
                </td>  
                <td>  
                     </td>  
            </tr>  
            <tr>  
                <td class="style1">  
                    <asp:Label ID="Label3" runat="server" Text="Choose Your City :"></asp:Label>  
                </td>  
                <td>  
                    <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true" DataTextField="city_name"   
                        DataValueField="city_id">  
                        <asp:ListItem Value="0">-- Select City--</asp:ListItem>  
                    </asp:DropDownList>  
                </td>  
                <td>  
                     </td>  
            </tr>  
        </table>  
        <div>  
       </div>  
    </form>  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!Page.IsPostBack)  
        {  
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
            SqlCommand cmd = new SqlCommand("select * from tbl_country", con);  
            SqlDataAdapter sda = new SqlDataAdapter(cmd);  
            DataTable dt = new DataTable();  
            sda.Fill(dt);  
            DropDownList1.DataSource = dt;  
            DropDownList1.DataBind();  
              
        }  
      
    }  
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        DropDownList2.Items.Clear();  
        DropDownList2.Items.Add("Select State");  
      
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
        SqlCommand cmd = new SqlCommand("select * from tbl_state where country_id=" + DropDownList1.SelectedItem.Value, con);  
        SqlDataAdapter sda = new SqlDataAdapter(cmd);  
        DataTable dt = new DataTable();  
        sda.Fill(dt);  
        DropDownList2.DataSource= dt;  
        DropDownList2.DataBind();  
    }  
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        DropDownList3.Items.Clear();  
        DropDownList3.Items.Add("Select State");  
      
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
        SqlCommand cmd = new SqlCommand("select * from tbl_city where state_id=" + DropDownList2.SelectedItem.Value, con);  
        SqlDataAdapter sda = new SqlDataAdapter(cmd);  
        DataTable dt = new DataTable();  
        sda.Fill(dt);  
      
        DropDownList3.DataSource = dt;  
        DropDownList3.DataBind();  
      
    }  

    my question is instead of using numbers for

    Create Table stateCity  
    02.(  
    03.   CityId Int,  
    04.   StateId Int Foreign Key References countryState(StateId),  
    05.   City Varchar(30)  
    06.)  
    
    Create Table countryState  
    02.(  
    03.   StateId Int Primary Key,  
    04.   CountryId Int Foreign Key References Country(CountryId),  
    05.   State Varchar(30)  
    06.)  
    
    Create Table Country  
    02.(  
    03.   CountryId Int Primary Key,  
    04.   County Varchar(30)  
    05.)  
    

    my question is instead of using numbers for the CITY,COUNTRY and State how can I use text for example if Illinois equals to Illinois than display data that belongs to Illinois? A asp.net Dropdown list will only allow a data and value field how can i add another data field?

    Tuesday, October 13, 2020 2:49 AM

All replies

  • User-1952463932 posted

    I'm not sure I understand your question.  Do you mean that in the dropdown you want to see more than one column?  Such as "USA Illinois Chicago" and the next line "USA Illinois Springfield" for example?

    Tuesday, October 13, 2020 6:26 AM
  • User1298215938 posted

    in the example above the tables are connected by a ID number how can I connect them by using a string value instead?

    Tuesday, October 13, 2020 1:01 PM
  • User-1952463932 posted

    There's nothing stopping you from using DataValueField="state_name" the question for me would be why do you want to do this?  Even when you have DataValueField="state_id" the dropdownlist's SelectedValue property will have the string representation of state_id and not the integer value.

    Tuesday, October 13, 2020 6:21 PM