Tuesday 12 September 2017

How to write Exceptions to Event Viewer in C#

1 - First Create Static Method (LogError)
public static class Log
{
  public static void LogError(Exception ex)
  {
     EventLog.WriteEntry("Application", ex.Message + " Trace" + ex.StackTrace, EventLogEntryType.Error, 121, short.MaxValue);
  }
}
2 - Then Call this Static Method inside Catch Block.
try
{
               
}
catch (Exception ex)
{
  Log.LogError(ex);
}

How to Calculate Days difference Between two Dates in C#

DateTime fromdt = dtpFromDate.Value.Date;

DateTime todt = dtpToDt.Value.Date;

int fday = fromdt.Day;

int fmonth = fromdt.Month;

int fyear = fromdt.Year;

DateTime oldDate = new DateTime(fyear, fmonth, fday);

TimeSpan ts = todt - oldDate;

int differenceInDays = ts.Days;

MessageBox.Show(differenceInDays.ToString());