Saturday, 22 October 2011

DB Helper

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


namespace ProjectHelper
{
    public class DBAccessHelper
    {
        /***************************************** Connection String ****************************************/
        public static string ConnectionString;
        /***************************************** Private Methods ****************************************/
        private static SqlConnection OpenConnection()
        {
            SqlConnection SqlCon = new SqlConnection();
            if (ConnectionString == string.Empty)
                throw (new ArgumentOutOfRangeException("ConnectionString"));

            SqlCon = new SqlConnection(ConnectionString);

            if (SqlCon.State == ConnectionState.Open)
                SqlCon.Close();

            SqlCon.Open();

            return SqlCon;
        }

        private static void SetCommandType(SqlCommand sqlCmd, CommandType cmdType, string cmdText)
        {
            sqlCmd.CommandTimeout = 0;
            sqlCmd.CommandType = cmdType;
            sqlCmd.CommandText = cmdText;
        }

        private static void AddParamToSQLCmd(SqlCommand sqlCmd, SqlParameter[] sqlParams)
        {
            if (sqlCmd == null)
                throw (new ArgumentNullException("sqlCmd"));

            for (int cnt = 0; cnt < sqlParams.Length; cnt++)
            {
                sqlCmd.Parameters.Add(sqlParams[cnt]);
            }
        }

        private static DataTable FillDataTable(SqlCommand sqlCmd)
        {
            SqlConnection SqlCon = new SqlConnection();
            DataTable tblResult = new DataTable();
            SqlDataAdapter _sqlDataAdapter = new SqlDataAdapter();

            SqlCon = OpenConnection();
            sqlCmd.Connection = SqlCon;
            _sqlDataAdapter.SelectCommand = sqlCmd;
            _sqlDataAdapter.Fill(tblResult);

            return tblResult;
        }

        private static DataSet FillDataSet(SqlCommand sqlCmd)
        {
            DataSet dsResult = new DataSet();
            SqlConnection SqlCon = new SqlConnection();
            SqlDataAdapter _sqlDataAdapter = new SqlDataAdapter();

            SqlCon = OpenConnection();
            sqlCmd.Connection = SqlCon;
            _sqlDataAdapter.SelectCommand = sqlCmd;
            _sqlDataAdapter.Fill(dsResult);

            return dsResult;
        }
        private static object ExecuteScalarCommand(SqlCommand sqlCmd)
        {
            SqlConnection SqlCon = new SqlConnection();
            object reslt = new object();

            SqlCon = OpenConnection();
            sqlCmd.Connection = SqlCon;
            reslt = sqlCmd.ExecuteScalar();

            return reslt;
        }
        private static int ExecuteNonQuery(SqlCommand sqlCmd)
        {
            int Counter = 0;
            SqlConnection SqlCon = new SqlConnection();

            SqlCon = OpenConnection();
            sqlCmd.Connection = SqlCon;
            Counter = sqlCmd.ExecuteNonQuery();

            return Counter;

        }
        public static SqlParameter SetParameter(string paramId, SqlDbType sqlType, int paramSize, ParameterDirection paramDirection, object paramvalue)
        {
            SqlParameter newSqlParam = new SqlParameter();
            newSqlParam.ParameterName = paramId;
            newSqlParam.SqlDbType = sqlType;
            newSqlParam.Direction = paramDirection;

            if (paramSize > 0)
                newSqlParam.Size = paramSize;

            if (paramvalue != null)
                newSqlParam.Value = paramvalue;

            return newSqlParam;
        }
        public static SqlParameter SetTableValuedParameter(string paramId, SqlDbType sqlType, ParameterDirection paramDirection, string typename, DataTable paramvalue)
        {
            SqlParameter newSqlParam = new SqlParameter();
            newSqlParam.ParameterName = paramId;
            newSqlParam.SqlDbType = sqlType;
            newSqlParam.Direction = paramDirection;
            newSqlParam.TypeName = typename;

            if (paramvalue != null)
                newSqlParam.Value = paramvalue;

            return newSqlParam;
        }

        /***************************************** Public Methods ****************************************/


        /*1. GetDataTableFromProcedure - Used  to get datatables using Procedure  */

