5,664,339 members and growing! (17,350 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

Introduction to LINQ and VS 2008 web application

By Ashrafur Rahaman

very basic idea on asp.net 3.5 website using LINQ
C# (C# 3.0, C#), .NET (.NET, .NET 3.5), ASP.NET, Dev

Posted: 7 Feb 2008
Updated: 7 Feb 2008
Views: 6,874
Bookmarked: 11 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.46 Rating: 2.09 out of 5
2 votes, 40.0%
1
1 vote, 20.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 40.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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.

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.

Points of 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.

Happy coding :)

History

Just sharing some basic ideas so you become interested to work in VS2008 and LINQ. As I am already worlking on it and enjoying more to work than VS 2005.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ashrafur Rahaman


Lead programmers on project to finish the outsourced product, analyze requirement and define solution architecture for project. Design and maintain database. Software quality assurance, source control, bug tracking, feature and project schedule planning. Developed Statement of Work, setup project plan, designed Data Model and Web Application architecture in .NET 2.0/3.5 and SQL Server, and supervise a .net developer’s team.
Occupation: Team Leader
Company: Latitude-23
Location: Bangladesh Bangladesh

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralPlagiarism?memberMaskaev5:34 13 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Feb 2008
Editor:
Copyright 2008 by Ashrafur Rahaman
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project