Friday, 10 August 2012

"One dropdownlist based upon action of another dropdownlist"

Create Procedure In Database:



create proc sp_BindJobTypeddlCountry
as
select CountryId,CountryName from tblCountry

create proc sp_sp_BindJobTypeddlState
@CountryId int
as
select StateId,StateName from tblState
where CountryId = @CountryId

default.aspx


    <td>
                Country
            </td>
            <td>
                <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlCountry_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                State
            </td>
            <td>
                <asp:DropDownList ID="ddlState" runat="server">
                </asp:DropDownList>
            </td>
        </tr>


default.aspx.cs


 //**************** ddl Fill Country *******
    public void MFill_DropCountry()
    {
        try
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("sp_BindJobTypeddlCountry", (SqlConnection)Ncconnection.cconnection.MConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);

            ddlCountry.DataValueField = "CountryId";
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataSource = ds.Tables[0];
            ddlCountry.DataBind();

            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlCountry.Items.Insert(0, "----Select----");
               
            }
            else
            {
                ddlCountry.Items.Insert(0, "No Country");
            }
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }


    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        MFill_DropState(int.Parse(ddlCountry.SelectedValue.ToString()));

    }

    //**************** ddl Fill State *******
    public void MFill_DropState(int ddlCountryId)
    {
        try
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("sp_sp_BindJobTypeddlState", (SqlConnection)Ncconnection.cconnection.MConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CountryId",ddlCountryId);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);

            ddlState.DataValueField = "StateId";
            ddlState.DataTextField = "StateName";
            ddlState.DataSource = ds.Tables[0];
            ddlState.DataBind();

            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlState.Items.Insert(0, "----Select----");
            }
            else
            {
                ddlState.Items.Insert(0, "No State");
            }
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }

No comments:

Post a Comment