Introduction to LINQ and VS 2008 web application

No.of Views1015
Bookmarked0 times
Downloads 
Votes0
By  ashraf   On  16 Feb 2010 03:02:44
Tag : LINQ , How to
very basic idea on asp.net 3.5 website using LINQ
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This article will give you an overview how to make am asp.net 3.5 website using LINQ. This is very easy and simple way to build a web site using LINQ because this will provide you SQL classes.

Step By Step

Here are the steps and procedure to make your application.Prepare Your application

1. Create a new web site
2. Go to “Add new item” and select “LINQ to SQL Classes” and give a name of that .dbml file(TestDatabase.dbml)
3. Now go to “Server Explorer” and Select your database from “Data Connections”. Add tables for which you want to make SQL Class. See in the Solution explorer your SQL class has created under the dbml file you have taken.

Image Loading

Insert, delete, update methods for your application

Now it’s time to make your database helper class. Take a class file names DatabaseHelper.cs
In DatabaseHelper.cs make a method to get database, when you have made your dbml file it makes a DataContext for you as my database name is TestDatabase so the DataContext name is “TestDatabaseDataContext”.

To get DataContext of you current database write a method GetDatabaseDataContext()

public const string ConnectionStringName = "TestDBConnectionString";
public static TestDatabaseDataContext GetDatabaseData()
{
var db = new TestDatabaseDataContext(ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString);
return db;
} 

 

Lets Write a generic insert method to insert data in table 

public static bool Insert<T>(T obj) where T : class
{
using (var db = GetDatabaseData())
{
db.GetTable<T>().InsertOnSubmit(obj);
db.SubmitChanges();
return true;
}
}

 

This is the generic update method to update data in tables

 

public static void Update<T>(T obj, Action<T> update) where T : class
{
using (var db = GetDatabaseData())
{
db.GetTable<T>().Attach(obj);
update(obj);
db.SubmitChanges();
}
}

 Here is the generic delete method to delete data from table 

public static void Delete<T>(T obj) where T : class, new()
{
using (var db = GetDatabaseData())
{
db.GetTable<T>().Attach(obj);
db.GetTable<T>().DeleteOnSubmit(obj);
db.SubmitChanges();
}
}

So your database helper class is ready to make insert, delete, update operation with any table in your Datatabse which you have enter in you DataContext. Here i am showing you one example of how to call these methods from your web page so you can use these DatabaseHelper class perfectly.

Call database methods from web pages

Here to show how to use these DatabaseHelper methods I am using a sample table tblUser . To get data from user you will write a very simple query look like that 

public tblUser GetUserById(int Id)
{
using (var db = DatabaseHepler.GetDatabaseData())
{
var user = (from e in db.tblUsers
where e.ID == Id
select e).FirstOrDefault();
return user;
}
} 

Now to insert data in your table call a method InsertUser(tblUser user), here parameter is coming with new data of user and will insert this data to table. Here is the code to call Databasehelper Insert Method. 

public void InsertUser(tblUser user)
{
DatabaseHepler.Insert<tblUser>(user);
}

 Code for Update and delete data are

public void UpdateUser(tblUser user)
{
DatabaseHepler.Update<tblUser>(user, delegate(tblUser t)
{
t.ID = user.ID;
t.UserName = user.UserName;
t.UserPass = user.UserPass;
});
}
public void DeleteUser(int Id)
{
tblUser user = GetUserById(1);
DatabaseHepler.Delete<tblUser>(user);
}

 So you are now prepare to build your website using in ASP.NET 3.5 using LINQ. It is easy and you can write your code faster.

About my interest

As i am working one of my project in ASP.NET 3.5 and using LINQ I am enjoying the latest technology. I feel every developer can enjoy such interesting programming so sharing my basic idea on it.

Sample Project Source

Download source files -6 kb

 
Sign Up to vote for this article
 
About Author
 
ashraf
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-01 Aug 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top