Wednesday, 26 June 2013

Steps to retrieve records from Database using 3-tier architecture

->Create a ASP.NET website (File-New-Project)

->Click on Solution ->Add - NewProject (Visual C# - Windows - Class Library (Give name DataAccessLayer and DataLinkLayer))

->Give the reference of DLL to DAL And reference of  DAL to the Project

Web.config
------------------
<appSettings>
<add key="Constr" value="Server=ABC-PC;Database=Test;Integrated Security=False;Trusted_Connection=True"/>
</appSettings>


In DAL
-------------

Create New Class AppConfiguration

  public static  class AppConfiguration
    {
        public static string ConnectionString
        {
            get
            {
               return ConfigurationSettings.AppSettings.Get("Constr");
            }
        }
    }


In DLL
-------------

Create New Class  and define some property

public class ClassName
    {
       private string empName;

        public string EmpName
        {
            get { return empName; }
            set { empName = value; }
        }

        private int eno;

        public int Eno
        {
            get { return eno; }
            set { eno = value; }
        }

     
    }



In DAL
-------------

Create New Class and declare the function to fetch record

 public  class ClassName
    {
      SqlConnection con = new SqlConnection(AppConfiguration.ConnectionString);
      public DataTable RetrieveRecords(InsertRecord objInsert)
      {
          DataTable dt = new DataTable();
          SqlCommand cmd = new SqlCommand("retrieve_Records", con);
         //retrieve_Records is the procedure name
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Add("@eno", SqlDbType.Int).Value = objInsert.Eno;
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          da.Fill(dt);
          return dt;

      }
    }
In Aspx Page
----------------------------

    InsertRecord objInsert = new InsertRecord();
    Dal_InsertRecord objDal = new Dal_InsertRecord();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           objInsert.Eno = 1;
           DataTable dt= objDal.RetrieveRecords(objInsert);

        }

    }

No comments:

Post a Comment