Friday, April 29, 2011

DAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace UserDocMapping
{
    public class DAL
    {

        string ConString = ConfigurationSettings.AppSettings["DBCon"].ToString();

        #region return Dataset Methods With Params
        public DataSet DSWithParams(string SPName, SqlParameter[] param)
        {
            SqlConnection con = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand(SPName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                ad.Fill(ds);
                return ds;

            }
            catch (Exception Ex)
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
        #endregion

        #region return Dataset Methods Without Params
        public DataSet DSWithOutParams(string SPName)
        {
            SqlConnection con = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand(SPName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                ad.Fill(ds);
                return ds;

            }
            catch (Exception Ex)
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        #endregion

        #region return DataTable Methods With Params
        public DataTable DTWithParams(string SPName, SqlParameter[] param)
        {
            DataSet dsLogin = new DataSet();
            SqlConnection con = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand(SPName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                ad.Fill(ds);
                return ds.Tables[0];

            }
            catch (Exception Ex)
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
        #endregion

        #region return DataTable Methods Without Params
        public DataTable DTWithOutParams(string SPName)
        {
            SqlConnection con = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand(SPName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                ad.Fill(ds);
                return ds.Tables[0];

            }
            catch (Exception Ex)
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        #endregion

        #region ExecuteNonQuery
        public void ExecuteNonQuery(string SPName, SqlParameter[] param)
        {
            SqlConnection con = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand(SPName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception Ex)
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
        #endregion

        #region ExecuteScalar
        public object ExecuteScalar(string SPName, SqlParameter[] param)
        {
            SqlConnection con = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand(SPName, con);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                con.Open();
                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                object val = cmd.ExecuteScalar();
                return val;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
        #endregion
    }

}

BAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace UserDocMapping
{
    public class BAL
    {
        DAL _ObjDal = new DAL();// In UI Declare Like BAL _ObjBal=new BAL();

        #region UserLoginCheck
        public DataTable UserLoginCheck(string UserName, string Password)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserName", SqlDbType.VarChar);
            param[0].Value = UserName;
            param[1] = new SqlParameter("@Password", SqlDbType.VarChar);
            param[1].Value = Password;
            DataTable ds = _ObjDal.DTWithParams("SP", param);
            return ds;
        }
        #endregion

        #region GetDoctorsList
        public DataSet GetDoctorsList()
        {
            return _ObjDal.DSWithOutParams("MSP_SelectOnlyLabDoctors");
        }
        #endregion

        #region InsertDetails
        public int InsertDetails(int DocId, int UserId, int CentreId, string Mode)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@DocId", SqlDbType.Int);
            param[0].Value = DocId;
            param[1] = new SqlParameter("@UserId", SqlDbType.Int);
            param[1].Value = UserId;
            param[2] = new SqlParameter("@CentreId", SqlDbType.Int);
            param[2].Value = CentreId;
            param[3] = new SqlParameter("@Mode", SqlDbType.VarChar);
            param[3].Value = Mode;
            object val = _ObjDal.ExecuteScalar("SP", param);
            int _val = Convert.ToInt32(val);
            return _val;
        }
        #endregion

        #region SelectMappedList
        public DataSet SelectMappedList(int DocId, int UserId, int CentreId, string Mode)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@DocId", SqlDbType.Int);
            param[0].Value = DocId;
            param[1] = new SqlParameter("@UserId", SqlDbType.Int);
            param[1].Value = UserId;
            param[2] = new SqlParameter("@CentreId", SqlDbType.Int);
            param[2].Value = CentreId;
            param[3] = new SqlParameter("@Mode", SqlDbType.VarChar);
            param[3].Value = Mode;
            return _ObjDal.DSWithParams("SP", param);
        }
        #endregion

        #region Inert New Doctor or Technician
        public int InsertNewDoctorTechnician(string Username,string Firtname,string Lastname,int RoleId,string Permission)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@Username", SqlDbType.VarChar);
            param[0].Value = Username;
            param[1] = new SqlParameter("@Firtname", SqlDbType.VarChar);
            param[1].Value = Firtname;
            param[2] = new SqlParameter("@Lastname", SqlDbType.VarChar);
            param[2].Value = Lastname;
            param[3] = new SqlParameter("@RoleId", SqlDbType.Int);
            param[3].Value = RoleId;
            param[4] = new SqlParameter("@Permission", SqlDbType.VarChar);
            param[4].Value = Permission;
            object val = _ObjDal.ExecuteScalar("SP", param);
            int _val = Convert.ToInt32(val);
            return _val;
        }
        #endregion

        #region Update New Doctor or Technician
        public void UpdateNewDoctorTechnician(string Firtname,string Lastname,string Restofname, int RoleId,string Permission,int UserId)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@Firtname", SqlDbType.VarChar);
            param[0].Value = Firtname;
            param[1] = new SqlParameter("@Lastname", SqlDbType.VarChar);
            param[1].Value = Lastname;
            param[2] = new SqlParameter("@Restofname", SqlDbType.VarChar);
            param[2].Value = Restofname;
            param[3] = new SqlParameter("@RoleId", SqlDbType.Int);
            param[3].Value = RoleId;
            param[4] = new SqlParameter("@Permission", SqlDbType.VarChar);
            param[4].Value = Permission;
            param[5] = new SqlParameter("@UserId", SqlDbType.Int);
            param[5].Value = UserId;
            _ObjDal.ExecuteNonQuery("SP", param);
        }
        #endregion

       
        #region Insert Doctor Signature
        public void InsertDoctorSignature(int UserId,int RoleId,byte[] ImgBinay)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@UserId", SqlDbType.Int);
            param[0].Value = UserId;
            param[1] = new SqlParameter("@RoleId", SqlDbType.Int);
            param[1].Value = RoleId;
            param[2] = new SqlParameter("@UserSignature", SqlDbType.VarBinary);
            param[2].Value = ImgBinay;
            _ObjDal.ExecuteNonQuery("SP", param);
        }
        #endregion

        #region Update TblUserMapping
        public void UpdateTblUserMapping(int DocId,int UserId)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@DocId", SqlDbType.Int);
            param[0].Value = DocId;
            param[1] = new SqlParameter("@UserId", SqlDbType.Int);
            param[1].Value = UserId;
            _ObjDal.ExecuteNonQuery("SP", param);
        }
        #endregion
    }

}