Saturday 5 August 2017

How to code Data Access Layer in ASP.Net ?

Data Access Layer is the layer in which we define the Methods for CRUD Operations.
Here we are using a Student Registration for Insert data Submitted by Student from ASP Web form,
the step by step process is given below.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for studentregistration_DAl
/// </summary>
public class studentregistration_DAl
{
    public static string _connectionStr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection conn = new SqlConnection(_connectionStr);

    #region Save RegisterStudent
    public void RegisterStudent(studentregistration_BEL objStudentReg_BEL)
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_Insert_StudentRegistration";
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                    cmd.Parameters.Add("@GenderID", SqlDbType.Int).Value = objStudentReg_BEL.GenderID;
                    cmd.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = objStudentReg_BEL.FirstName;
                    cmd.Parameters.Add("@lastname", SqlDbType.NVarChar).Value = objStudentReg_BEL.LastName;
                    cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = objStudentReg_BEL.UserName;
                    cmd.Parameters.Add("@DateOfBirth", SqlDbType.DateTime).Value = Convert.ToDateTime(objStudentReg_BEL.DOB).ToString("MM/dd/yyyy");
                    cmd.Parameters.Add("@MobileNumber", SqlDbType.NVarChar).Value = objStudentReg_BEL.Mobile;
                    cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = objStudentReg_BEL.Email;
                    cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = objStudentReg_BEL.Password;
                   cmd.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }
    }
    #endregion RegisterStudent
}


Firstly Create a folder into App_code folder of Profect and named it DATALAYER.
In DATALAYER ==> ADD NEW ITEM ==>Studentregistration_DAL  
In The Above Code STudentregistration_DAL is the name of CLass in which code is written and we
access connection from Web.config file and store it into the static variable _connectionStr and for accessing connetion string from web.config file we have to use Using System.Configuration name space at the top of the page.

   public static string _connectionStr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection conn = new SqlConnection(_connectionStr);

We use a method named  RegisterStudent for storing data and pass a object of  studentregistration_BEL  class
 public void RegisterStudent(studentregistration_BEL objStudentReg_BEL)

And then create command object and add parameters into it .....

No comments:

Post a Comment

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...