Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 protected void ddlbranchdate_SelectedIndexChanged(object sender, EventArgs e)
   {
       try
       {
           DataTable dtgetvalues = new DataTable();
           //objRetailPL.date = Convert.ToDateTime(hdndate.Value);
           objRetailPL.branchdate = ddlbranchdate.SelectedItem.ToString();
           dtgetvalues = objRetailBAL.getbradisdate(objRetailPL);
           foreach(GridViewRow row in gvMeatDispatch.Rows)
           {
               DataTable dtpr = new DataTable();
               DropDownList ddl1 = (DropDownList)row.FindControl("ddlpartyname");
               objRetailPL.branch = dtgetvalues.Rows[0]["branch"].ToString();

               dtpr = objRetailBAL.getbran(objRetailPL);
               ddl1.DataSource = dtpr;
               ddl1.DataTextField = "partyname";
               ddl1.DataValueField = "sno";
               ddl1.DataBind();
               ddl1.Items.Add(new ListItem("--Select--", "0"));
              ddl1.SelectedIndex = ddl1.Items.Count - 1;
           }
       }
}



why i am getting this exception?..

i am using Dynamic Gridview and adding Rows Dynamically with the help of Button Control..

Outside i am declare another dropdown list ..if user selected some value from this Dropdownlist
then automatically bind the values to Dynamic Gridview ,Dropdown list values..


This is my Setprevious Data in Dynamic control Method..Here i called Dropdownlist selectedindexchanged event ..like..

C#
private void SetPreviousData()
   {
       try
       {
           int rowIndex = 0;
           if (ViewState["CurrentTable"] != null)
           {
               DataTable dt = (DataTable)ViewState["CurrentTable"];
               if (dt.Rows.Count > 0)
               {
                   for (int i = 0; i < dt.Rows.Count; i++)
                   {
                       DropDownList ddlpname = (DropDownList)gvMeatDispatch.Rows[rowIndex].Cells[1].FindControl("ddlpartyname");
                       DropDownList ddlbt = (DropDownList)gvMeatDispatch.Rows[rowIndex].Cells[2].FindControl("ddlbirdtype");
                       TextBox txttotwt = (TextBox)gvMeatDispatch.Rows[rowIndex].Cells[3].FindControl("txttotwt");
                       TextBox txttransbirds1 = (TextBox)gvMeatDispatch.Rows[rowIndex].Cells[4].FindControl("txtrateforkg");
                       TextBox txtmort1 = (TextBox)gvMeatDispatch.Rows[rowIndex].Cells[5].FindControl("txtdcno");

                      // Dropdownlist selectedIndex Changed event

                       ddlbranchdate_SelectedIndexChanged(null, null);

                       ddlpname.SelectedValue = dt.Rows[i]["Col1"].ToString();
                       ddlbt.SelectedValue = dt.Rows[i]["Col2"].ToString();
                       txttotwt.Text = dt.Rows[i]["col3"].ToString();
                       txttransbirds1.Text = dt.Rows[i]["Col4"].ToString();
                       txtmort1.Text = dt.Rows[i]["Col5"].ToString();

                       rowIndex++;
                   }
               }
           }
       }


please help me...
Posted
Updated 26-Feb-14 1:24am
v2
Comments
ZurdoDev 26-Feb-14 7:52am    
What is the exact error and which line of code causes it?
Siva Hyderabad 26-Feb-14 7:53am    
ddl1.SelectedIndex = ddl1.Items.Count - 1; this line..
if dynamically adding row,then call SetPreviousData()..In this i called selectedindex changed event –
ZurdoDev 26-Feb-14 7:56am    
And what is the exact error?
Siva Hyderabad 26-Feb-14 8:00am    
i am getting exception..my title is an exception..
Multiple times call dropdownlist selected changed event,tha't why i am getting this exception
ZurdoDev 26-Feb-14 8:02am    
So, you can't set the selected value and selected index during the same postback.

1 solution

Since you can't set the SelectedItem twice during the same postback you need to check for it first. For example:

C#
if (ddl.SelectedItem != null)
   ddl.SelectedItem.Selected = false;


So, check to see if there is a selected item and either do nothing or clear it out and set it again.
 
Share this answer
 
Comments
Siva Hyderabad 26-Feb-14 8:24am    
it's not effect the exception?
ZurdoDev 26-Feb-14 8:26am    
Are you databinding more than one time during postback?
Siva Hyderabad 26-Feb-14 8:35am    
yes,dynamically add No:of rows in Dynamic grid..
ZurdoDev 26-Feb-14 8:36am    
I'm not sure what that means. Are you calling .Databind on the same instance of the ddl more than once per postback?
Siva Hyderabad 26-Feb-14 8:38am    
databind more than one time

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