Tuesday, January 27, 2015

All date related solution

All date related solution
  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetnextSunday();

                for (int i = 1; i <= 31; i++)
                {
                    DateTime start = new DateTime(2015, 1, i);
                    DateTime stop = new DateTime(2015, 1, 31);

                    int totalWorkingDays = GetNumberOfWorkingDays(start, stop);

                    Console.WriteLine("There are {1} working days from Oct {0}, 2014 to Oct 31, 2014.", i, totalWorkingDays);
                }
                DateTime start1 = new DateTime(2015, 1, 1);
                DateTime stop1 = new DateTime(2015, 1, 31);
                int cntcoun = CountDays(start1, stop1);
                GetAllSundays(start1, stop1);
            }
        }
        public void GetnextSunday()
        {
            var date = DateTime.Now;
            var nextSunday1 = date.AddDays(7 - (int)date.DayOfWeek);

            DateTime d = DateTime.Today;

            int offset = d.DayOfWeek - DayOfWeek.Monday;

            DateTime lastMonday = d.AddDays(-offset);
            DateTime nextSunday = lastMonday.AddDays(6);
        }
        private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
        {
            TimeSpan interval = stop - start;

            int totalWeek = interval.Days / 7;
            int totalWorkingDays = 5 * totalWeek;

            int remainingDays = interval.Days % 7;


            for (int i = 1; i <= remainingDays; i++)
            {
                DayOfWeek test = (DayOfWeek)(((int)start.DayOfWeek) % 7);
                if (test >= DayOfWeek.Monday && test <= DayOfWeek.Friday)
                    totalWorkingDays++;
            }

            return totalWorkingDays;
        }

        public int CountDays(DateTime fromDate, DateTime toDate)
        {
            int noOfDays = 0;
            DateTime fDate = Convert.ToDateTime(fromDate);
            DateTime tDate = Convert.ToDateTime(toDate);
            while (DateTime.Compare(fDate, tDate) <= 0)
            {
                if (fDate.DayOfWeek != DayOfWeek.Saturday && fDate.DayOfWeek != DayOfWeek.Sunday)
                {
                    noOfDays += 1;
                }
                fDate = fDate.AddDays(1);
            }
            return noOfDays;
        }
        public void GetAllSundays(DateTime Date1, DateTime Date2)
        {
            TimeSpan DateDiff = Date2.Subtract(Date1);
            List<DateTime> lstdate = new List<DateTime>();
            for (int i = 0; i <= DateDiff.Days; i++)
            {
                if (Date1.Date.AddDays(i).DayOfWeek == DayOfWeek.Sunday)
                {
                    lstdate.Add(Date1.Date.AddDays(i));
                }
            }
        }

Read more »

how to do sorting on gridview


 private void BindGrid()
        {
            if (ViewState["SortExp"] != null && ViewState["order"] != null)
            {
                lstemp.Sort(new GenericComparer<Employee>(ViewState["SortExp"].ToString(), (SortDirection)ViewState["order"]));
            }
            GridView1.DataSource = lstemp;
            GridView1.DataBind();

        }
        SortDirection sortOrder;
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (ViewState["SortExp"] != null && ViewState["SortExp"].ToString().ToLower() == e.SortExpression.ToString().ToLower()
                        && ViewState["order"] != null && (SortDirection)ViewState["order"] == SortDirection.Ascending)
                {
                    sortOrder = SortDirection.Descending;
                }
                else
                {
                    sortOrder = SortDirection.Ascending;
                }

                ViewState["order"] = sortOrder;
                ViewState["SortExp"] = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
               
            }
        }

create new class

public class GenericComparer<T> : IComparer<T>
    {
        /// <summary>
        /// Define sort direction Ascending or descending
        /// </summary>
        private SortDirection sortDirection;

        /// <summary>
        /// Gets or sets the sort direction.
        /// </summary>
        /// <value>The sort direction.</value>
        /// <remarks></remarks>
        public SortDirection SortDirection
        {
            get { return this.sortDirection; }
            set { this.sortDirection = value; }
        }

        /// <summary>
        /// Define the name in which data is to be sort.
        /// </summary>
        private string sortExpression;

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericComparer&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="sortExpression">The sort expression.</param>
        /// <param name="sortDirection">The sort direction.</param>
        /// <remarks></remarks>
        public GenericComparer(string sortExpression, SortDirection sortDirection)
        {
            this.sortExpression = sortExpression;
            this.sortDirection = sortDirection;
        }
        public int Compare(T x, T y)
        {
            PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);
            IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
            IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);

            if (obj1 == null)
            {
                if (SortDirection == SortDirection.Ascending)
                {
                    return (obj2 == null) ? 0 : -1;
                }
                else
                {
                    return (obj2 == null) ? -1 : 0;
                }
            }
            if (obj2 == null) { return 1; }

            if (SortDirection == SortDirection.Ascending)
            {
                return obj1.CompareTo(obj2);
            }
            else return obj2.CompareTo(obj1);
        }
     
    }

