Click here to Skip to main content
14,427,893 members
Rate this:
Please Sign up or sign in to vote.
See more:
 protected void ddlbranchdate_SelectedIndexChanged(object sender, EventArgs e)
           DataTable dtgetvalues = new DataTable();
           // = 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.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

private void SetPreviousData()
           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();


please help me...
Updated 26-Feb-14 2:24am
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 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.
Siva Hyderabad 26-Feb-14 8:07am
yes,please help me?how to call same postback
ZurdoDev 26-Feb-14 8:10am
OK. See Solution.
Bojjaiah 26-Feb-14 7:54am
what is an error?
Siva Hyderabad 26-Feb-14 7:56am
my title is an error

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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.
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
Siva Hyderabad 26-Feb-14 8:41am
we call multiple times ddl selected changed event,so it's binded data
ZurdoDev 26-Feb-14 8:43am
You'll need to rework the code so you aren't databinding it more than one time during a postback.
ZurdoDev 26-Feb-14 8:42am
Calling databind more than one time on a ddl can cause the error too.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100