Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView fired event RowDeleting which wasn't handled 1

Status
Not open for further replies.

xcaliber2222

Programmer
Apr 10, 2008
70
US
Hello,

This is for an ASP.NET 2.0 application:

Here is my delete button link, which verifies the operation with the user:

Code:
 <asp:TemplateField> 
 <ItemTemplate> 
 <asp:LinkButton ID="btnDelete" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("Referral Id") %>' OnClientClick='return confirm("Are you sure you want to delete this referral?");' runat="Server">
</asp:LinkButton> 
</ItemTemplate> 
</asp:TemplateField>

Here is what I'm doing in the code-behind:

Code:
protected void btnDelete_Click(object sender, EventArgs e)
{
//Deleting code goes here
string sqlString = "DELETE FROM [Referrals] WHERE [ReferralId] =" + referralId.ToString();
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetSqlStringCommand(sqlString);
db.ExecuteNonQuery(dbCommand);
lblmsg.Text = rowAffected.ToString() + " Row(s) Deleted Successfuly";
}

Shouldn't I be able to simply run a DELETE operation against the database like I am here with an UPDATE, which works fine:

Code:
private void SaveStatus(int referralId, string status)
{
//Saving code goes here
string sqlString = "UPDATE [Referrals] SET [Status] ='" + status + "' WHERE [ReferralId] =" + referralId.ToString();
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetSqlStringCommand(sqlString);
db.ExecuteNonQuery(dbCommand);
}

This is the error I'm getting:
Code:
The GridView 'gvList' fired event RowDeleting which wasn't handled. 
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.Web.HttpException: The GridView 'gvList' fired event RowDeleting which wasn't handled.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[HttpException (0x80004005): The GridView 'gvList' fired event RowDeleting which wasn't handled.]
   System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +1494719
   System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +604
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1134
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
Any help as to what I'm missing or doing wrong would be greatly appreciated.

Thank you,
Alejandro
 
I'm assuming the delete button is in a datagrid. Since the name is 'delete', that's a reserved name and it will trigger the deleting event. Either change the name or add a dummy RowDeleting handler.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Thanks Artie! That worked! Now I have just one more little problem. To make sure the grid was refreshing after the delete and showing the latest records I added my bindgrid() to my RowCommand event as follows:

Code:
protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int ReferralId = Convert.ToInt32(e.CommandArgument);
// Delete the record 
DeleteRecordByID(ReferralId);
bindgrid();
}
}

This works great, EXCEPT, if I delete the LAST record in the grid I get this error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

One thing to note is that I'm doing a sort DESC on the grid so the record that appears as the last record on my grid is actually the first one that appears in the database table, if this makes any difference.

Any ideas or suggestions?

Thank you,
Alejandro
 
Show me the delete code. I suspect you are trying to delete the rows.count entry. The grid is numbered from 0 to rows.count - 1.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Hi Artie,

OK, here it is:

On my list.aspx file:

Code:
<asp:GridView DataKeyNames="Referral ID" ID="gvList" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="gvList_RowCommand" OnRowDataBound="gvList_RowDataBound" OnRowDeleted="gvList_RowDeleted" OnRowDeleting="gvList_RowDeleting" AllowPaging="True" PageSize="20" AutoGenerateColumns="False" OnPageIndexChanging="gvList_PageIndexChanging">

Delete link button:
Code:
<asp:TemplateField HeaderText="Delete"> 
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandArgument='<%# Eval("Referral Id") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate> 
</asp:TemplateField>

And here is my code behind. Notice I do a bindgrid() on the RowCommand event so the grid is refreshed after the delete operation:

Code:
protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int ReferralId = Convert.ToInt32(e.CommandArgument);
                // Delete the record 
                DeleteRecordByID(ReferralId);
                bindgrid();
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string status = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
                DropDownList ddl = e.Row.FindControl("ddlStatus") as DropDownList;
                ddl.SelectedValue = status;

                LinkButton l = (LinkButton)e.Row.FindControl("btnDelete");
                l.Attributes.Add("onclick", "javascript:return " +
                     "confirm('Are you sure you want to delete referral #" +
                     DataBinder.Eval(e.Row.DataItem, "Referral ID") + "?')");
            }
        }

        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ReferralId = (int) gvList.DataKeys[e.RowIndex].Value;
            DeleteRecordByID(ReferralId);
        }

        private void DeleteRecordByID(int referralId)
        {
            string sqlString = "DELETE FROM [Referrals] WHERE [ReferralId] =" + referralId.ToString();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sqlString);
            db.ExecuteNonQuery(dbCommand);
        }

Thanks for looking it over.

Alejandro
 
Hi Artie,

I meant to also say the error occurrs on this line in the RowDeleting event:

Code:
int ReferralId = (int) gvList.DataKeys[e.RowIndex].Value;

Thanks,
Alejandro
 
Hello,

I found the solution. To make the page refresh I had a bindgrid() on the RowCommand event. Instead, I should have put the bindgrid() on the RowDeleting event. Works fine now. I hae to admit, I'm not sure why it didn't work on the RowCommand event.

Thanks,
Alejandro
 
Glad you found the solution. Not sure why it doesn't work in the RowCommand event either. BTW, it looks like you are trying to delete the record twice, once in the RowCommand and once in the RowDeleting.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top