Page_Load Error: Index was out of range

4Learning
4Learning used Ask the Experts™
on
Hello,
I am getting the following error while trying to load page..... this is the very first page of my application....

(exception1) My App Login Page_Load Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun KlineLead Software Engineer

Commented:
Without more details, the exact cause is hard to determine. However, the general meaning of this exception is that you were trying to access the members of a collection using the index and the index number you used exceeded the bounds of the collection. In a for loop, this generally happens when you do not take into account that indexes are zero-based, and you did not subtract 1 from the length property of the collection.

Author

Commented:
Following is the code in the .aspx file--

<html>
      <head>
            <title>Application Login</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1" />
            <meta name="CODE_LANGUAGE" content="C#"/>
content="http://schemas.microsoft.com/intellisense/ie5"/>
          <style type="text/css">
            #Form1
            {
                height: 60px;
            }
        </style>
      </head>
      <body bgcolor="#ffff66">
            <form id="Form1" method="post" runat="server">
                <input type="hidden" id="txth_error" runat="server" />
                  <table id="Table1" style="Z-INDEX: 101; left: 184px; width: 504px; position: absolute; top: 96px; height: 120px"
                              cellspacing="1" cellpadding="1" width="504" border="1">
                              <tr>
                                    <td valign="top" align="center" colspan="3">&nbsp;&nbsp;&nbsp;&nbsp;
                                        <font color="#000066" size="5">
                                            <b><u>User Access App</u></b>
                                        </font>
                                    </td>
                              </tr>
                              <tr bgcolor="#ff3300">
                                    <td>Username:</td>
                                    <td><input type="text" id="txt_username" name="txt_username" tabindex="1"/></td>
                                    <td width="5%"><input id="btn_login" style="width: 48px; height: 24px" type="submit" value="Login" tabindex="3"/></td>
                              </tr>
                              <tr bgcolor="#ff3300">
                                    <td>Password:</td>
                                    <td colspan="2"><input id="txt_password" type="password" name="txt_password" tabindex="2"/></td>
                              </tr>
                              <tr bgcolor="#ff3300"><td colspan="3" ><br/><font color="red">Enter username and password.</td></tr>
                        </table>
                      <script type="text/javascript" language="javascript">
                          if(String(document.Form1.txth_error.value).length > 0){
                              alert(document.Form1.txth_error.value);
                          }
                      </script>
            </form>
      </body>
</html>



Following is the code in the aspx.cs file--

namespace My_app
{
      /// <summary>
      /// Login page
      /// </summary>
      public partial class Applogin : System.Web.UI.Page
      {
            public void Page_Load(object sender, EventArgs e)
            {
                  try
                  {
                        //If this is a postback
                            if (Page.IsPostBack)
                                  {
                              ...some code
                        }
                        
                  }//end try
                  catch(Exception exception1)
                  {
                        Response.Write("(exception1) My App Login Page_Load Error:  "+exception1.Message);
                  }
            }//end Page_Load

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    

            }
            #endregion
      }//end class
}//end namespace
Shaun KlineLead Software Engineer

Commented:
Try outputting the stack trace in your Page_Load exception handler.
This will help point you to the specific section/line that is causing the problem.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
Could someone point me to get the stack trace output. thanks.

Author

Commented:
Can someone point to why the error is being caused... the code listed above is the very first page that does not contain any data or gridview.... I am doing a simple postback within the try block and that is the error being thrown.

Thanks for any help...

Author

Commented:
Hi all,

Following is the stack trace that I have got..... dont see where the error might be....

[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) +7487768
   System.Collections.Specialized.NameObjectCollectionBase.BaseGet(Int32 index) +13
   System.Collections.Specialized.NameValueCollection.Get(Int32 index) +8
   MyNameSpace.MyFirstPage.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\dlm19\My Documents\Visual Studio 2005\Projects\MyApp\MyFirstPage.aspx.cs:24
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Shaun KlineLead Software Engineer

Commented:
In your code, what is line 24? The following line is pointing to that line number as the culprit:

MyNameSpace.MyFirstPage.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\dlm19\My Documents\Visual Studio 2005\Projects\MyApp\MyFirstPage.aspx.cs:24

Are you trying to access any Request, Response, Session or Form objects on that line?

Author

Commented:
Shaun, I am trying to figure out the problem... what I am noticing is that even if I comment out all the code in the Page_load function above, in that case there will be no line#, it still returns the same error....

this is indicating that somewhere the folder is being hardcoded and not pointing to the correct folder on my c drive.... and I checked my web config file, the csproj, csproj user file and the csproj webinfo file and I do not see anywhere a reference to the C:\Documents and Settings\dlm19\My Documents\Visual Studio 2005\Projects\MyApp\MyFirstPage.aspx.cs

The correct files (all my project files)  are in C:\CSProj\ folder.... but I do not know from where the other path is coming into the picture..... maybe that could be the error ...... not sure.....
Shaun KlineLead Software Engineer

Commented:
As a possibility, check the <@ Page reference in your MyFirstPage.aspx. There is an attribute for CodeFile. This points to the location of your .CS file. If you had originally created the .aspx file in your My Documents directory and then moved the file, it is possible this attribute is still pointing to an old reference.
the page to be loaded needed a param such as MyFirstPage.aspx?ID=1. since the ID=1 was not supplied when run from within the IDE by clicking "View in browser", this error was being generated. Neither was the start URL set to correctly pass the value.

Hence the error.

Thankyou all the experts for the time.

Author

Commented:
Found out the error

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial