Friday, February 18, 2011

Get LastDayOfMonth,FirstDayOfMonth,LastDayOfPreviousMonth,FirstDayOfPreeviousMonth From Date


    public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
    {

        DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);

        return firstDayOfTheMonth.AddMonths(1).AddDays(-1);

    }

    public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
    {

        return new DateTime(dateTime.Year, dateTime.Month, 1);

    }


    public DateTime LastDayOfPreviousMonthFromDateTime(DateTime dateTime)
    {

        DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month - 1, 1);

        return firstDayOfTheMonth.AddMonths(1).AddDays(-1);

    }


    public DateTime FirstDayOfPreeviousMonthFromDateTime(DateTime dateTime)
    {

        return new DateTime(dateTime.Year, dateTime.Month - 1, 1);

    }

Create Current Page Name

public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }

Create Error File

public void LogErrorToFile(string strPageName, string strMsg)
    {
        string strLogFilePath = System.Web.HttpContext.Current.Server.MapPath("~/Log/StateContractEmployee.txt");
        System.IO.File.AppendAllText(strLogFilePath, Environment.NewLine + strPageName + " : " + System.DateTime.Now.ToString() + " -- '" + strMsg + "'" + Environment.NewLine);
    }

GridView Sorting

protected void GridView_OnSorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dtSortTable = ViewState["CompanyData"] as DataTable;

        if (dtSortTable != null)
        {


            DataView dvSort = new DataView();
            dvSort = dtSortTable.DefaultView;
            //dtViewData is your data table - which in this case is loaded in teh function SearchControls
            string str = Convert.ToString(e.SortDirection);
            e.SortDirection = SortDirection.Ascending;

            ViewState.Add("SortCol", Convert.ToString(e.SortExpression));

            if (ViewState[e.SortExpression] != null)
            {
                if (Convert.ToString(ViewState[e.SortExpression]) == "Ascending")
                {
                    e.SortDirection = SortDirection.Descending;
                    ViewState[e.SortExpression] = "Desending";
                    dvSort.Sort = e.SortExpression + " DESC";
                }
                else
                {
                    e.SortDirection = SortDirection.Ascending;
                    ViewState[e.SortExpression] = "Ascending";
                    dvSort.Sort = e.SortExpression + " ASC";
                }
            }
            else
            {
                ViewState.Add(Convert.ToString(e.SortExpression), "Ascending");
                dvSort.Sort = e.SortExpression + " ASC";

            }

            GridView.DataSource = dvSort;
            GridView.DataBind();

        }
    }


Read Data From Excel to Asp.Net

Add this
 
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;


    public DataSet ExtractDataFromExcelFile(string strPathExcelFile)
    {
        OleDbConnection MyConnection = null;
        string ConStringOLEDB1 = "";
        string ConStringOLEDB2 = "";
        ConStringOLEDB1 = System.Configuration.ConfigurationManager.AppSettings["ConStringOLEDB1"];
        ConStringOLEDB2 = System.Configuration.ConfigurationManager.AppSettings["ConStringOLEDB2"];
        DataSet objDS = new DataSet();
        object missing = System.Reflection.Missing.Value;
        try
        {
           
            MyConnection = new OleDbConnection(ConStringOLEDB1 + strPathExcelFile + ConStringOLEDB2);
            MyConnection.Open();
            DataTable objDT = new DataTable();
            objDT = MyConnection.GetSchema("Tables");
            DataRow dr = objDT.Rows[0];
            string strWorksheetName = dr["TABLE_NAME"].ToString();
            System.Data.OleDb.OleDbDataAdapter objDataAdpter = null; ;
            objDataAdpter = new System.Data.OleDb.OleDbDataAdapter("select * from [" + strWorksheetName + "]", MyConnection);
            objDataAdpter.Fill(objDS);
            MyConnection.Close();
           
        }
        catch (Exception Ex)
        {
            MyConnection.Close();
           
        }
        return objDS;
    }


//Set Web Config File


<add key="ConStringOLEDB1" value="provider=Microsoft.Jet.OLEDB.4.0; data source='"/>
      <add key="ConStringOLEDB2" value="';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"/>
     
      <add key="ConStringOLEDB1_2003"  value="Provider=Microsoft.ACE.OLEDB.12.0; data source= '"/>
      <add key="ConStringOLEDB2_2003" value="';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"/>