Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can anyone know this.

how to solve index was out of range. must be non-negative and less than the size of
the collection?

code is:

What I have tried:

HiddenField hfId = (HiddenField)gridData.Rows[gridData.SelectedIndex].FindControl("hfId");

error coming on this line

 protected void lnkUpdate_Click(object sender, EventArgs e)
        {
HiddenField hg = (HiddenField)gridData.Rows[gridData.SelectedIndex].FindControl("hfId");
 try
                {
                    
                    List<SqlParameter> parameters = new List<SqlParameter>
                    {
                    };
                    parameters.Add(new SqlParameter("@P_APPLICATION_ID", hg));
                    parameters.Add(new SqlParameter("@P_APPLICATION_NAME", txtApplicationName.Text));
                    parameters.Add(new SqlParameter("@P_VERSION", txtAppVersion.Text));
                    parameters.Add(new SqlParameter("@P_DESCRIPTION", txtDesc.Text));
                    parameters.Add(new SqlParameter("@P_USER", lblName.Text));
                    parameters.Add(new SqlParameter("@P_MODE", "E"));

                    string strResponse = objAppFunctionModule.ExecuteNonQueryMethod(strUSP_APPLICATION_MST, parameters);
                    if (strResponse == "1")
                    {
                        divMessage.Visible = true;
                        divDelete.Visible = false;
                        lnkSave.Visible = true;
                        lnkAddNew.Visible = false;
                        lnkUpdate.Visible = false;
                        PopulateGrid();
                        ClearControls();
                    }
                    else
                    {
                        lblResponse.Text = "Record Not updated.";
                    }
                }


                catch (Exception ex)
                {
                    lblResponse.Text = ex.Message;
                }
Posted
Updated 1-Feb-18 23:58pm
v2
Comments
F-ES Sitecore 2-Feb-18 5:34am    
Look at how many rows are in gridData.Rows, next look at the value of gridData.SelectedIndex. The reason why you get the error should be obvious.
Laxmidhar tatwa technologies 2-Feb-18 12:06pm    
Why do not use gridviename_rowupfating method
Pahi2317 16-Feb-18 1:52am    
Thanks For your reply...but is already solved.

1 solution

The error message is pretty explicit: the value you are using to index the array is not correct - it needs to be between 0 and the number of elements minus one.

At a guess, there is no selected item, so teh index returns -1 and you get the error - but without running your code exactly as you do, we can't tell. And we can't do that!

So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
CPallini 2-Feb-18 5:58am    
5.

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