indy28
asked on
Gridview throws index out of rang error on RowUpdating
Hi I have the following ASP.NET page
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" SelectCommandType="StoredP rocedure"
SelectCommand="Get_TopSear chTermsByL ang" UpdateCommandType="StoredP rocedure" UpdateCommand="Update_TopT ermPublish Status" runat="server" />
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" EnableViewState="true"
OnSelectedIndexChanged="Dr opDownList 1_Selected IndexChang ed" runat="server">
</asp:DropDownList>
<asp:GridView ID="GridView1" OnRowUpdating="GridView1_R owUpdating " runat="server" AutoGenerateColumns="False " >
<Columns>
<asp:BoundField DataField="ID" Visible="False" />
<asp:BoundField DataField="SearchTerm" HeaderText="Search Term" />
<asp:BoundField DataField="TotalCount" HeaderText="Item Count" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" Id="CheckBox1" Checked='<%#Bind("PublishS tatus") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
</div>
</form>
As you can there is a dropdownlist and a gridview, where the contents of the gridview are filtered on the selection of the dropdownlist. You are then able to modify the publishstatus checkbox and then click Update and the GridView should refresh databind with the changes but this breaks on the Update method. I get the following error:
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.ArgumentOutOfRangeE xception: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 68:
Line 69:
Line 70: GridViewRow selectedrow = (GridViewRow)GridView1.Row s[index];
Line 71: Label1.Text = GridView1.Rows.Count.ToStr ing();
Line 72:
Source File: c:\Program Files\Immediacy\CMS\6.0\Ad min\Addins \MoogDev\T agCloudAdm in.aspx.cs Line: 70
Stack Trace:
[ArgumentOutOfRangeExcepti on: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayLi st.get_Ite m(Int32 index) +2880797
System.Web.UI.WebControls. GridViewRo wCollectio n.get_Item (Int32 index) +10
Addins_MoogDev_TagCloudAdm in.GridVie w1_RowUpda ting(Objec t sender, GridViewUpdateEventArgs e) in c:\Program Files\Immediacy\CMS\6.0\Ad min\Addins \MoogDev\T agCloudAdm in.aspx.cs :70
System.Web.UI.WebControls. GridViewUp dateEventH andler.Inv oke(Object sender, GridViewUpdateEventArgs e) +0
System.Web.UI.WebControls. GridView.O nRowUpdati ng(GridVie wUpdateEve ntArgs e) +133
System.Web.UI.WebControls. GridView.H andleUpdat e(GridView Row row, Int32 rowIndex, Boolean causesValidation) +776
System.Web.UI.WebControls. GridView.H andleEvent (EventArgs e, Boolean causesValidation, String validationGroup) +837
System.Web.UI.WebControls. GridView.R aisePostBa ckEvent(St ring eventArgument) +199
System.Web.UI.WebControls. GridView.S ystem.Web. UI.IPostBa ckEventHan dler.Raise PostBackEv ent(String eventArgument) +7
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +177
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +1746
Please help I'm confused why this is happening. why is it Updating twice?
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" SelectCommandType="StoredP
SelectCommand="Get_TopSear
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" EnableViewState="true"
OnSelectedIndexChanged="Dr
</asp:DropDownList>
<asp:GridView ID="GridView1" OnRowUpdating="GridView1_R
<Columns>
<asp:BoundField DataField="ID" Visible="False" />
<asp:BoundField DataField="SearchTerm" HeaderText="Search Term" />
<asp:BoundField DataField="TotalCount" HeaderText="Item Count" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" Id="CheckBox1" Checked='<%#Bind("PublishS
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Update" Text="Update" />
</Columns>
</asp:GridView>
</div>
</form>
As you can there is a dropdownlist and a gridview, where the contents of the gridview are filtered on the selection of the dropdownlist. You are then able to modify the publishstatus checkbox and then click Update and the GridView should refresh databind with the changes but this breaks on the Update method. I get the following error:
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.ArgumentOutOfRangeE
Parameter name: index
Source Error:
Line 68:
Line 69:
Line 70: GridViewRow selectedrow = (GridViewRow)GridView1.Row
Line 71: Label1.Text = GridView1.Rows.Count.ToStr
Line 72:
Source File: c:\Program Files\Immediacy\CMS\6.0\Ad
Stack Trace:
[ArgumentOutOfRangeExcepti
Parameter name: index]
System.Collections.ArrayLi
System.Web.UI.WebControls.
Addins_MoogDev_TagCloudAdm
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
Please help I'm confused why this is happening. why is it Updating twice?
private SqlDataSource langDS;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
langDS = new SqlDataSource();
langDS.ConnectionString = new iSession().ConnectionString;
langDS.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
langDS.SelectCommand = "immsp_cms_MultiLanguage_GetLanguages";
DropDownList1.DataSource = langDS;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "StringValue";
DropDownList1.DataBind();
//string[] langvalarray;
foreach (ListItem item in DropDownList1.Items)
{
item.Value = item.Value.Split('!')[0];
}
SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ConnectionString = new iSession().ConnectionString;
SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
SqlDataSource1.SelectCommand = "Get_TopSearchTermsByLang";
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add("lang", "en");
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow selectedrow = (GridViewRow)GridView1.Rows[index];
SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ConnectionString = new iSession().ConnectionString;
SqlDataSource1.UpdateParameters.Clear();
SqlDataSource1.UpdateParameters.Add("termid", (string)selectedrow.Cells[0].ToString());
SqlDataSource1.UpdateParameters.Add("pubbool", (string)selectedrow.Cells[3].ToString());
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ConnectionString = new iSession().ConnectionString;
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add("lang", DropDownList1.SelectedValue);
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DropDownList1.SelectedInde
GridView1.RowUpdating += new GridViewUpdateEventHandler
from the page load function, as you have already mentioned the eventhandlers in .aspx page in <asp:DropDownList & <asp:GridView tags respectively
I hope this will work
Anurag