Avatar of Peter Chan
Peter Chan
Flag for Hong Kong asked on

Problem to RowDataBound event

Hi,
I get this
Server Error in '/Start_dev' Application.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ArrayList.get_Item(Int32 index) +14330801
   Start._Default.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) +778
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +314
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +4780
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +95
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +19
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +222
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +313
   Start._Default.BindData() +417
   Start._Default.Page_Load(Object sender, EventArgs e) +139
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237

Open in new window


due to these codes
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //ds = GetDataSet();
            string selVal;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddl2");
                    if (ddl == null)
                    {
                        return;
                    }
                    else
                    {
                        selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                        this.BindDropDown(ddl, selVal);
                    }

                    Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                    if (Session["status"] != "Edit")
                        ddl.Enabled = false;
                    else
                        ddl.Enabled = true;
                }
                else
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
                    if (ddl == null)
                    {
                        return;
                    }
                    else
                    {
                        selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                        this.BindDropDown(ddl, selVal);
                    }
                    Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                    if (Session["status"] != "Edit")
                        ddl.Enabled = false;
                    else
                        ddl.Enabled = true;
                }
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddl3");
                if (ddl == null)
                {
                    return;
                }
                else
                {
                    selVal = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                    this.BindDropDown(ddl, selVal);
                }
                Label lb_rt = (Label)e.Row.FindControl("curr_rate");

                if (Session["status"] != "Edit")
                    ddl.Enabled = false;
                else
                    ddl.Enabled = true;
            }
        }
        private void BindDropDown(DropDownList par_ddl, string par_selVal)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "select [rec_id],[Item_code] as [Item_code] from [Actrate] order by 1";

            par_ddl.DataSource = GetData(cmd);
            par_ddl.DataTextField = "Item_code";
            par_ddl.DataValueField = "rec_id";
            par_ddl.DataBind();
            //par_ddl.Items.Insert(0, new ListItem("Please select"));
            par_ddl.Items.Insert(0, new ListItem("Please select"));
            par_ddl.SelectedValue = par_selVal;
        }

Open in new window

why?
C#.NET ProgrammingScripting Languages

Avatar of undefined
Last Comment
Lokesh B R

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Lokesh B R

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck