Work with LINQ using Asp.NET 3.5

Go to Create New Item in your existing project
1.Select LinQ to SQL Classes >> Click Add
2.Go to Server Explorer >> Data Connections >> Add Connection >> Server

Name >>DataBase Name
3.Drag your table in (data classes).dbml as per your working table.
4.Create new Page called LinQ.aspx

A. Write the code below code in your page (.aspx)






onclick="lnkSubmit_Click">Add


onclick="lnkUpdate_Click">Update




B. Write the code below code in your page (.aspx.cs)
1.Declare Namespace as using System.Linq;
B.Declare dbml Referecne as below.
public partial class LINQ : System.Web.UI.Page
{
DataClassesDataContext objDAL = new DataClassesDataContext(); //LINQ to SQL

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind News

bindNews();
}
}

private void bindNews()
{
//using DataTable object
var dt = objDAL.objNews.Where(tt => tt.delstatus.Equals("N")).Take(5);

//get only 5 Result as per your wish change no or Remove if you don't want.
gvNews.DataSource = dt;
gvNews.DataBind();

// Using Qyery

Below is the selected column list you want to show in gridview according to their

date in descending order
/* var varNews = from LN in objDAL.objNews
orderby LN.creation_dt descending
select new
{
Heading = LN.Heading,
ShortDesc = LN.Short_description,
CreationDt = LN.creation_dt
};

gvNews.DataSource = varNews;
gvNews.DataBind();
*/
}
//Submit Data in your table
protected void lnkSubmit_Click(object sender, EventArgs e)
{
string strName = "ved pathak";
bool delStatus = true;

testtable testAdd = new testtable //Add Method
{
cName = strName,
DelStatus = delStatus
};
objDAL.testtables.InsertOnSubmit(testAdd);
objDAL.SubmitChanges();
}

//To update data as per your primary key (here considered as id)
protected void lnkUpdate_Click(object sender, EventArgs e)
{
testtable testUpdt = objDAL.testtables.Single(testup=> testup.id==1);
testUpdt.cName = "Husnain";
testUpdt.DelStatus = false;

objDAL.SubmitChanges();
}
}

Enjoy Programming

Comments