Surendra Sharma

Surendra Sharma

Search This Blog

Monday, July 8, 2013

How to create the 3 tier / layer architecture application in .NET

Most of the projects in .NET are developed using tier / layer architecture. Though tier and layer have different meaning, but for all .NET beginners I am using same term.
Below is typical diagram of 3 tier architecture.


  1. Presentation Layer: It’s a User Interface Layer (UIL) where users are interacting with an application. It also contains additional application logic. This layer may be ASP.NET, windows or mobile application. This layer interacts with middle layer. It never directly access data layer.

2.     Business Layer: It’s a Business Logic Layer (BLL) which communicates both to Presentation layer or Data Layer.

3.     Data Layer: It’s a Data Access Logic Layer (DAL) which interacting with database.

Steps to create in Visual Studio using C#
  1. Create one window / web project.
  2. Right click on your solution, Add one new project as shown below


  1. Select one class library project, specify its name as “DataAccessLayer”, as shown below


  1. Similarly create another class library name as “BusinessLogicLayer”
  2. Create classes in DataAccessLayer for StudentInfo table as shown below
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace DataAccessLayer
{
    class StudentInfo
    {
        public DataTable LoadAll()
        {
            //Write code to load all the records of studentinfo Table
            DataTable objDataTable = new DataTable();
            return objDataTable;
        }

        //Write rest of the code for each SP - Insert, Update, delete
    }
}

  1. Build DataAccessLayer Project.
  2. Add the reference of DataAccessLayer in BusinessLogicLayer project as shown below

 



  1. Try to access the DataAccessLayer’s StudentInfoDAL class in BusinessLogicLayer as shown below
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class StudentInfoBLL
    {
        /// <summary>
        /// Call the StudentInfoDAL class of DataAccessLayer
        /// </summary>
        /// <returns></returns>
        public DataTable LoadAll()
        {
            StudentInfoDAL objStudentInfoDAL = new StudentInfoDAL();
            return objStudentInfoDAL.LoadAll();
        }

        //Write rest of the code to call the Insert, Update, delete method of StudentInfoDAL
    }
}

  1. Build BusinessLogicLayer Project.
  2. Similarly Add the reference of BusinessLogicLayer in your window / web project.
  3. Try to access the BusinessLogicLayer’s StudentInfoBLL class.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BusinessLogicLayer;

namespace _3TierDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindStudentInfoData();
            }
        }

        public void BindStudentInfoData()
        {
            StudentInfoBLL objStudentInfoBLL = new StudentInfoBLL();
            DataTable objDataTable = objStudentInfoBLL.LoadAll();
            GridView1.DataSource = objDataTable;
            GridView1.DataBind();
        }
    }
}


Finally your 3-Tier application look like.




 

Hope this is helpful. Please leave your comments for any help / suggestion.

Unable to copy file "obj\Debug\xyzproj.exe" to "bin\Debug\xyzproj.exe". The process cannot access the file 'bin\Debug\xyzproj.exe' because it is being used by another process.

If you come across the error in visual studio like “Unable to copy file "obj\Debug\xyzproj.exe" to "bin\Debug\xyzproj.exe". The process cannot access the file 'bin\Debug\xyzproj.exe' because it is being used by another process.

Solution: - It simply means that your EXE is already running. Close already running exe or window, then build / rebuild and run your application.

Thursday, July 4, 2013

Daily Yoga Chart

I was involved with different kind of exercise - heavy weight, Cardio, running, jogging, Karate, cycling, dance etc.

Now I am doing YOGA from DEC 2012. It’s my personal experience that YOGA is the best among all the exercises.

Here you can find daily YOGA Chart for reference.

It covers daily Aaasans, Pranayam, Dhayan.

Aasans - It includes different body parts exercise which makes body flexible and healthy
Pranayama - It includes different breathing exercises
Dhayan - It includes Meditation which improve mind peace

If you want more details about any Aasans, Pranayama or Dhayan, please leave comments here.




Please share with your friends and leave your valuable feedback.

Wednesday, July 3, 2013

The application domain in which the thread was running has been unloaded

If you are getting error “The application domain in which the thread was running has been unloaded.” while building .NET project in Visual Studio.

Solution 1:  Rebuild the whole solution

