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
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:
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)
{
}
}
public void MainMenuddl()
{
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.Clear();
string ConnectionString = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
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
{
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;
int pid = Convert.ToInt16(DropDownList2.SelectedItem.Value);
DataSet dt = new DataSet();
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;
}
if(chk3.Checked==true)
{
write=1;
}
else
{
write=0;
}
if(chk4.Checked==true)
{
update=1;
}
else
{
update=0;
}
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;
}
if (chk3.Checked == true)
{
write = 1;
}
else
{
write = 0;
}
if (chk4.Checked == true)
{
update = 1;
}
else
{
update = 0;
}
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)
{
for (int i = -1; i < GridView1.Rows.Count; i++)
{
for (int j = -1; j < GridView1.Columns.Count; 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");
if (t.Checked == 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;
}
}
}
}
}
}
}