Wednesday, March 14, 2012

Linq to Sql CRUD examples in Asp.Net c#



*************************************************************************

/*Add Stylesheet in your application names StyleSheet.css*/

 body {border:1px solid #000;padding:40px;}
.gridView{width:400px;background:#ffffff;border:1px solid #476db6; border-collapse:separate;}
.gridView th{border:0px;background:url('Images/hfbg.jpg') repeat-x;color:#ffffff;text-align:center;font-size:18px;}
.gridView td{padding-left:10px;border:1px solid #b5b5b5}

.altRow{background:url('Images/alt.png') repeat-x}

.gridPager{background:url('Images/hfbg.jpg') repeat-x;text-align:center;}
.gridPager table{margin:0px auto;}
.gridPager table td{width:25px;border:0px;}
.gridPager table span{cursor:pointer;font-weight:bold;color:#ffffff;}
.gridPager table a{cursor:pointer;text-decoration:none;color:#ffffff;}
.gridPager table a:hover{text-decoration:underline;}
.gridPager:hover{background:url('Images/hfbg.jpg') repeat-x;text-align:center;}

.heading{font:15px;font-weight:bold;width:300px;border-bottom:1px solid #000;}

a{color:#000;text-decoration:none}
a:hover{text-decoration:underline}

*************************************************************************

<%@ 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>Linq to Sql CRUD examples in Asp.Net c#</title>
    <link href="Resources/StyleSheet/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p id="result" runat="server"></p>
    <p class="heading">Create Department</p>
    <p>Department Name</p>
    <p><asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox></p>
    <p><asp:Button ID="butSaveDept" runat="server" Text="Save"
            onclick="butSaveDept_Click" /></p>
 
    <p class="heading">Read Department</p>
    <asp:GridView ID="GridDept" runat="server"
    AutoGenerateColumns="false"
    DataKeyNames="id"
    AutoGenerateEditButton="true"
    AutoGenerateDeleteButton="true"
    CssClass="gridView"
    AlternatingRowStyle-CssClass="altRow"
    onrowcancelingedit="GridDept_RowCancelingEdit"
    onrowdeleting="GridDept_RowDeleting"
    onrowediting="GridDept_RowEditing"
    onrowupdating="GridDept_RowUpdating">
    <Columns>
    <asp:BoundField DataField="departmentname" HeaderText="Department Name" />
    </Columns>
    </asp:GridView>
 
    </div>
    </form>
</body>
</html>

*************************************************************************

using System;
using System.Linq;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private DataClassesDataContext ctx = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDepartment();
        }
    }

    private void BindDepartment()
    {
        ctx = new DataClassesDataContext();
        GridDept.DataSource = ctx.GetTable<Department>().ToList();
        GridDept.DataBind();
    }

    protected void butSaveDept_Click(object sender, EventArgs e)
    {
        try
        {
            ctx = new DataClassesDataContext();
            Department department = new Department();
            department.DepartmentName = txtDepartment.Text;
            ctx.GetTable<Department>().InsertOnSubmit(department);
            ctx.SubmitChanges();
            result.InnerHtml = "Row inserted.";
        }
        catch (Exception ex)
        {
            result.InnerHtml = ex.Message;
        }
        finally {
            BindDepartment();
        }
    }
    protected void GridDept_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        GridDept.EditIndex = e.NewEditIndex;
        BindDepartment();
    }
    protected void GridDept_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        GridDept.EditIndex = -1;
        BindDepartment();
    }
    protected void GridDept_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        try
        {
            GridViewRow currentRow = GridDept.Rows[e.RowIndex];
            String dataKey = GridDept.DataKeys[currentRow.RowIndex].Values[0].ToString();

            ctx = new DataClassesDataContext();
            Department department = ctx.GetTable<Department>().SingleOrDefault(q => q.Id.Equals(dataKey));
            ctx.GetTable<Department>().DeleteOnSubmit(department);
            ctx.SubmitChanges();
            result.InnerHtml = "Row deleted.";
            GridDept.EditIndex = -1;
        }
        catch (Exception ex)
        {
            result.InnerHtml = ex.Message;
        }
        finally
        {
            BindDepartment();
        }
    }
    protected void GridDept_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        try
        {
            GridViewRow currentRow = GridDept.Rows[e.RowIndex];
            String dataKey = GridDept.DataKeys[currentRow.RowIndex].Values[0].ToString();
            String departmentName = ((TextBox)currentRow.Cells[1].Controls[0]).Text;

            ctx = new DataClassesDataContext();
            Department department = ctx.GetTable<Department>().SingleOrDefault(q => q.Id.Equals(dataKey));
            department.DepartmentName = departmentName;
            ctx.SubmitChanges();
            result.InnerHtml = "Row updated.";
            GridDept.EditIndex = -1;
        }
        catch (Exception ex)
        {
            result.InnerHtml = ex.Message;
        }
        finally
        {
            BindDepartment();
        }
    }
}

*************************************************************************

Go
Create database softwarekaffee

Go
use softwarekaffee

Go
create table Department
(
Id int identity primary key,
DepartmentName nvarchar(30)
)
Insert Into Department Values ('Sales');
Insert Into Department Values ('Production');
Insert Into Department Values ('Purchase');
Insert Into Department Values ('Stock');

*************************************************************************
*************************************************************************


Post a Comment