Click here to Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralPlagiarism?
Maskaev
5:34 13 Mar '08  
I downloaded sample web application from Omar Al Zabir
http://www.codeplex.com/dropthings

It seems that this article code is quite similar to the application source code, even procedures have the same names and signatures.

E.g.

      public static class DatabaseHelper
      {
            public const string ConnectionStringName = "DashboardConnectionString";
            public const string ApplicationID = "fd639154-299a-4a9d-b273-69dc28eb6388";
            public readonly static Guid ApplicationGuid = new Guid(ApplicationID);

            public static DashboardDataContext GetDashboardData()
            {
                  var db = new DashboardDataContext(ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString);
                  return db;
            }

            public static void Update<t>(T obj, Action<t> update) where T : class
            {
                  using (var db = GetDashboardData())
                  {  
                        db.GetTable<t>().Attach(obj);
                        update(obj);
                        db.SubmitChanges();
                  }
            }
            public static void UpdateAll<t>(List<t> items, Action<t> update) where T : class
            {
                  using (var db = GetDashboardData())
                  {
                        Table<t> table = db.GetTable<t>();
                        foreach (T item in items)
                        {
                              table.Attach(item);
                              update(item);
                        }

                        db.SubmitChanges();
                  }
            }
            public static void Delete<t>(T entity) where T : class,new()
            {
                  using (var db = GetDashboardData())
                  {
                        Table<t> table = db.GetTable<t>();
                        table.Attach(entity);
                        table.DeleteOnSubmit(entity);
                        db.SubmitChanges();
                  }
            }
            public static void Insert<t>(T obj) where T : class
            {
                  using (var db = GetDashboardData())
                  {
                        db.GetTable<t>().InsertOnSubmit(obj);
                        db.SubmitChanges();
                  }
            }
      }


Quite strange. Unsure
GeneralRe: Plagiarism?
Moim Hossain
9:50 28 May '09  
Strange indeed! Shucks

Moim Hossain
R&D Project Manager
BlueCielo ECM Solutions BV


Last Updated 8 Feb 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010