About Me

My photo
New Delhi, Delhi, India
I am working in Infosys.

April 30, 2015

ClsCommon

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using DBLibrary;
using System.Security.Cryptography;


/// <summary>
/// Summary description for clsPatientInfo
/// </summary>
public class clsCommon
{
    public  string ConnStr;
    public String User_ID { get; set; }
    public String Token_No { get; set; }
    public String UserName { get; set; }
    public String Login_Date { get; set; }
    public int DepID { get; set; }
    public string Password { get; set; }
    public string ProviderID { get; set; }
    public int Profile { get; set; }
    public Boolean Status { get; set; }
    public string StaffNumber { get; set; }
    public string FullName { get; set; }
    public int CounterID { get; set; }
    public string UserType { get; set; }
     static List<clsGetProvider> lstGetProvider = new List<clsGetProvider>();
     List<clsGetProvider> lstPatientList_ASMX1 = new List<clsGetProvider>();
     List<clsGetProvider> lstGet_Final_Provider = new List<clsGetProvider>();
    public clsCommon()
    {
       
                CnSettings clsObjSetting = new CnSettings();
                ConnStr = clsObjSetting.ConnectionStr;
      
    }
   
    public DataSet RunSQLQuery(string sql)
    {
        DataSet ds = new DataSet();       
        ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql);
        return ds;
       
    }

    public Int32 MaxId(string FieldName, string TableName)
    {
        int i;
        object value = null;
        string str = "select isnull(max(" + FieldName + "),0) as MaxID from " + TableName + " ";
        DataSet ds = new DataSet();
        ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, str);
        value =Convert.ToInt32( ds.Tables[0].Rows[0]["MaxID"].ToString());

        i = Convert.ToInt32(value);

        i = i + 1;
        return i;

    }

    public string GetDateForUI(string obj)
    {
        DateTime objDate;
        if (DateTime.TryParse(obj.ToString(), out objDate) && objDate.Year != 1)
            return objDate.ToString("dd/MM/yyyy");
        else
            return "";
    }
    public DateTime GetDateForDBAddOneDate(object obj)
    {
        DateTime objDate;
        if (!string.IsNullOrEmpty(obj.ToString()))
        {
            obj = (object)GetDateInMMDDYYYY(obj);
            DateTime a = Convert.ToDateTime(obj.ToString());
            obj = a.AddDays(1);
        }

        if (DateTime.TryParse(obj.ToString(), System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out objDate) && objDate.Year != 1)
            return objDate;
        else
            return objDate;

    }
    public DateTime GetDateForDB(object obj)
    {
        DateTime objDate;
        if (!string.IsNullOrEmpty(obj.ToString()))
            obj = (object)GetDateInMMDDYYYY(obj);

        if (DateTime.TryParse(obj.ToString(), System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out objDate) && objDate.Year != 1)
            return objDate;
        else
            return objDate;
      
    }
    public String GetDateTimeOP(object obj)
    {
        String objDate="";
        if (!string.IsNullOrEmpty(obj.ToString()))
        {
            char rowSep = ' ';
            objDate = GetDateForDB(obj).ToString();
            string[] Text = objDate.Split(rowSep);
            if (Text.Length >= 1)
            {
                objDate = Text[0].TrimStart().ToString() + " ";
                objDate = objDate + DateTime.Now.ToShortTimeString();
                return objDate;
            }
            else
                return objDate;
        }
        else
            return objDate;


    }


    public String GetDateInMMDDYYYY(object obj)
    {
        String[] objDate = obj.ToString().Split('/');
        return objDate[1] + "/" + objDate[0] + "/" + objDate[2];
    }


    public bool IsDateTime(object Expression)
    {
        try
        {
            if (Expression == null)
                return false;

            DateTime objVal;
            if (DateTime.TryParse(Expression.ToString(), System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out objVal))
                return true;
            else

                return false;
        }
        catch (Exception ex)
        {
             throw ex;
            return false;
        }
    }


    public DataSet UserLogin(String UserName, String Pass, String ProviderID)
    {

        string sql = "select * from UserLogin where UserName = '" + UserName + "'  and Password = '" + Pass + "'";
        DataSet ds = new DataSet();


        ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql);
       
      
        return ds;
       
    }
   



    public DataSet DepartmentDetail(string UserID,Int32 DepartmentID)
    {

        string sql = "select * from Userlogin where UserName = '" + UserID + "'";
        DataSet ds = new DataSet();
        ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql);
       
        return ds;
      
    }


    public DataSet DataGridFill(String FldName, String FldValue, String tableName)
    {
        string sql = null;
        if (String.IsNullOrEmpty(FldName))
            sql = "select * from " + tableName + "";
        else
        {
            FldName = FldName + " = ";
            /// FldValue = FldValue ;
            sql = "select * from " + tableName + " where " + FldName + " '" + FldValue + "'";
        }
        DataSet ds = new DataSet();
        ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql);
        return ds;
       
    }


    public bool CheckSessionValue( String ProviderID,  string userID)
    {
        bool  mes = false ;
        
        if (ProviderID == "")
        { mes = true; }
        
        if (userID == "")
            mes = true ;
        return mes;
   
    }

    public bool CheckCookiesValue(String ProviderID, string userID)
    {
        bool mes = false;

        if (ProviderID == "" || ProviderID==null)
        { mes = true; }

        if (userID == "" || userID==null)
            mes = true;
        return mes;

    }
    public DateTime GetDateForDBNew(object obj)
    {
        DateTime objDate;
        String FORMAT = "dd/MM/yyyy";
        if (!DateTime.TryParseExact(obj.ToString(), FORMAT, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out objDate))
        {
            DateTime.TryParse("01/01/0001", out objDate);
        }

        return objDate;
       
    }
    public Int64 GetInteger64(object objVal)
    {
        Int64 intValue = 0;
        if (objVal == null) return 0;
        if (objVal is DBNull) return 0;
        if (objVal is Int64) return (Int64)objVal;
        Int64.TryParse(objVal.ToString(), out intValue);
        return intValue;
    }
   

    public Int32 GetInteger32(object objVal)
    {
        Int32 intValue = 0;
        if (objVal == null) return 0;
        if (objVal is DBNull) return 0;
        if (objVal is Int32) return (Int32)objVal;
        Int32.TryParse(objVal.ToString(), out intValue);
        return intValue;
    }

    public DateTime GetTimeForDB(object obj)
    {
        DateTime objDate;
        if (DateTime.TryParse("01/01/1900 " + obj.ToString(), out objDate) && objDate.Year != 1)
            return objDate;
        else
            return objDate;
    }

    public object GetTimeForUI(object obj)
    {
        DateTime objDate;
        if (DateTime.TryParse(obj.ToString(), out objDate) && objDate.Year != 1)
            return objDate.ToString("hh:mm:ss tt");
        else
            return null;
    }
    public DateTime GetDate(object objVal)
    {
        if (objVal == null) return Convert.ToDateTime(null);
        if (objVal is DBNull) return Convert.ToDateTime(null);
        if (objVal is string && string.IsNullOrEmpty(objVal.ToString())) return Convert.ToDateTime(null);
        return Convert.ToDateTime(objVal);
    }
    public DateTime GetDateForDB_APP(object obj)
    {

        DateTime objDate;
        String FORMAT = "dd/MM/yyyy";
        if (!DateTime.TryParseExact(obj.ToString(), FORMAT, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out objDate))
        {
            DateTime.TryParse("01/01/0001", out objDate);
        }

        return objDate;
    }

    public DateTime GetDateForDB_APP_FromDB(object obj)
    {
        DateTime objDate;
        String FORMAT = "dd/MM/yyyy";
        if (!DateTime.TryParseExact(obj.ToString(), FORMAT, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out objDate))
        {
            DateTime.TryParse("01/01/0001", out objDate);
        }

        return objDate;
    }

    public void GetAge(DateTime DOB, System.Web.UI.WebControls.TextBox txt)
    {
        if (DOB < DateTime.Now)
        {
            TimeSpan dateDiff = DateTime.Now - DOB;
            DateTime age = new DateTime(dateDiff.Ticks);

            Int32 Years = age.Year - 1;
            Int32 Month = age.Month - 1;
            Int32 Days = age.Day - 1;
            txt.Text = Years.ToString("00") + " Y, " + Month.ToString("00") + " M, " + Days.ToString("00") + " D ";

        }

    }
    public string GetUniqueFileName(string filename)
    {
        try
        {
            string[] ext = new string[5];
            ext = filename.Split('.');
            return Guid.NewGuid().ToString() + "." + ext[ext.Length - 1];
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
    public DateTime GetDateForDB_IPD(object obj)
    {
    
        DateTime objDate;
        String FORMAT = "dd/MM/yyyy";
        if (!DateTime.TryParseExact(obj.ToString(), FORMAT, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out objDate))
        {
            DateTime.TryParse("01/01/0001", out objDate);
        }

        return objDate;
    }

    public DataSet DeleteData(String FldName, String FldValue, String tableName)
    {
        string sql = null;
        if (String.IsNullOrEmpty(FldName))
            sql = "Delete from " + tableName + "";
        else
        {
            FldName = FldName + " = ";
            /// FldValue = FldValue ;
            sql = "delete from " + tableName + " where " + FldName + " '" + FldValue + "'";
        }
        DataSet ds = new DataSet();
        ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql);
        return ds;
    }

    public string  Database(string ProviderID)
    {
       // try
        //{
            string con = null;
            string sql = "select * from IE_Database_Setting where ProviderID='" +  HttpContext.Current.Session["Check"].ToString() + "' ";
            DataSet ds = new DataSet();

            ds = SqlHelper.ExecuteDataset(ConnStr, CommandType.Text, sql);
         
           if (ds.Tables[0].Rows.Count > 0)
            {
                ConnStr = ds.Tables[0].Rows[0]["ConnectionString"].ToString();
                ;
            }
            return ConnStr;
       
    }

    public string autostring10()
    {

        string id = Guid.NewGuid().ToString("N").Substring(15);
        return id;
    }
    public string autopassword()
    {
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
        var random = new Random(); string output = "";
        for (int i = 0; i < 7; i++)
        {
            output += chars[random.Next(chars.Length)];
        }
        return output;
    }

    public System.Collections.Generic.List<clsCommon> CheckLogin(String UserName, String Pass, Int32 CounterID, String QueryType, string ProviderId)
    {

        SqlDataReader dr = null;
        System.Collections.Generic.List<clsCommon> lstLoginInfo = new System.Collections.Generic.List<clsCommon>();
      
        SqlParameter[] ParamList = new SqlParameter[5];
        ParamList[0] = new SqlParameter("@UserName", UserName);
        ParamList[1] = new SqlParameter("@Password", Pass);
        ParamList[2] = new SqlParameter("@ProviderID", ProviderId);
        ParamList[3] = new SqlParameter("@QueryType", QueryType);
        ParamList[4] = new SqlParameter("@CounterID", CounterID);
        dr = SqlHelper.ExecuteReader(ConnStr, CommandType.StoredProcedure, "Proc_Create_Login", ParamList);

        while (dr.Read())
        {
            clsCommon objApp = new clsCommon();
            objApp.User_ID = dr["UserID"].ToString();
            objApp.UserName = dr["UserName"].ToString();
            objApp.Password = dr["Password"].ToString();
            objApp.ProviderID = dr["ProviderID"].ToString();
            objApp.Profile = Convert.ToInt32(dr["Profile"].ToString());
            objApp.Status = Convert.ToBoolean(dr["Status"].ToString());
            objApp.StaffNumber = dr["StaffNumber"].ToString();
            objApp.FullName = dr["FullName"].ToString();
          
            lstLoginInfo.Add(objApp);
        }
        return lstLoginInfo;
    }

    public string ChopStringWithLen(string StrName, int Len)
    {
        if (StrName.Length > Len)
            return (StrName.Substring(0, Len) + "...");
        else
            return StrName;
    }



    public string EncryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

        // Step 2. Create a new TripleDESCryptoServiceProvider object
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

        // Step 3. Setup the encoder
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;

        // Step 4. Convert the input string to a byte[]
        byte[] DataToEncrypt = UTF8.GetBytes(Message);

        // Step 5. Attempt to encrypt the string
        try
        {
            ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
            Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
        }
        finally
        {
            // Clear the TripleDes and Hashprovider services of any sensitive information
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }
        // Step 6. Return the encrypted string as a base64 encoded string
        return Convert.ToBase64String(Results);
    }

    public string DecryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

        // Step 1. We hash the passphrase using MD5
        // We use the MD5 hash generator as the result is a 128 bit byte array
        // which is a valid length for the TripleDES encoder we use below

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

        // Step 2. Create a new TripleDESCryptoServiceProvider object
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

        // Step 3. Setup the decoder
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;

        // Step 4. Convert the input string to a byte[]
        byte[] DataToDecrypt = Convert.FromBase64String(Message);

        // Step 5. Attempt to decrypt the string
        try
        {
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
        }
        finally
        {
            // Clear the TripleDes and Hashprovider services of any sensitive information
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }

        // Step 6. Return the decrypted string in UTF8 format
        return UTF8.GetString(Results);
    }
    public System.Collections.Generic.List<clsCommon> CheckLoginAmbiguity(Int64 UserID, String QueryType)
    {

        SqlDataReader dr = null;
        System.Collections.Generic.List<clsCommon> lstLoginInfo = new System.Collections.Generic.List<clsCommon>();
        SqlParameter[] ParamList = new SqlParameter[2];
        ParamList[0] = new SqlParameter("@User_Id", UserID);
        ParamList[1] = new SqlParameter("@QueryType", QueryType);
        dr=SqlHelper.ExecuteReader(ConnStr, CommandType.StoredProcedure, "Proc_IE_LoginStatus", ParamList);
      
         while (dr.Read())
        {
            clsCommon objApp = new clsCommon();
            objApp.User_ID = dr["User_Id"].ToString();
            objApp.Token_No = dr["Token_No"].ToString();
            objApp.Login_Date = dr["Login_Date"].ToString();
            //objApp.Status = Convert.ToBoolean(dr["Status"].ToString());
            lstLoginInfo.Add(objApp);
        }
        return lstLoginInfo;
    }

    public System.Collections.Generic.List<clsCommon> CheckForLogout(Int64 UserID, string  TokenIdNo, String QueryType)
    {

        SqlDataReader dr = null;
        System.Collections.Generic.List<clsCommon> lstLoginInfo = new System.Collections.Generic.List<clsCommon>();
        SqlParameter[] ParamList = new SqlParameter[3];
        ParamList[0] = new SqlParameter("@User_Id", UserID);
        ParamList[1] = new SqlParameter("@Token_No", TokenIdNo);
        ParamList[2] = new SqlParameter("@QueryType", QueryType);
        dr = SqlHelper.ExecuteReader(ConnStr, CommandType.StoredProcedure, "Proc_IE_LoginStatus", ParamList);
        while (dr.Read())
        {
            clsCommon objApp = new clsCommon();
            objApp.User_ID = dr["User_Id"].ToString();
            objApp.Token_No = dr["Token_No"].ToString();
            objApp.Login_Date = dr["Login_Date"].ToString();
            //objApp.Status = Convert.ToBoolean(dr["Status"].ToString());
            lstLoginInfo.Add(objApp);
        }
        return lstLoginInfo;
    }

    public System.Collections.Generic.List<clsCommon> LoginCounter()
    {
        SqlDataReader dr = null;
        System.Collections.Generic.List<clsCommon> lstLoginInfo = new System.Collections.Generic.List<clsCommon>();
      
        SqlParameter[] ParamList = new SqlParameter[1];      
        ParamList[0] = new SqlParameter("@QueryType", "SlectCount");
        dr=SqlHelper.ExecuteReader(ConnStr, CommandType.StoredProcedure, "Proc_IE_LoginStatus", ParamList);
        while (dr.Read())
        {
            clsCommon objApp = new clsCommon();
            objApp.User_ID = dr["User_Id"].ToString();
            objApp.Token_No = dr["Token_No"].ToString();
            objApp.Login_Date = dr["Login_Date"].ToString();
            //objApp.Status = Convert.ToBoolean(dr["Status"].ToString());
            objApp.UserName =dr["UserName"].ToString();
           

            lstLoginInfo.Add(objApp);
        }
        return lstLoginInfo;
    }

    public void DeleteLogin(String TokenNo, String QueryType)
    {
        SqlParameter[] ParamList = new SqlParameter[2];
        ParamList[0] = new SqlParameter("@QueryType", QueryType);
        ParamList[1] = new SqlParameter("@Token_No", TokenNo);
        SqlHelper.ExecuteDataset(ConnStr, CommandType.StoredProcedure, "Proc_IE_LoginStatus", ParamList);
    }
    public void InsertLogin(Int64 UserID, String QueryType, String Token_No)
    {
        SqlParameter[] ParamList = new SqlParameter[3];
        ParamList[0] = new SqlParameter("@QueryType", QueryType);
        ParamList[1] = new SqlParameter("@User_Id", UserID);
        ParamList[2] = new SqlParameter("@Token_No", Token_No);
        SqlHelper.ExecuteDataset(ConnStr, CommandType.StoredProcedure, "Proc_IE_LoginStatus", ParamList);
    }

}

No comments:

Post a Comment