Introduction
In this article we will see how to delete data from database using linqtosql with deleteallonsubmit method.
Step 1: Create ASP.NET Web Application
WebForm1.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DeleteAllOnSubmit_LINQtoSQL.WebForm1" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="Delete Data" OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
WebForm1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace DeleteAllOnSubmit_LINQtoSQL
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- DataClasses1DataContext objContext = new DataClasses1DataContext();
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- List<Employee> emp = new List<Employee>()
- { new Employee { EmpId = 1 },
- new Employee { EmpId = 2 },
- new Employee { EmpId = 3 }
- };
-
- objContext.Employees.DeleteAllOnSubmit(emp);
- objContext.SubmitChanges();
-
- }
- }
- }
Output of the application looks like this
Summary
In this blog we have seen how we can delete data from database using linqtosql with deleteallonsubmit. Happy coding!