Thursday, 29 December 2011

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

 
source code-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <link rel="Stylesheet" href="StyleSheet.css" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div style="width:940px">
  <li class="A">ID</li><li class="B">:</li><li class="C"><asp:TextBox ID="tb" runat="server"></asp:TextBox></li>
   <li class="A">NAME</li><li class="B">:</li><li class="C"><asp:TextBox ID="tb1" runat="server"></asp:TextBox></li>
     <li class="A">&nbsp;</li><li class="B">&nbsp;</li><li class="C">
        <asp:Button ID="btn" runat="server" Width="80" Text="Save"
            onclick="btn_Click" /></li>
   <li class="A">&nbsp;</li><li class="B">&nbsp;</li><li class="C">
        <asp:GridView ID="gv" DataKeyNames="id" runat="server" AllowPaging="True"
            onpageindexchanging="gv_PageIndexChanging" PageSize="2"
            onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting"
            onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating">
            <Columns>
                <asp:CommandField HeaderText="UPDATE" ShowEditButton="True" ShowHeader="True" />
                <asp:CommandField HeaderText="DELETE" ShowDeleteButton="True"
                    ShowHeader="True" />
            </Columns>
        </asp:GridView></li>
       </div>
    </center>
    </form>
</body>
</html>
CODE-:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    DataClassesDataContext o;
    protected void Page_Load(object sender, EventArgs e)
    {

        #region
   o = new DataClassesDataContext("" + Server.MapPath("~/App_Data/Database.mdf") + "");
        #endregion
   if (!IsPostBack)
       display();
      
    }

    void display()
    {
     

        var m = from y in o.EMPs
                select y;
        gv.DataSource = m;
        gv.DataBind();
     
    }

    protected void btn_Click(object sender, EventArgs e)
    {
      EMP p=new EMP();
        p.ID=Convert.ToInt32(tb.Text);
        p.NAME=tb1.Text;
        o.EMPs.InsertOnSubmit(p);
        o.SubmitChanges();
        Response.Write("One record saved.");
        display();
       
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        display();
    }
    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int i=Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
       // EMP x = o.EMPs.Single(a => a.ID == i);
       //x.NAME = "" + ((TextBox)gv.Rows[e.RowIndex].Cells[3].Controls[0]).Text + "";
       //o.SubmitChanges();
        var m = (from a in o.EMPs
                 where a.ID == i
                 select a).First();
        m.NAME = "" + ((TextBox)gv.Rows[e.RowIndex].Cells[3].Controls[0]).Text + "";
        o.SubmitChanges();
       gv.EditIndex = -1;
       Response.Write("One record updated.");
       display();
      
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int i = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
        var m = (from a in o.EMPs
                 where a.ID == i
                 select a).First();
        o.EMPs.DeleteOnSubmit(m);
        o.SubmitChanges();
        Response.Write("One record deleted.");
        display();
    }
    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        display();
    }
    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
        display();
    }
}

No comments:

Post a Comment