CRUD With LINQDataSource

Introduction

This article demonstrates an interesting and very useful concept in LINQ.

Question: What is CRUD with LINQDataSource?

In simple terms "It provides the ability to perform a CRUD operation with a data source based on a data context object".

Step 1: Create a new web application

Output1.jpg

Step 2: Adding a new LINQ-to-SQL

Output2.png

Output3.jpg

Output4.png

Step 3: Add a new data source to the GridView

Output5.png


Output6.png

Output7.png


Output8.png


Output9.png


Output10.png



Step 4: The complete code of WebForm1.aspx is as in the following:
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CRUD_LINQDataSource.WebForm1" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

                AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"

                BorderWidth="1px" CellPadding="2" DataKeyNames="EmpId" DataSourceID="LinqDataSource1"

                ForeColor="Black" GridLines="None">

                <AlternatingRowStyle BackColor="PaleGoldenrod" />

                <Columns>

                    <asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" ShowEditButton="True" />

                    <asp:BoundField DataField="EmpId" HeaderText="EmpId" InsertVisible="False" ReadOnly="True"

                        SortExpression="EmpId" />

                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />

                    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />

                    <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />

                </Columns>

                <FooterStyle BackColor="Tan" />

                <HeaderStyle BackColor="Tan" Font-Bold="True" />

                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

                <SortedAscendingCellStyle BackColor="#FAFAE7" />

                <SortedAscendingHeaderStyle BackColor="#DAC09E" />

                <SortedDescendingCellStyle BackColor="#E1DB9C" />

                <SortedDescendingHeaderStyle BackColor="#C2A47B" />

            </asp:GridView>

            <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="CRUD_LINQDataSource.DataClasses1DataContext"

                EntityTypeName="" EnableDelete="true" EnableUpdate="true" TableName="Employees"

                EnableInsert="True">

            </asp:LinqDataSource>

            <br />

        </div>

    </center>

    </form>

</body>

</html>

Step 5: The complete code of WebForm1.aspx.cs is as in the following:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace CRUD_LINQDataSource

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

    }

}

Step 6: The output of the application is as in the following:

Output11.png

I hope this article was useful for you.

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.