I was coding for my project which I am working on currently and had the need to
using gridview for the same.
I am using OnRowCommand event, the deletion works fine,entry is being deleted from database too, but for some reason, this error is being thrown:
"The GridView 'gvCourses' fired event RowDeleting which wasn't handled."
Note:- I am not using 'OnRowDeleting' in the aspx file.
Here is the odd thing which I, as a beginner do not understand. The error does not show up anymore when I generate the 'OnRowDeleting' event and leave the event blank in code behind behind(0 lines of code inside the event handler).
Looking to learn and understand why it is happening. Any help would be greatly appreciated.
.aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Panel ID="Panel2" runat="server">
<table style="width:100%;">
<tr>
<td style="width:210px">
</td>
<td>
</td>
</tr>
<tr>
<td style="width:210px"> </td>
<td> </td>
</tr>
<tr>
<td style="width:210px; height: 331px;"></td>
<td style="height: 331px">
<asp:GridView ID="gvCourses" runat="server"
AutoGenerateColumns="False"
CssClass="table-hover table"
GridLines="None" Width="800px"
ShowFooter="True"
OnRowCommand="gvCourses_RowCommand" >
<Columns>
<asp:BoundField DataField="course"
HeaderText="Courses in Valsura"
SortExpression="DateField" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Students</asp:ListItem>
<asp:ListItem>Teachers</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click"
Text="Add Course"
CssClass="btn-danger" />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
runat="server">View Batches</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3"
runat="server"
CommandName="delete"
CommandArgument='<%#Eval ("course") %>'>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RequiredFieldValidator ID="rfvCourse"
ControlToValidate="DropDownList1"
InitialValue="Select"
ErrorMessage="Select*"
ForeColor="Red" >
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 180px; height: 22px;"></td>
<td style="height: 22px"></td>
<td style="height: 22px"></td>
</tr>
<tr>
<td style="width: 180px"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</asp:Panel>
</asp:Content>
.aspx.cs code: (code behind,including only required)
protected void gvCourses_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataTable dt = new DataTable();
string query =
"delete from tblCourses where course='"+e.CommandArgument.ToString()+"'";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
gvCourses.DataSource = dt;
gvCourses.DataBind();
ViewState["query"] = "select course from tblCourses";
bindgrid();
}
protected void bindgrid()
{
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(ViewState["query"].ToString(), con))
{
sda.Fill(dt);
}
gvCourses.DataSource = dt;
gvCourses.DataBind();
}
Page looks like this(batches haven't been coded yet).
There seems to be some built-in gridview code that is created and fires when a command button is called Delete
. According to this, this and this you can just change the name from Delete
to anything else. Otherwise, add the empty event handler.
Btw, DataSet
s have CRUD functionality built-in and they can be easier to work with, but some people don't like the overhead.