Solved

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Posted on 2004-03-31
7
3,249 Views
Last Modified: 2008-02-01
Tried very hard to solve the error above but I don't really know what does it mean, please help. My codes are as below :

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace RealTimePortal
{
      /// <summary>
      /// Summary description for Directory.
      /// </summary>
      public class Directory : System.Web.UI.Page
      {
            protected string u_name;
            protected System.Data.SqlClient.SqlConnection connection1;
            protected System.Data.SqlClient.SqlConnection connection2;
            protected System.Data.SqlClient.SqlConnection WebHostConn;
            protected System.Data.SqlClient.SqlCommand command1;
            protected System.Data.SqlClient.SqlDataAdapter SqlDataAdapter1;
            protected DataSet m_ds = new DataSet();
            protected System.Web.UI.WebControls.Button Button2;
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.Label Label2;
            protected System.Web.UI.WebControls.DataGrid DataGrid1;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  u_name = Request.QueryString["u_name"];
                  
                  //connection1 = new SqlConnection("server=localhost;database=PORTAL DONE;Trusted_Connection=yes");
                  System.Data.SqlClient.SqlConnection WebHostConn = new SqlConnection ("data source=66.102.133.244; initial catalog=sanath;integrated security=false;user id=koongaik;password=koongaik1051");
                  command1 = new SqlCommand("SP_udirSel",WebHostConn);
                  command1.CommandType = CommandType.StoredProcedure;

                  SqlParameter uname = new SqlParameter("@uname", SqlDbType.VarChar,50);
                  uname.Value = u_name;
                  command1.Parameters.Add(uname);

                  SqlParameter a1 = new SqlParameter("@a1", SqlDbType.VarChar,50);
                  a1.Value = "";
                  command1.Parameters.Add(a1);

                  SqlParameter a2 = new SqlParameter("@a2", SqlDbType.VarChar,50);
                  a2.Value = "";
                  command1.Parameters.Add(a2);

                  SqlParameter a3 = new SqlParameter("@a3", SqlDbType.VarChar,50);
                  a3.Value = "";
                  command1.Parameters.Add(a3);

                  SqlParameter a4 = new SqlParameter("@a4", SqlDbType.VarChar,50);
                  a4.Value = "";
                  command1.Parameters.Add(a4);

                  SqlParameter a5 = new SqlParameter("@a5", SqlDbType.VarChar,50);
                  a5.Value = "";
                  command1.Parameters.Add(a5);


                  SqlDataAdapter1 = new SqlDataAdapter();
                  SqlDataAdapter1.SelectCommand = command1;
                  SqlDataAdapter1.SelectCommand.CommandType = CommandType.StoredProcedure;
                  SqlDataAdapter1.Fill(m_ds,"Directory");
                  DataGrid1.DataSource = m_ds;
                  if (!IsPostBack)
                  {
                        DataGrid1.DataBind();
                  }
            }

            #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()
            {    
                  this.Button2.Click += new System.EventHandler(this.Button2_Click);
                  this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
                  this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                  string clickID = e.Item.Cells[5].Text;
                  if (e.CommandName == "Select")
                  {
                        Response.Redirect(clickID);
                  }
            }

            private void Button2_Click(object sender, System.EventArgs e)
            {
                  Response.Redirect("Home.aspx?u_name=" + u_name);

            }

            private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
            
            }
      }
}
0
Comment
Question by:alanvoon
7 Comments
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10730304
The only line where I can see a problem is the following one :
string clickID = e.Item.Cells[5].Text;

Check the count of cells to validate that 5 is a valid index :
if (e.Item.Cells.Count >= 6)
{
    string clickID = e.Item.Cells[5].Text; //Cells[5] is the 6th cell
}
0
 

Author Comment

by:alanvoon
ID: 10733293
I have tried the above code, but the same error still persists, would somebody please give me a detailed explanation of the error ?? Please help...
0
 
LVL 2

Accepted Solution

by:
eric_duncan earned 20 total points
ID: 10733430
The error is being thrown because somewhere your code is trying to access either an array or a collection with an index that is either less than zero (the first element in an array has an index of 0, so you can't go any lower) or greater than the index of the last item in the array. For example, if you have the following array:

int[] x = new int[5] {1,2,3,4,5};

then

x[0] is 1,
x[1] is 2,
x[2] is 3,
x[3] is 4,
and x[4] is 5.

If you were to try this:

x[-1]

or this:

x[5]

you would get the error you are describing above because your indexer is out of the bounds of the array.

Does this help? Try stepping through the code in the debugger to see the line that is causing the error, then check to see if there is a variable being used as an indexer that is outside of the bounds of the array.

Eric
0
 
LVL 2

Assisted Solution

by:sedmans
sedmans earned 20 total points
ID: 10741228
The other line that could possible cause this problem is

 u_name = Request.QueryString["u_name"];

You need to make sure that your url contains a u_name parameter.
0
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 20 total points
ID: 10753089
Be carefull publishing connection strings of servers using public IP addresses! I think you should change username and password.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# find out if selected(highlighted) text in textbox was replaced or removed 9 60
Get id from json Data with NewtonSoft 3 31
Connection String 16 41
Error in script 11 43
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now