Read more »

Tuesday, June 26, 2012

Example on Windows Service Wothout Setup

Hi Friends,in this post i would like to explain windows service for displaying message box for every 3 seconds.

Step 1:

* Open windows service project with projectName WinService1
* Goto ToolBox-->General-->Right Click-->Select choose items-->Select Timer(Systems.Timers)
* Project-->AddReference-->System.Windows.Forms(Message Box is a part of above reference).
* Place a Timer(System.Timers) with interval-3000 & enabled-false.

Code:

{//timer1_Elapsed event.
System.Windows.Forms.MessageBox.Show("This message from Windows Service.");
}

Code for OnStart() event
{
timer1.Enabled=true;
}

Code for OnStop() event
{
timer1.Enabled=false;
}


Step 2:

* Open service1.cs[design]
Right Click
Add installer(Select).
Then 2 controls will be added to the form.
1)ServiceProcessInstaller-->Choose properties-->Account=LocalSystem.
2)ServiceInstaller-->Properties-->ServiceName=MessageBoxDisplay

* Build the project(Build-->Select Build solution)

Note:
* WinService1.exe is created under:
D:\WinService1\bin\debug folder with a service name called as "MessageBoxDisplay"

Step 3:

* open .Net Command prompt.
Start-->Programs-->MSVisualStudio2005-->VSTools-->.Net Command Prompt.
>installutil -i D:\WinService1\bin\debug\WinService1.exe (press enter)
>....
>....
>....
>transaction install has completed.

Step 4:

* Open service(Start-->run-->services.msc)
* MessageBoxDisplay-->RightClick-->Properties-->Logon-->Check "interact with desktop" checkBox-->OK
(Only service contains MessageBox otherwise above step is not required).
* MessageBoxDisplay-->rightClick-->Start.

Then service will be started & MessageBox with message(This message from Windows Service.) will be displayed for every 3 seconds.


Thank You...

Read more »

What Is Delegate with a simple example

aIntroduction
This article is an attempt to explain a delegate with a simple example. Delegates are similar to function pointers in C++. For simple understanding delegates can be defiend as methods that are used to call other method. Only condition to call

another method from a delegate is that the signature of the calling methods and delegates should match.
Follw This Step
Delegates should have the same signature as the methods to be called by them.
i.e. Say that we are calling an addNumbers method which returns an integer by taking (int, int) as input. Then our

delegate also must have been declared with the same signature.
Delegate Declaration:
public delegate int simpleDelegate (int a, int b);

Method:
public int addNumber(int a, int b)
Step For Create And Cal A Delegate
--Create a delegate. A delegate can be created with the delegate keyword, followed by it's return type, then name of the

delegate with the input patameters.

public delegate int simpleDelegate (int a, int b);

--Define the methods which are having similar signature as the delegate.

public int mulNumber(int a, int b)
public int addNumber(int a, int b)
--Now we are all set to use the delegates. Just instatiate the class and call the methods using the delegates.
Code Use
class clsDelegate
{
public delegate int simpleDelegate (int a, int b);
public int addNumber(int a, int b)
{
return (a+b);
}
public int mulNumber(int a, int b)
{
return (a*b);
}
static void Main(string[] args)
{
clsDelegate RAPatel = new clsDelegate();
simpleDelegate addDelegate = new simpleDelegate(RAPatel.addNumber);
simpleDelegate mulDelegate = new simpleDelegate(RAPatel.mulNumber);
int addAns = addDelegate(10,12);
int mulAns = mulDelegate(10,10);
Console.WriteLine("Result by calling the addNum method using a delegate: {0}",addAns);
Console.WriteLine("Result by calling the mulNum method using a delegate: {0}",mulAns);
Console.Read();
}
}

Read more »

How to Create Setup File And how to install,Start windows service

aaa
Introduction:

Here I will explain how to install windows service and how to start the windows service in our local machine.

Description:

In previous article I explained clearly how to Create Setup File and how to run windows service in scheduled intervals. Now I will explain how to install windows service in our system.

To install windows service in your follow these steps

Step-1
Create New Project- Select Setup- And Give Setup File Name…….


Step-2
First Bind Both Project..
And Install your Setup File..




Step-3
Open Services And Start Our ScheduledService Service……




Read more »

Monday, June 25, 2012

Moveable Telerik Rad window in WPF

Introduction :
How to Move Telerik Rad Window in WPF

Descritption :
You can move your rad window move any where in your page


Using Code :
In Design View :
Hide Expand


In Coding View :
Hide Expand


Read more »

Display message in your pc Notification Area in WPF


Introduction :
How To Display message in your pc Notification Area in WPF


Descritption :
You can set Custom icon and Custom Message in Your Pc Notification Area


Using Code :

This code for Notify Icon 
Hide Expand

This Code for Notify Icon Double 
Hide Expand

This Code for Balloon Icon Double click
Hide Expand

Read more »

Followers