0% found this document useful (0 votes)
29 views

LINQ in ASP#

This document provides code examples for performing CRUD operations in ASP.NET using LINQ to SQL. It includes presentation layer code with buttons and textboxes for insert, update and delete. It also includes code behind classes with methods for these operations using a LINQ data context.

Uploaded by

asnakechwube28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

LINQ in ASP#

This document provides code examples for performing CRUD operations in ASP.NET using LINQ to SQL. It includes presentation layer code with buttons and textboxes for insert, update and delete. It also includes code behind classes with methods for these operations using a LINQ data context.

Uploaded by

asnakechwube28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ASP.

NET LINQ to SQL Using C# Lab Manual

ASP.NET LINQ Lab Manual Using C#


Presentation Layer
<table style=" font-family:Times New Roman; font-size:medium; font-weight:bolder">
<tr><td>ID</td><td>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox></td></tr>
<tr><td>Name</td><td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td></tr>
<tr><td>Sex</td><td>
<asp:TextBox ID="txtSex" runat="server"></asp:TextBox></td></tr>
<tr><td>Age</td><td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td></tr>
<tr><td>Address</td><td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td></tr>
<tr><td></td><td>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Update"
onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Delete"
onclick="Button3_Click" />
<asp:Label ID="lblMsg" runat="server" ForeColor="Red" ></asp:Label>
</td></tr>
</table>
Code Behind for Insert, Update and Delete
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Onrtire
{
public partial class LinqInsert2 : System.Web.UI.Page
{
StudentDBDataContext db = new StudentDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

BindGridview(0);

}
}

1|Page
ASP.NET LINQ to SQL Using C# Lab Manual

private void BindGridview(int p)


{

protected void Button1_Click(object sender, EventArgs e)


{
try
{
LabOne l = new LabOne();
db.IN_LabOne(txtName.Text, txtSex.Text, Convert.ToInt32(txtAge.Text),
txtAddress.Text);
db.SubmitChanges();
lblMsg.Text = "Record inserted successfully!";

}
catch (Exception ex)
{
lblMsg.Text = "Record inserted Failed!";

protected void Button2_Click(object sender, EventArgs e)


{
try
{
LabOne l = new LabOne();
db.UP_LabOne(Convert.ToInt32(txtID.Text), txtName.Text, txtSex.Text,
Convert.ToInt16(txtAge.Text), txtAddress.Text);
db.SubmitChanges();
lblMsg.Text = "Record Updated successfully!";

}
catch (Exception ex)
{
lblMsg.Text = "Record Updated Failed!";

}
}

protected void Button3_Click(object sender, EventArgs e)


{
try
{

2|Page
ASP.NET LINQ to SQL Using C# Lab Manual

LabOne l = new LabOne();


db.DE_LabOne(Convert.ToInt32(txtID.Text));
db.SubmitChanges();
lblMsg.Text = "Record Deleted successfully!";

}
catch (Exception ex)
{
lblMsg.Text = "Record Deleted Failed!";

}
}
}
}
Select
Presentation Layer
<table><tr><td>
<asp:GridView runat="server" ID="SelectData" onp></asp:GridView>

</td></tr></table>
Code Behind for Select
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Onrtire
{
public partial class LINQSelect : System.Web.UI.Page

{
StudentDBDataContext db = new StudentDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

BindGridview(0);

}
}
//LINQ to SQL Select Operation

private void BindGridview(int id)

3|Page
ASP.NET LINQ to SQL Using C# Lab Manual

SelectData.DataSource = db.SE_Linq();
SelectData.DataBind();
db.Dispose();

}
}
}

4|Page

You might also like