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?
LVL 11
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Did you set the DataKeyNames in GridView?

Set the following and try.

<asp:GridView ID="GridView1" DataKeyNames="rec_id"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.