简体   繁体   中英

Dropdownlist showing data for empty table

I have a dropdownlist code like

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        fill1();
        fill();
        fill2();
    }
}

protected void fill1()
{
    string str = "select distinct CompanyName from Company";
    SqlDataReader dr1 = conn.query(str);
    dwn.dropdwnlist(str, ddcompany);

}

protected void fill()
{
    string company = ddcompany.SelectedValue.ToString();
    string str = "select CompanyID from Company where CompanyName='" + company + "'";
    SqlDataReader dr1 = conn.query(str);
    if (dr1.Read())
    {
        string id = dr1[0].ToString();

        string str1 = "select distinct Zone from Zone where CompanyID='" + id + "'";
        SqlDataReader dr = conn.query(str1);
        dwn.dropdwnlist(str1, ddzone);
    }

}
protected void fill2()
{
    string zone = ddzone.SelectedValue.ToString();
    string str = "select ZoneID  from Zone where Zone='" + zone + "'";
    SqlDataReader dr1 = conn.query(str);
    if (dr1.Read())
    {
        string id = dr1[0].ToString();
        string str1 = "select distinct  Region from Region where ZoneID='" + id + "'";
        SqlDataReader dr = conn.query(str1);
        dwn.dropdwnlist(str1, ddregion);
    }

}

I have two company named Infosys and wipro, and both companies have North,south and west zone respectively.In north and south zone i have some values but My west zone for both companies are empty but when i select west zone after selection south, the south zone values getting under west zone.North and south zone have same value with different id but when i select zone for both companies it not showing values by id .

您可以添加一个空的检查从查询返回的数据。如果它为空,则无需绑定数据并将下拉列表设置为空

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