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

XML
Source Error:


Line 238:                        //if (GridView1.Columns[i] == GridView1.Columns[j])
Line 239:                        //{
Line 240:                        CheckBox t = (CheckBox)GridView1.Rows[j].Cells[2].FindControl("Select");
Line 241:                        CheckBox t1 = (CheckBox)GridView1.Rows[j].Cells[3].FindControl("Read");
Line 242:                        CheckBox t2 = (CheckBox)GridView1.Rows[j].Cells[4].FindControl("Write");


Source File: c:\Documents and Settings\ssm\Desktop\My Data\Bew1\WebSite3\RoleMapping.aspx.cs    Line: 240

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) +9368776
   System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index) +16
   RoleMapping.Select_CheckedChanged1(Object sender, EventArgs e) in c:\Documents and Settings\ssm\Desktop\My Data\Bew1\WebSite3\RoleMapping.aspx.cs:240
   System.Web.UI.WebControls.CheckBox.OnCheckedChanged(EventArgs e) +118
   System.Web.UI.WebControls.CheckBox.RaisePostDataChangedEvent() +142
   System.Web.UI.WebControls.CheckBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10
   System.Web.UI.Page.RaiseChangedEvents() +134
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5201


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272


My Code was:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Globalization;


public partial class RoleMapping : System.Web.UI.Page
{
    
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
        MenuMapping css = new MenuMapping();
        string sess;

    
    protected void Page_Load(object sender, EventArgs e)
    {
       sess = this.Session["userName"].ToString();
        if (!IsPostBack)
        {
            //dt.Clear();
           // String(Session["Choice"]);
            
            
        }

    }
    public void MainMenuddl()
    {

    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        DataSet ds = new DataSet();
        ds.Clear();
        // string s = DropdownList2.SelectedValue.Tostring(); 
        string ConnectionString = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        // create SqlCommand object
        int dd =Convert.ToInt16(DropDownList2.SelectedValue);
        string roledd = Convert.ToString(DropDownList1.SelectedItem.Text.ToString());

        string CommandText = "select a.submenuid,a.submenu,b.status from submenu as a left outer join menumapping as b on a.submenuid=b.submenuid WHERE a.ParentId = '" + dd + "'  group by a.SubMenuId, a.SubMenu,b.status ";
        
        SqlCommand myCommand = new SqlCommand(CommandText, myConnection);

       

        
        try
        {
            //string s = DropdownList2.SelectedValue.Tostring(); 
            myConnection.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            da = new SqlDataAdapter("select a.submenuid,a.submenu,b.status from submenu as a left outer join menumapping as b on a.submenuid=b.submenuid WHERE a.ParentId = '" + dd + "'  group by a.SubMenuId, a.SubMenu,b.status ", myConnection);

            
            da.Fill(ds);
            

            DataTable dt = ds.Tables[0].Copy();
               
            GridView1.Visible = true;
            GridView1.DataSource = ds;
            GridView1.DataBind();
                
                        
                    
        }
       catch
       {
           
           myConnection.Close();
       }
    
      
    }
    

   
    protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     