        public static DataTable GetDataTableFromProcedure(string CmdText, bool isParams, SqlParameter[] sqlParams)
        {
            SqlCommand sqlCmd = new SqlCommand();

            SetCommandType(sqlCmd, CommandType.StoredProcedure, CmdText);

            if (isParams)
            {
                AddParamToSQLCmd(sqlCmd, sqlParams);
            }

            DataTable objDt = new DataTable();
            objDt = FillDataTable(sqlCmd);
            return objDt;
        }

        /*2. GetDataSetFromProcedure - Used  to get collection of datatables using procedure as Dataset  */

        public static DataSet GetDataSetFromProcedure(string CmdText, bool isParams, SqlParameter[] sqlParams)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, CmdText);

            if (isParams)
            {
                AddParamToSQLCmd(sqlCmd, sqlParams);
            }

            DataSet objDs = new DataSet();
            objDs = FillDataSet(sqlCmd);
            return objDs;
        }

        /*3. DataTransactionsByProcedure - Used for excure procedures to save ,update ,block etc   */

        public static string DataTransactionsByProcedure(string CmdText, bool isParams, SqlParameter[] sqlParams)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, CmdText);

            if (isParams)
            {
                AddParamToSQLCmd(sqlCmd, sqlParams);
            }

            object objResult = ExecuteScalarCommand(sqlCmd);
            return objResult.ToString();
        }

        /*4. GetDataTableFromQuery -  Used  to get datatables using query  */

        public static DataTable GetDataTableFromQuery(string CmdText)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.Text, CmdText);

            DataTable objDt = new DataTable();
            objDt = FillDataTable(sqlCmd);
            return objDt;
        }

        /*5. DataTransactionsByQuery - Used for excure query to save ,update ,block etc   */

        public static string DataTransactionsByQuery(string CmdText)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.Text, CmdText);
            object objRecCnt = ExecuteNonQuery(sqlCmd);
            return objRecCnt.ToString();
        }

        /*6. GetDataSetFromQuery - Used  to get collection of datatables using query as Dataset  */

        public static DataSet GetDataSetFromQuery(string CmdText)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.Text, CmdText);

            DataSet objDt = new DataSet();
            objDt = FillDataSet(sqlCmd);
            return objDt;
        }

        ///
        /// SqlDataReader
        /// This member takes a SqlCommand (with all properties already set),
        /// executes it and returns a SqlDataReader.
        ///

        public SqlDataReader getSqlDataReader(SqlCommand myCommand)
        {
            //Create a SQL connection
            SqlConnection myConnection = new SqlConnection();
            myConnection = OpenConnection();
            myCommand.Connection = myConnection;

            //open a dataReader and return
            SqlDataReader myReader = null;
            try
            {
                //myConnection.Open();
                myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                //Call custom local error handler

            }
            return myReader;
        }


    }
}




---------------------------------------------FOR CONNECTION IN GLOBAL.ASAX------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ProjectHelper;
using System.Configuration;

namespace NAMESPACENAME
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

            if (ConfigurationManager.ConnectionStrings["CONNECTIONSTRING"] == null)
                throw (new NullReferenceException("ConnectionString configuration is missing from your App.config file."));

            string connectionString = ConfigurationManager.ConnectionStrings["CONNECTIONSTRING"].ConnectionString;

            if (String.IsNullOrEmpty(connectionString))
                throw (new NullReferenceException("ConnectionString configuration is missing from your App.config file."));

            DBAccessHelper.ConnectionString = connectionString;

        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends.
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer
            // or SQLServer, the event is not raised.

        }

    }
}


-------------------------------Connection string
  
     

Tuesday, 10 May 2011

Disposition Approval Workflow

The Disposition Approval workflow is meant to be used in order to ensure that SharePoint does not
become cluttered with documents that are no longer needed. The disposition approval process entails
simply choosing whether a document should be deleted or not. If a document is to be deleted, optionally
select a checkbox to indicate if a copy of the item’s metadata should be retained in the audit log..


1.For create a disposition workflow for a document library select Library tab then select Library Settings. 2.Select Workflow settings

3. Select Disposition Workflow from Workflow options.Give name to WorkFlow and click ok.



4.To start  Workflow for a document Click on the document and select Workflows

5.Click on the Workflow name to start workflow.
6.WorkFlow Started

7.Click on WorkFlow status will display details about the workflow

8. Click on the title of the document.

9.Select Delete this item to delete the document.If you wanted to keep copy of items metadata check the option Retain a copy of the items metadata in auditlog.