LINQ example in asp.net
OR
if you want to
-- > Insert record using Linq OR
-- > Update record using Linq OR
-- > Delete record using Linq OR
-- > Display record using Linq
then do following
-->first add LINQ to SQL Classes from solution explorer(current project)
-->then open one window name AppCode/DataClasses.dbml
-->drag your database or table in this window
( Here :
Table name:Student (id,name,city,age)
Your table name automatic change
when drag in DataClasses.dbml)
-->next copy the following code in our .aspx page
------------------------------------------------------------------------------------------------
--> next copy the following code in your code behind file (.aspx.cs file)
------------------------------------------------------------------------------------------------
OR
if you want to
-- > Insert record using Linq OR
-- > Update record using Linq OR
-- > Delete record using Linq OR
-- > Display record using Linq
then do following
-->first add LINQ to SQL Classes from solution explorer(current project)
-->then open one window name AppCode/DataClasses.dbml
-->drag your database or table in this window
( Here :
Table name:Student (id,name,city,age)
Your table name automatic change
when drag in DataClasses.dbml)
-->next copy the following code in our .aspx page
------------------------------------------------------------------------------------------------
<div>
<div>
Id<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<br />
Name<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
<br />
City<asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox>
<br />
Age<asp:TextBox ID="TextBox4"
runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
Text="insert"
/>
<asp:Button ID="Button2"
runat="server"
onclick="Button2_Click"
Text="update"
/>
<asp:Button ID="Button3"
runat="server"
onclick="Button3_Click"
Text="delete"
/>
<asp:GridView ID="GridView1"
runat="server">
</asp:GridView>
</div>
<asp:linqdatasource runat="server"></asp:linqdatasource>
<asp:ObjectDataSource ID="ObjectDataSource1"
runat="server">
</asp:ObjectDataSource>
</div>
--> next copy the following code in your code behind file (.aspx.cs 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
{
DataClassesDataContext obj = new
DataClassesDataContext();
//create object for access
table
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
disp();
}
}
public void disp()
{
var item = from data in obj.students //Students
is your table name
select data;
GridView1.DataSource = item;
GridView1.DataBind();
}
protected void
Button1_Click(object sender, EventArgs e)
{
student s = new student();
s.name = TextBox2.Text;
s.city = TextBox3.Text;
s.age
= int.Parse(TextBox4.Text);
obj.students.InsertOnSubmit(s);//insert record
obj.SubmitChanges();
disp();//display when after operation is
complete
}
protected void
Button2_Click(object sender, EventArgs e)
{
var data = obj.students.First(hello => hello.id ==
Convert.ToInt16(TextBox1.Text));
data.name = TextBox2.Text;
data.city = TextBox3.Text;
data.age = int.Parse(TextBox4.Text);
obj.SubmitChanges();//update record
disp();//display when after operation is
complete
}
protected void
Button3_Click(object sender, EventArgs e)
{
var delete = obj.students.First(hi => hi.id == int.Parse(TextBox1.Text));
obj.students.DeleteOnSubmit(delete);//delete
record
obj.SubmitChanges();
disp();//display when after operation is complete
}
}
No comments:
Post a Comment