简体   繁体   中英

Inserting value from database to dropdownlist fails

I have state and city dropdownlists(cascaded) as below :

 protected void BindStateDropDown()
{
    string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tblState", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlState.DataSource = ds;
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateId";
        ddlState.DataBind();
    }
    ddlState.Items.Insert(0, new ListItem("---Select---", "0"));
    ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
}


protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
    int StatId = Convert.ToInt32(ddlState.SelectedValue);
    string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("spCities", con);
        cmd.Parameters.AddWithValue("@StatId", StatId);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlCity.DataSource = ds;
        ddlCity.DataTextField = "CityName";
        ddlCity.DataValueField = "CityId";
        ddlCity.DataBind();
    }
    ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
}

which i use to insert values to the database as below :

 cmd.Parameters.AddWithValue("@State", ddlState.SelectedItem.Text);
 cmd.Parameters.AddWithValue("@City", ddlCity.SelectedItem.Text);

This works fine and inserts the state and city in the database table.Now i try to show back the values on the edit page as below :

 ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(rdr["State"].ToString()));
 ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(rdr["City"].ToString()));

The abpve code binds the state but not the City.The city dropdown still shows ---Select---.Once the city is bound, i should be able to select any other city/state and update it again. Any help will be appreciated

Try this

ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(rdr["State"].ToString()));

then set city value to hidden value hf_city

hf_city.Value = rdr["City"].ToString();

then close your reader and connection and then again call your function which is created on ddlState_SelectedIndexChanged

 int StatId = Convert.ToInt32(ddlState.SelectedValue);
    string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("spCities", con);
        cmd.Parameters.AddWithValue("@StatId", StatId);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlCity.DataSource = ds;
        ddlCity.DataTextField = "CityName";
        ddlCity.DataValueField = "CityId";
        ddlCity.DataBind();
    }
    ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));

 ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(hf_city.Value));

Create another method for binding cityDropdownlist:

protected void BindCityDropDown(int StatId)
{
    string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("spCities", con);
        cmd.Parameters.AddWithValue("@StatId", StatId);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlCity.DataSource = ds;
        ddlCity.DataTextField = "CityName";
        ddlCity.DataValueField = "CityId";
        ddlCity.DataBind();
    }
    ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
}

Call this inside dropdownlistselected index change:

protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
    int StatId = Convert.ToInt32(ddlState.SelectedValue);
    BindCityDropdown(StatId);
}

Now in edit section bind citydropdownlist then assign value:

ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(rdr["State"].ToString()));
int StatId = Convert.ToInt32(ddlState.SelectedValue);
BindCityDropdown(StatId);
 ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(rdr["City"].ToString()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM