Click here to Skip to main content
Licence CPOL
First Posted 7 Feb 2008
Views 22,624
Downloads 137
Bookmarked 18 times

Introduction to LINQ and VS 2008 web application

very basic idea on asp.net 3.5 website using LINQ
2 votes, 40.0%
1
1 vote, 20.0%
2

3

4
2 votes, 40.0%
5
2.09/5 - 5 votes
μ 2.09, σa 3.59 [?]

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

Software Developer (Senior)
Orion Informatics Limited
Bangladesh Bangladesh

Member
Software engineer with broad experience in enterprise application development, product deployment automation, software test & test automation, application & system management, and manage big projects and team using proven agile technologies.
 
Passionate on Microsoft technologies, developed solutions using asp.net (1.1/2.0/3.5), SQL Server (2005/2008), Sharepoint, Powershell, SSRS, SSAS, WPF, Silverlight, Ajax and so on.
 
Develop innovative application with cutting edge technologies always boosting inside.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPlagiarism? PinmemberMaskaev5:34 13 Mar '08  
AnswerRe: Plagiarism? PinmemberMoim Hossain9:50 28 May '09  
Strange indeed! Shucks | :->
 
Moim Hossain
R&D Project Manager
BlueCielo ECM Solutions BV

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 8 Feb 2008
Article Copyright 2008 by Ashrafur Rahaman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid