Click here to Skip to main content
15,916,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i have 2 dropdown lists:

1- ddlCity: that display all cities in the database
2-ddlBranch:display all branches per city.

So once i select a city, related branches should be selected.

and i have a method in C# source code that clear my form which contains the 2 dropdownlists. So once i click on the "Clear" button, the 2 dropdown list should be empty:

1-ddlcity display: "--Select--"
2-ddlBranch display: "----"

Now the problem is that When i click on the button "Clear" an error message occurs: "
SQL
'ddlBranch' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value


Please I need your help and thank you in advance.

Asp. Net Code:

ASP.NET
 <asp:DropDownList ID="ddlCity" runat="server" Width="170px" 
        DataSourceID="tblCity" DataTextField="city" DataValueField="cityID" AutoPostBack="true" AppendDataBoundItems="true"
        onselectedindexchanged="ddlCity_SelectedIndexChanged">
        <asp:ListItem Text="--Select--" Value=""></asp:ListItem>

 <asp:SqlDataSource ID="tblCity" runat="server" ConnectionString="<%$ ConnectionStrings:SmartBookingEngineConn %>" SelectCommand="SELECT [cityID],[city] FROM [tblCity]"></asp:SqlDataSource>
 
<asp:DropDownList ID="ddlBranch" runat="server" Width="170px" AutoPostBack="true" 
        onselectedindexchanged="ddlBranch_SelectedIndexChanged"  >
         <asp:ListItem Text="----" Value="" Selected="True"></asp:ListItem>
    </asp:DropDownList>

<telerik:RadAjaxManagerProxy ID="rp"  runat="server">              
</telerik:RadAjaxManagerProxy>


C# code:

C#
  protected void Page_Load(object sender, EventArgs e)
  {
 if (!IsPostBack)
                {
                                       
                    // dropdown branches per city load
                   
                    ddlCity.DataBind();
                    if (ddlCity.SelectedValue !="")
                    {
                    dtbranch = null;
                    string selectedIDCity = ddlCity.SelectedValue;
                    string mysqlSelectBranch = "select Branch,CityID,BranchID      from viewBranchPerCity where CityID=" + selectedIDCity;
                    dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
                    ddlBranch.DataSource = dtbranch;
                    ddlBranch.DataValueField = "BranchID";
                    ddlBranch.DataTextField = "Branch";
                    }
                    ddlBranch.DataBind();

                }
rp.AjaxSettings.AddAjaxSetting(ddlCity, ddlBranch, RadAjaxLoadingPanel1);

  }

protected void btnClear_Click(object sender, EventArgs e)
        {
                      clearForm();
        }

protected void clearForm()
       {
           ddlCity.Enabled = true;
           ddlBranch.Enabled = true;

           ddlCity.SelectedValue = string.Empty;
           ddlBranch.SelectedValue = string.Empty;
       }
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtbranch = null;
            string selectedIDCity = ddlCity.SelectedValue;
            string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
            dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
            ddlBranch.DataSource = dtbranch;
            ddlBranch.DataValueField = "BranchID";
            ddlBranch.DataTextField = "Branch";
            ddlBranch.DataBind();
        }
Posted
Comments
dimpledevani 21-Nov-12 4:01am    
try changing the datasource to null and then add new values '--SELECT--' ,etc as new datasource
[no name] 21-Nov-12 4:10am    
can you show me your clear button event solution.
Maksud Saifullah Pulak 21-Nov-12 4:11am    
<pre lang="vb">Try

ddlCity.Items.Clear();

before binding.</pre>

Hi,

Please update your code as below.
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      //ddlCity.DataBind();
      if (ddlCity.SelectedValue !="")
      {
         dtbranch = null;
         string selectedIDCity = ddlCity.SelectedValue;
         string mysqlSelectBranch = "select Branch,CityID,BranchID      from viewBranchPerCity where CityID=" + selectedIDCity;
         dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
         ddlBranch.DataSource = dtbranch;
         ddlBranch.DataValueField = "BranchID";
         ddlBranch.DataTextField = "Branch";
         ddlBranch.DataBind();  
      }
      //ddlBranch.DataBind(); //Move this line in if Block
   }
   rp.AjaxSettings.AddAjaxSetting(ddlCity, ddlBranch, RadAjaxLoadingPanel1); 
}
 
protected void btnClear_Click(object sender, EventArgs e)
{
   clearForm();
}
 
protected void clearForm()
{
   //ddlCity.Enabled = true;
   //ddlBranch.Enabled = true; 
   ddlCity.SelectedIndex= 0;
   ddlBranch.Items.Clear();
   ddlBranch.DataBind();
   ListItem li = new ListItem("----","");
   ddlBranch.Items.Add(li);
   ddlBranch.SelectedIndex= 0;
   ddlBranch.DataBind();
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
   dtbranch = null;
   string selectedIDCity = ddlCity.SelectedValue;
   string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
   dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
   ddlBranch.DataSource = dtbranch;
   ddlBranch.DataValueField = "BranchID";
   ddlBranch.DataTextField = "Branch";
   ddlBranch.DataBind();
}

Hope this will help you.
 
Share this answer
 
v5
Comments
Mohd. Mukhtar 21-Nov-12 4:29am    
if you want to keep all the values from the data base then you only need to put below line into clearForm Method
ddlCity.SelectedIndex= 0;
ddlCity.DataBind();
ddlBranch.SelectedIndex= 0;
ddlBranch.DataBind();
m_safaaaa 21-Nov-12 4:59am    
the first solution is working fine thank you for your help.
Hi,

you can try the following code. May be it will help you.

C#
ddlBranch.DataSource = null;
ddlCity.DataSource = null;
 
Share this answer
 
Comments
m_safaaaa 21-Nov-12 4:21am    
i like just to display as selected value for
1-ddlCity: "--Select--"
2-ddlbranch: "----"

with keeping the other items of each dropdown list from the database .
You can use this in page load method.

SQL
ddlCity.Items.Insert(0, "Select...");
ddlBranch.Items.Insert(0, "Select...");
 
Share this answer
 
Comments
m_safaaaa 21-Nov-12 4:17am    
unfortunatelly,same error with this code.
Yasir Farooq 22-Nov-12 7:39am    
You should clear ddl by
ddlCity.SelectedIndex = 0;
ddlBranch.SelectedIndex = 0;

Hope it work well
Hi, You can use the following for clear dropdown list.

C#
protected void clearForm()
       {
           ddlCity.Items.Clear();
           ddlBranch.Items.Clear();
           ddlCity.SelectedValue.Selected = false;
           ddlBranch.SelectedValue.Selected = false;
       }


Thank You.
 
Share this answer
 
Comments
m_safaaaa 21-Nov-12 4:24am    
actually the 2 lines are wrong, :
ddlCity.SelectedValue.Selected = false;
ddlBranch.SelectedValue.Selected = false;
and with the first two lines it clear everything in the dropdown lists.
and what i want is to display as selected value for
1-ddlCity: "--Select--"
2-ddlbranch: "----"

with keeping the other items of each dropdown list from the database .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900