Basic DB commands With LINQ to SQL
Markup File:
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblLocation" runat="server" Text="Location:"></asp:Label>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox><br />
<asp:Button ID="btnShow" runat="server" Text="Show Records"
onclick="btnShow_Click" />
<asp:Button ID="btnInsert" runat="server" Text="Add Record"
onclick="btnInsert_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="UpdateRecord"
onclick="btnUpdate_Click" />
<asp:Button ID="btnDelete" runat="server" Text="Delete Record"
onclick="btnDelete_Click" /><br />
<asp:GridView ID="gdShow" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="gdShow_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="DeptID" HeaderText="Department ID"
SortExpression="DeptID" />
<asp:BoundField DataField="DName" HeaderText="Dept Name"
SortExpression="DName" />
<asp:BoundField DataField="DLocation" HeaderText="Location"
SortExpression="DLocation" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
CS code File:
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
{
LinqToSqlDataContext db = new LinqToSqlDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnShow_Click(object sender, EventArgs e)
{
FillGrid();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
try
{
tblDepartment dept = new tblDepartment { DName = txtName.Text, DLocation = txtAddress.Text };
db.tblDepartments.InsertOnSubmit(dept);
db.SubmitChanges();
txtName.Text = "";
txtAddress.Text = "";
FillGrid();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
tblDepartment dept = db.tblDepartments.Single(p => p.DName.Equals(txtName.Text) );
dept.DLocation = txtAddress.Text;
db.SubmitChanges();
txtName.Text = "";
txtAddress.Text = "";
FillGrid();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
var dept=from p in db.tblDepartments where p.DName ==txtName .Text select p ;
// tblDepartment dept = db.tblDepartments.First(p => p.DName.StartsWith(txtName.Text) && p.DLocation.StartsWith(txtAddress.Text));
// tblDepartment dept = db.tblDepartments.Single(p => p.DName.Equals(gdShow.SelectedRow.Cells[1].Text) && p.DLocation.Equals(gdShow.SelectedRow.Cells[2].Text));
//tblDepartment dept = gdShow.SelectedRow.Cells [0];
// db.tblDepartments .DeleteOnSubmit (dept );
// db.SubmitChanges ();
foreach (tblDepartment dept2 in dept )
{
db.tblDepartments.DeleteOnSubmit(dept2);
}
//db.tblDepartments.DeleteOnSubmit(dept);
db.SubmitChanges();
FillGrid();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
public void FillGrid()
{
try
{
var Department = db.tblDepartments;
gdShow.DataSource = Department;
gdShow.DataBind();
}
catch (Exception ex)
{
}
}
protected void gdShow_SelectedIndexChanged(object sender, EventArgs e)
{
txtName.Text = gdShow.SelectedRow.Cells[1].Text .ToString ();
txtAddress.Text = gdShow.SelectedRow.Cells[2].Text .ToString();
}
}
Markup File:
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblLocation" runat="server" Text="Location:"></asp:Label>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox><br />
<asp:Button ID="btnShow" runat="server" Text="Show Records"
onclick="btnShow_Click" />
<asp:Button ID="btnInsert" runat="server" Text="Add Record"
onclick="btnInsert_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="UpdateRecord"
onclick="btnUpdate_Click" />
<asp:Button ID="btnDelete" runat="server" Text="Delete Record"
onclick="btnDelete_Click" /><br />
<asp:GridView ID="gdShow" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="gdShow_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="DeptID" HeaderText="Department ID"
SortExpression="DeptID" />
<asp:BoundField DataField="DName" HeaderText="Dept Name"
SortExpression="DName" />
<asp:BoundField DataField="DLocation" HeaderText="Location"
SortExpression="DLocation" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
CS code File:
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
{
LinqToSqlDataContext db = new LinqToSqlDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnShow_Click(object sender, EventArgs e)
{
FillGrid();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
try
{
tblDepartment dept = new tblDepartment { DName = txtName.Text, DLocation = txtAddress.Text };
db.tblDepartments.InsertOnSubmit(dept);
db.SubmitChanges();
txtName.Text = "";
txtAddress.Text = "";
FillGrid();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
tblDepartment dept = db.tblDepartments.Single(p => p.DName.Equals(txtName.Text) );
dept.DLocation = txtAddress.Text;
db.SubmitChanges();
txtName.Text = "";
txtAddress.Text = "";
FillGrid();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
var dept=from p in db.tblDepartments where p.DName ==txtName .Text select p ;
// tblDepartment dept = db.tblDepartments.First(p => p.DName.StartsWith(txtName.Text) && p.DLocation.StartsWith(txtAddress.Text));
// tblDepartment dept = db.tblDepartments.Single(p => p.DName.Equals(gdShow.SelectedRow.Cells[1].Text) && p.DLocation.Equals(gdShow.SelectedRow.Cells[2].Text));
//tblDepartment dept = gdShow.SelectedRow.Cells [0];
// db.tblDepartments .DeleteOnSubmit (dept );
// db.SubmitChanges ();
foreach (tblDepartment dept2 in dept )
{
db.tblDepartments.DeleteOnSubmit(dept2);
}
//db.tblDepartments.DeleteOnSubmit(dept);
db.SubmitChanges();
FillGrid();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
public void FillGrid()
{
try
{
var Department = db.tblDepartments;
gdShow.DataSource = Department;
gdShow.DataBind();
}
catch (Exception ex)
{
}
}
protected void gdShow_SelectedIndexChanged(object sender, EventArgs e)
{
txtName.Text = gdShow.SelectedRow.Cells[1].Text .ToString ();
txtAddress.Text = gdShow.SelectedRow.Cells[2].Text .ToString();
}
}
No comments:
Post a Comment