*************************************************************************
/*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>DataContext.ExecuteCommand Method and DataContext.ExecuteQuery Method in Linq</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.ExecuteQuery<Department>("Select *from department");
GridDept.DataBind();
}
protected void butSaveDept_Click(object sender, EventArgs e)
{
try
{
ctx = new DataClassesDataContext();
ctx.ExecuteCommand("Insert into department values({0})", txtDepartment.Text);
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();
ctx.ExecuteCommand("Delete from department where id={0}", dataKey);
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();
ctx.ExecuteCommand("Update department set departmentname={0} where id={1}", departmentName,dataKey);
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');
*************************************************************************
*************************************************************************
No comments:
Post a Comment