Can we write Insert or Update statement inside Row_Deleting event?

 Posted by vishalneeraj-24503 on 11/25/2013 | Category: ASP.NET Interview questions | Views: 2073 | Points: 40
Answer:

YES, we can write any codes inside Row_Deleting event.
We have to give CommnadName = "Delete" in ItemTemplate, then in Gridview RowDeleting event we can write any code or perform any operations like below:-

Protected Sub grid_employee_details_RowDeleting(sender As Object,e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting


//write insert statement/update statement as
Dim sql_query as String = "update employee_master set employee_name = 'vishal' where employee_id = 1 and status = 'AA'"
Dim con as New SqlConnection("ConnectionString")
Dim cmd as New SQLCommand(sql_query,con)
Dim row_affected as Integer = cmd.ExecuteNonQuery();

If(row_affected>0) Then
lbl_message.Visible = True;
lbl_message.Text = "Record Successfull Updated"
End If

End Sub


Aspx page code is as follows:-

<asp:Gridview id="grid_employee_details" runat="server">

<Command>
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btn_delete" runat="Server" CommandName="Delete" Text="Delete"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Command>
</asp:Gridview>
<asp:Label id="lbl_message" runat="server" visible="False"></asp:Label>


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response