Introduction

In this article we will see how to add data to database using linqtosql with insertallonsubmit method.

Step 1: Create asp.net web application

WebForm1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="InsertAllOnSubmit_LINQtoSQL.WebForm1" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:Button ID="Button1" runat="server" Text="Insert Data" OnClick="Button1_Click" />
  11. </div>
  12. </form>
  13. </body>
  14. </html>

WebForm1.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace InsertAllOnSubmit_LINQtoSQL
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. DataClasses1DataContext objContext = new DataClasses1DataContext();
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. List<Employee> emp = new List<Employee>(){
  18. new Employee{FirstName = "Andy", LastName="Jones"},
  19. new Employee{FirstName = "Steve", LastName="Robes"},
  20. new Employee{FirstName = "David", LastName="Orme"},
  21. };
  22. objContext.Employees.InsertAllOnSubmit(emp);
  23. objContext.SubmitChanges();
  24. }
  25. }
  26. }

Output of the application looks like this

Summary

In this blog we have seen how we can add data to database using linqtosql with insertallonsubmit. Happy coding!