简体   繁体   English

将值从数据库插入到下拉列表失败

[英]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. abpve代码绑定状态但不绑定城市.city下拉列表仍显示--- Select ---。绑定城市后,我应该能够选择任何其他城市/州并再次更新。 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

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

then close your reader and connection and then again call your function which is created on ddlState_SelectedIndexChanged 然后关闭您的阅读器和连接,然后再次调用在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: 创建用于绑定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: 在dropdownlistselected索引更改内部调用此方法:

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: 现在在编辑部分绑定citydropdownlist,然后分配值:

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()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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