0

I have an asp.net page that has the following form fields:

  1. Dropdown 1. This drop down receives the data via SQL data source.
  2. Dropdown 2. Based on the selection of the drop down 1, this drop down queries to the database using SQL datasource and is populated.
  3. A panel control that is placed below drop down list 2 and it has a set of the controls.

My problem is there may be a situation when nothing is returned from the datasource of drop down 2, I want to show an Item "No Data found" in the dropdown list 2 and simultaneously hide the panel that is placed below dropdown 2.

Can someone please let me know how this situation can be handled.

Appreciate the help.

Thanks, Yagya

3
  • where is code ? you have not posted any code Commented Jul 14, 2012 at 15:03
  • Sorry..forgot to add the code I tried following code: protected void DropDown1_SelectedIndexChanged(object sender, EventArgs e) { if (Dropdown2.Items.Count == 0) { Panel1.Visible = false; Dropdown2.Items.Add("No Data Exists"); } } Commented Jul 14, 2012 at 15:14
  • also add this code to your question. Commented Jul 14, 2012 at 15:18

1 Answer 1

0

Add the following code to your dropdownlist1 selected index change event.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Run this code when your sql datasource 2 does not return record, you can also place an IfElse condition
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Value");
    DataRow row = dt.NewRow();
    row[0] = "-1";
    row[1] = "Data Not Found";
    dt.Rows.Add(row);
    DropDownList2.DataSource = dt;
    DropDownList2.DataTextField = "Value";
    DropDownList2.DataValueField = "ID";
    DropDownList2.DataBind();

}

Updated answer: ( Try this one ) You can also place it in your sqldatasource2 selecting event.

protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    if (e.Arguments.TotalRowCount == 0)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Value");
        DataRow row = dt.NewRow();
        row[0] = "-1";
        row[1] = "Data Not Found";
        dt.Rows.Add(row);
        DropDownList2.DataSource = dt;
        DropDownList2.DataTextField = "Value";
        DropDownList2.DataValueField = "ID";
        DropDownList2.DataBind();
    }
}

The above code will add a Item to your dropdownlist having Text = "Data Not Found"

Sign up to request clarification or add additional context in comments.

2 Comments

I have used the following code to determine the row count by SQLDatasource. I am not sure how to call the following function as suggested by you. protected void Y0079_dsGetDefectID_Selecting(object sender, SqlDataSourceStatusEventArgs e) { int affectedRecords = e.AffectedRows; }
if(e.Arguments.TotalRowCount == 0) { Y0090_pnlDefect.Visible = false; DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Value"); DataRow row = dt.NewRow(); row[0] = "-1"; row[1] = "Data Not Found"; dt.Rows.Add(row); Dropdown2.DataSource = dt; Dropdown2.DataTextField = "Value"; Dropdown2.DataValueField = "ID"; Dropdown2.DataBind(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.