Solution 2:  Rebuild individual class libraries and project.

Friday, June 28, 2013

How to get autoincrement assembly version for ASP.NET website

In ASP.NET, developer can not assign assembly version to web site. However they can get assembly version info from console, windows, class library, web applications.

There is one way to get auto increment for assembly version for website.
Create blank solution, add web site project into it.

Add one class library to this solution from File -> Add -> New Project -> Class Library



Write a method which return assembly version of this class library as below

Open “AssemblyInfo.cs” file and paste following code

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright ©  2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e6761a90-b4a8-42cf-a455-c47a240dd8eb")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.0.*")]

namespace ClassLibrary1
{
    public partial class AssemblyInfo
    {
        /// <summary>
        /// Get version numbers from Assembly
        /// </summary>
        /// <returns></returns>
        public static string GetAssemblyVersion()
        {
            var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            return string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
        }
    }
}

Add reference of this class library in website project.

Write following code to get assembly version.

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Version " + ClassLibrary1.AssemblyInfo.GetAssemblyVersion();
}



Now everytime you rebuild your solution, you will get different assembly version.

Thursday, June 27, 2013

Checklist for Outing / Trip / Picnic

1.    Decide place and date
2.    Inform all friends who are interested
3.    Collect money
4.    Take permissions of all the places from authorities
5.    Book Vehicle – bus
6.    Make arrangement for food
7.    Collect personal info of all members – Name, Contact number, Address, email id, emergency contact number
8.    Get vehicle details and driver info with contact number
9.    Get route map from source to destination
10. Always wear comfortable dress – Avoid jeans and tight clothes
11.  Sport Shoes – recommended
12. Avoid high heels sandal, loose chappals
13. Extra pair of dresses, chappals
14. Packed snacks - dry fruits, chiwda, farsan, biscuits, fruit cakes
15. Paper plates
16. Water bottle – **** Most important
17. Towel and napkin
18. Goggle, cap, scarf
19. Sweeter for winter or raincoat during rainy season
20. Umbrella
21. Camera
22. Torch
23. Sunscreen cream – If going to play in water
24. Mobile – Charge it a day before
25. Cash Money
26. Medicines
27. Identity proof
28. Garbage bags – Don’t though wastes at road or destination
29. Poly bags – Useful to keep wet clothes in rainy season
30. Small polythin – Can keep mobile, money, identity proof in rainy season
31. Remember or keep any telephone number on paper in case of mobile lost
32. Same dress code - It’s very easy to identify each other
33. Paper Soap *** or liquid soap – In most of the food place you will never get any soap


I added all the possible items. Please suggest if I am missing anything.
If you feel it’s useful for you, please mark below or leave comments.

How to assign function output to table variable

I have Split function and want to store its output to table varaibale.

DECLARE @ParsedTable AS TABLE (Item VARCHAR(MAX)) ;
INSERT @ParsedTable SELECT * FROM dbo.Udf_split('505,468',',');
SELECT * FROM Employee WHERE  userid IN (SELECT * FROM   @ParsedTable)
 
     
-- =============================================    
-- Description:  Used for Spliting the list    
-- =============================================    
CREATE FUNCTION [dbo].[Udf_split] (@List  VARCHAR(max), @Delim CHAR)    
returns @ParsedTable TABLE (Item VARCHAR(max))    
AS    
  BEGIN    
      DECLARE @list1 VARCHAR(max),@Pos INT, @rList VARCHAR(max)    
   
      SET @list = Ltrim(Rtrim(@list)) + @Delim    
      SET @pos = Charindex(@delim, @list, 1)    
   
      WHILE @pos > 0     
        BEGIN    
            SET @list1 = Ltrim(Rtrim(LEFT(@list, @pos - 1)))    
   
            IF @list1 <> ''    
              INSERT INTO @ParsedTable VALUES (Cast(@list1 AS VARCHAR(max)))    
   
            SET @list = Substring(@list, @pos + 1, Len(@list))    
            SET @pos = Charindex(@delim, @list, 1)    
        END    
   
      SELECT @rlist = COALESCE(@rlist+',', '') + item FROM (SELECT DISTINCT Item FROM @ParsedTable) t    
   
      RETURN    

  END