I am getting error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index " on updating the gridview. I have checked the rowindex in update function and its good. But i checked the gridview row count as grdMain.Rows.Count it is showing 0. My update event is below
BO.FranchiseInquiry f = new BO.FranchiseInquiry();
int index = grdMain.EditIndex;
lbl.Text = grdMain.Rows.Count.ToString();
GridViewRow row = (GridViewRow)grdMain.Rows[index];
TextBox txtNote = (TextBox)row.FindControl("txtNote");
f.FranchiseInquiryID = Int32.Parse(grdMain.DataKeys[index].Value.ToString());
f.Note = txtNote.Text.Trim();
fd.UpdateFranchiseInquiry(f);
grdMain.EditIndex = -1;
BindData();
But this one too not working for me. i.e both the lines give me proper row index, but the grid becomes empty, that is what i told in my first post saying row count is showing 0 in next line. So it throws error index out of range while getting the grid row.
I have no idea how the grid becomes empty on row updating event.
I am not clear what is the specific actions in the class "BO".
But if I do this simple
test for your code and it can properly obtain the desired value:
The BO is just BLL class, which calls the SP to update the row in DB. To be exact the error is in line
GridViewRow row = (GridViewRow)grdMain.Rows[index];
I have the BindData function written same as you mentioned above. In fact I have done this in some other project exactly same way. But this time, I am getting the error. And purposly added the line
lbl.Text = grdMain.Rows.Count.ToString();
to check the row count of the grid before picking the row. And i found it is 0, which was grater on Load.
naveendkt
Member
12 Points
27 Posts
Error on Updating GridView row
Nov 14, 2012 09:18 PM|LINK
I am getting error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index " on updating the gridview. I have checked the rowindex in update function and its good. But i checked the gridview row count as grdMain.Rows.Count it is showing 0. My update event is below
BO.FranchiseInquiry f = new BO.FranchiseInquiry(); int index = grdMain.EditIndex; lbl.Text = grdMain.Rows.Count.ToString(); GridViewRow row = (GridViewRow)grdMain.Rows[index]; TextBox txtNote = (TextBox)row.FindControl("txtNote"); f.FranchiseInquiryID = Int32.Parse(grdMain.DataKeys[index].Value.ToString()); f.Note = txtNote.Text.Trim(); fd.UpdateFranchiseInquiry(f); grdMain.EditIndex = -1; BindData();<asp:GridView ID="grdMain" runat="server" EmptyDataText="No Records Found" AutoGenerateColumns="False" DataKeyNames="FranchiseInquiryID" OnRowEditing="grdMain_RowEditing" OnRowUpdating="grdMain_RowUpdating" OnRowCancelingEdit="grdMain_RowCancelingEdit"> <Columns> <asp:BoundField DataField="FranchiseInquiryID" Visible="false" HeaderText="FranchiseInquiryID" /> <asp:BoundField HeaderText="First Name" DataField="FirstName" ReadOnly="true" /> <asp:BoundField HeaderText="Last Name" DataField="LastName" ReadOnly="true" /> <asp:BoundField HeaderText="Phone" DataField="Phone" ReadOnly="true" /> <asp:BoundField HeaderText="Email" DataField="Email" ReadOnly="true" /> <asp:BoundField HeaderText="Zip Code" DataField="Zip" ReadOnly="true" /> <asp:BoundField HeaderText="Liquid Capital Available" DataField="LiquidCapital" ReadOnly="true" /> <asp:TemplateField HeaderText="Notes"> <ItemTemplate> <%# Eval("Note")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="txtNote" Text='<%# Eval("Note")%>' /> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="true" ShowDeleteButton="false" ButtonType="Button" CausesValidation="false"/> </Columns> </asp:GridView>I am not getting why the grid count becomes 0 in line lbl.Text = grdMain.Rows.Count.ToString();
Please help me.
oned_gk
All-Star
30979 Points
6338 Posts
Re: Error on Updating GridView row
Nov 14, 2012 09:59 PM|LINK
naveendkt
Member
12 Points
27 Posts
Re: Error on Updating GridView row
Nov 14, 2012 10:47 PM|LINK
I am not looking for rows count here. I am having error which i mentioned earlier in line
.
oned_gk
All-Star
30979 Points
6338 Posts
Re: Error on Updating GridView row
Nov 15, 2012 12:19 AM|LINK
try
naveendkt
Member
12 Points
27 Posts
Re: Error on Updating GridView row
Nov 15, 2012 12:57 PM|LINK
Hi,
I tried with the
But this one too not working for me. i.e both the lines give me proper row index, but the grid becomes empty, that is what i told in my first post saying row count is showing 0 in next line. So it throws error index out of range while getting the grid row. I have no idea how the grid becomes empty on row updating event.
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: Error on Updating GridView row
Nov 20, 2012 05:32 AM|LINK
Hi,
I am not clear what is the specific actions in the class "BO". But if I do this simple test for your code and it can properly obtain the desired value:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } protected void grdMain_RowUpdating(object sender, GridViewUpdateEventArgs e) { //BO.FranchiseInquiry f = new BO.FranchiseInquiry(); int index = grdMain.EditIndex; lbl.Text = grdMain.Rows.Count.ToString(); GridViewRow row = (GridViewRow)grdMain.Rows[index]; TextBox txtNote = (TextBox)row.FindControl("txtNote"); //f.FranchiseInquiryID = Int32.Parse(grdMain.DataKeys[index].Value.ToString()); int texBal = Int32.Parse(grdMain.DataKeys[index].Value.ToString()); //f.Note = txtNote.Text.Trim(); Label1.Text = "Datakeys:" + texBal.ToString() + ",txtNote:" + txtNote.Text; // fd.UpdateFranchiseInquiry(f); grdMain.EditIndex = -1; BindData(); } protected void grdMain_RowEditing(object sender, GridViewEditEventArgs e) { grdMain.EditIndex = e.NewEditIndex; BindData(); }Feedback to us
Develop and promote your apps in Windows Store
naveendkt
Member
12 Points
27 Posts
Re: Error on Updating GridView row
Nov 21, 2012 05:59 PM|LINK
Hi Frank,
The BO is just BLL class, which calls the SP to update the row in DB. To be exact the error is in line
GridViewRow row = (GridViewRow)grdMain.Rows[index];
I have the BindData function written same as you mentioned above. In fact I have done this in some other project exactly same way. But this time, I am getting the error. And purposly added the line
lbl.Text = grdMain.Rows.Count.ToString();
to check the row count of the grid before picking the row. And i found it is 0, which was grater on Load.