        int select = 0;
        //CheckBox chkBox = (CheckBox)Row.FindControl("checkBox");
         int pid = Convert.ToInt16(DropDownList2.SelectedItem.Value);
       // Guid g = new Guid(sess.ToString());
        DataSet dt = new DataSet();
       // dt.Clear();
        int la;
        int read;
        int write;
        int update;
        int delete;
        foreach (GridViewRow grd in GridView1.Rows)
        {
            Label SubMenuId = (Label)GridView1.Rows[grd.RowIndex].Cells[0].FindControl("lblSubMenuId");
            Label SubMenu = (Label)GridView1.Rows[grd.RowIndex].Cells[1].FindControl("lblSubMenu");
            string sa = SubMenuId.Text; sa = SubMenu.Text;

            CheckBox chk1 = (CheckBox)GridView1.Rows[grd.RowIndex].Cells[2].FindControl("Select");
            CheckBox chk2 = (CheckBox)GridView1.Rows[grd.RowIndex].Cells[3].FindControl("Read");
            CheckBox chk3 = (CheckBox)GridView1.Rows[grd.RowIndex].Cells[4].FindControl("Write");
            CheckBox chk4 = (CheckBox)GridView1.Rows[grd.RowIndex].Cells[5].FindControl("Update");
            CheckBox chk5 = (CheckBox)GridView1.Rows[grd.RowIndex].Cells[6].FindControl("Delete");

         
            if (chk1.Checked == true)
            {           
                  if(chk2.Checked==true)
                    {

                     read=1;
                    }
                 else
                    {
                     read=0;
                    }
               // int write;
                if(chk3.Checked==true)
                {
                    write=1;
                }
                else
                {
                    write=0;
                }
               // int update;
                if(chk4.Checked==true)
                {
                    update=1;
                }
                else
                {
                    update=0;
                }
               // int delete;
                if(chk5.Checked==true)
                {
                    delete=1;
                }
                else
                {
                    delete=0;
                }
                   select++;
                     la = 1;
                      string s =  css.insertMenuMapping(1,(DropDownList1.SelectedItem.Text.ToString()), Convert.ToInt16(DropDownList2.SelectedItem.Value.ToString()), Convert.ToInt16(SubMenuId.Text), SubMenu.Text.ToString(),sess.ToString(),la,"ADD",Convert.ToInt16(read),Convert.ToInt16(write),Convert.ToInt16(update),Convert.ToInt16(delete));
                                
            }
            else
            {
                if (chk2.Checked == true)
                {
                    read = 1;
                }
                else
                {
                    read = 0;
                }
                // int write;
                if (chk3.Checked == true)
                {
                    write = 1;
                }
                else
                {
                    write = 0;
                }
                // int update;
                if (chk4.Checked == true)
                {
                    update = 1;
                }
                else
                {
                    update = 0;
                }
                // int delete;
                if (chk5.Checked == true)
                {
                    delete = 1;
                }
                else
                {
                    delete = 0;
                }
                la = 0;
                string s = css.insertMenuMapping(1, (DropDownList1.SelectedItem.Text.ToString()), Convert.ToInt16(DropDownList2.SelectedItem.Value.ToString()), Convert.ToInt16(SubMenuId.Text), SubMenu.Text.ToString(), sess.ToString(), la, "Update", Convert.ToInt16(read), Convert.ToInt16(write), Convert.ToInt16(update), Convert.ToInt16(delete));
            }

        }
    
        dt.Clear();

    
}
  
   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    private void Select_CheckedChanged(object sender, EventArgs e)
    {
        
    }

    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void Select_CheckedChanged1(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            foreach (GridViewRow grd in GridView1.Rows)
            {
                //CheckBox chk1 = (CheckBox)GridView1.Rows[GridView1].Cells[2].FindControl("Select");
                for (int i = -1; i < GridView1.Rows.Count; i++)
                {

                    for (int j = -1; j < GridView1.Columns.Count; j++)
                    {

                        //if (GridView1.Columns[i] == GridView1.Columns[j])
                        //{
                        CheckBox t = (CheckBox)GridView1.Rows[j].Cells[2].FindControl("Select");
                        CheckBox t1 = (CheckBox)GridView1.Rows[j].Cells[3].FindControl("Read");
                        CheckBox t2 = (CheckBox)GridView1.Rows[j].Cells[4].FindControl("Write");
                        CheckBox t3 = (CheckBox)GridView1.Rows[j].Cells[5].FindControl("Update");
                        CheckBox t4 = (CheckBox)GridView1.Rows[j].Cells[6].FindControl("Delete");
                        //CheckBox t12 = (CheckBox)GridView1.Rows[j].Cells[2].FindControl("Read");
                        if (t.Checked == true)
                        {

                            // GridView1.Rows[i].Cells[j].Visible = true;
                            t1.Visible = true;
                            t2.Visible = true;
                            t3.Visible = true;
                            t4.Visible = true;
                        }

                        else
                        {
                            t1.Visible = false;
                            t2.Visible = false;
                            t3.Visible = false;
                            t4.Visible = false;
                        }
                    }
                }
            }
            }
        }
        //GridView1.Rows[0].Cells[3].Visible = true;
    }
Posted
Updated 24-Apr-13 2:18am
v2
Comments
v surya dev 24-Apr-13 8:11am    
pls anyne help me..?
[no name] 24-Apr-13 8:17am    
The error message is clear and you are even given the line the error occurs on so what sort of help do you need?
Sebastian T Xavier 24-Apr-13 8:19am    
Please don't dump all your code

1 solution

Hi,

As the exception message itself gives the detailed information, it is very easy to fix , if it is your code.

The exception is throwing from the Select_CheckedChanged1 event. How many rows / columns are there in that grid view? Please check for null. That will solve your issue.


Regards
Sebastian
 
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