Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Server Error in '/GridView' 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: 


Line 128:        //string currentExtension;
Line 129:
Line 130:        Id = Convert.ToInt32(GridView1.DataKeys[1].Value);
Line 131:
Line 132:        //currentTitleOfCourtesy = ((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text;
 

Source File: e:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\WebSites\Subbu\SelectEdit\GridView\Edti3.aspx.cs    Line: 130 

Stack Trace: 

advance thanks for your reply
Posted
Updated 13-Nov-11 8:35am
v2

It seems like the DataKeys collection is empty.


Try to change your code to something like:


C#
if (GridView1 != null && GridView1.DataKeys.Count > 1)
{
    Id = Convert.ToInt32(GridView1.DataKeys[1].Value);
}
 
Share this answer
 
v2
Comments
2011999 13-Nov-11 11:49am    
already mention int Id; i got this message


Description: An error occurred during the compilation of a resource required to ser

vice this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0165: Use of unassigned local variable 'Id'

Source Error:



Line 140: //currentExtension = ((TextBox)r.FindControl("ExtensionTextBox")).Text;
Line 141:
Line 142: DataRow row = originalDataTable.Select(String.Format("Id = {0}", Id))[0];
Line 143:
Line 144: //if (!currentTitleOfCourtesy.Equals(row["TitleOfCourtesy"].ToString())) { return true; }


Source File: e:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\WebSites\Subbu\SelectEdit\GridView\Edti3.aspx.cs Line: 142
2011999 13-Nov-11 12:03pm    
use of unsigned variable cid this is error message
Could you show your code on the page. verify whether you have set the DataKeyNames(for grridview) or not. you can get the DataKeys collection only if you set this property.
 
Share this answer
 
Id = Convert.ToInt32(GridView1.DataKeys[1].Value);


Here the Value of may be Empty..ie check instead 1 to 0.

Id = Convert.ToInt32(GridView1.DataKeys[0].Value);
 
Share this answer
 
Its a ArgumentOutOfRange exception which reads your given range is not exist with current control.

means GridView1.DataKeys[1] is not exist. check with the index you provide.
 
Share this answer
 
Hi,

You need to learn about DataKeys[^]
 
Share this answer
 

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