Wednesday, 28 December 2011

Select,Insert,Update,Delete Using LINQ in ASP.NET

Default.aspx(Design):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Button ID="Button1" runat="server" Text="Show" onclick="Button1_Click" />&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button
            ID="BtnUpdate" runat="server" Text="Update" onclick="BtnUpdate_Click" />&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button
            ID="BtnDelete" runat="server" Text="Delete" onclick="BtnDelete_Click" />&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button
            ID="BtnInsert" runat="server" Text="Insert" onclick="BtnInsert_Click" />
        <asp:GridView ID="GridView1" runat="server" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
            ForeColor="Black" GridLines="Horizontal"  Width="443px">
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#242121" />
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataClassesDataContext DC = new DataClassesDataContext();
        var items = from product in DC.PRODUCTMASTs where product.CatagoryID == 1 select product;
        GridView1.DataSource = items;
        GridView1.DataBind();
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        PRODUCTMAST prod = dc.PRODUCTMASTs.Single(p => p.ProductID == 1);
        prod.ProductName = "Sanjay";
        prod.UnitPrice = 500.50M;
        dc.SubmitChanges();
    }
    protected void BtnInsert_Click(object sender, EventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();

        CATAGORYMAST ct = new CATAGORYMAST();
        ct.CatagoryID = 4;
        ct.CatagoryName = "OFFICE STATIONARY";
        ct.Description = "STATIONARY";

        dc.CATAGORYMASTs.InsertOnSubmit(ct);
        //PRODUCTMAST P1 = new PRODUCTMAST();
        //P1.ProductID = 12;
        //P1.ProductName = "PEN";
        //P1.CatagoryID = 4;
        //P1.UnitPrice = 10.00M;

        //PRODUCTMAST P2 = new PRODUCTMAST();
        //P1.ProductID = 13;
        //P1.ProductName = "OFFICE PAD";
        //P1.CatagoryID = 4;
        //P1.UnitPrice = 50.00M;

        //ct.PRODUCTMASTs.Add(P1);
        //ct.PRODUCTMASTs.Add(P2);

        //dc.CATAGORYMASTs.

        dc.SubmitChanges();
    }
    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        //PRODUCTMAST pp=new PRODUCTMAST();
        var del = from cc in dc.PRODUCTMASTs where cc.ProductID == 11 select cc;
        foreach (var dd in del)
        {
            dc.PRODUCTMASTs.DeleteOnSubmit(dd);
        }
        dc.SubmitChanges();
    }
}

No comments:

Post a Comment