I am Writing a Windows service which will export reports to excel at user defined time. The User can choose to run report with following options:
- By Selecting StartTime and EndTime with DailyTime as the time to generate report
- Daily Report
- Weekly Report
- Monthly Report
- Yearly Report.
Note( My SQL Server Data is Updated every second & I have written a Windows Application From which user will configure the above, which will be saved to xml)
My windows service to configured to run at TimerInterval of 30000.
My problems are: 1. if User sets Start Time of 10:00 PM and End Time as 12:00 AM and dailytime as 12:00AM my below code fails as i am using system.datetime.now
DateTime tempDailyTime=DateTime.ParseExact(DailyTime,"HH:mm:ss tt",CultureInfo.InvariantCulture);
string curTime = System.DateTime.Now.ToString("HH:mm:ss tt");
DateTime tempCurrentTime = DateTime.ParseExact(curTime, "HH:mm:ss tt", CultureInfo.InvariantCulture);
if (tempCurrentTime >= tempDailyTime)
{
string tempFilename=Convert.ToString(tempDailyTime.TimeOfDay).Replace(":","-")+".xlsx";
if (!File.Exists(tempDir + "\\DailyTime" + "\\" + ReportName + "_" + tempFilename))
{
GenerateReportWithStartTimeandEndTime(ReportName,ReportID,ConnectionString,ReportColumnName,ReportBQuery,StartTime,EndTime,DailyTime,tempDir+"\\DailyTime",tempFilename);
}
}
For Generating Daily,Weekly,Monthly,Yearly I have specified the scheduled time to run the report as
DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59, 999);
after the timer elapse before reaching the below code part if datetime changes it will generate a blank report. My Daily Report Generation code is below:
if (Daily == "true")
{
DateTime currentTime = DateTime.Now;
int intervalToElapse = 0;
DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59, 999);
if (currentTime <= scheduleTime)
intervalToElapse = (int)scheduleTime.Subtract(currentTime).TotalSeconds;
else
intervalToElapse = (int)scheduleTime.AddDays(1).Subtract(currentTime).TotalSeconds;
_DailyTimer = new System.Timers.Timer(intervalToElapse);
if (_DailyTimer.Interval == 0)
{
//GenerateDailyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
}
}
if (Weekly == "true")
{
//GenerateWeeklyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
}
if (Monthly == "true")
{
//GenerateMonthlyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
}
if (Yearly == "true")
{
//GenerateYearlyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
}
The data to generate report is coming from xml File below:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAutoReportXML>
<AutoReportXML ReportName="oks" ReportID="64" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt FROM ab_Test t1 WHERE t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="18:00:00 PM" EndTime="22:00:00 PM" DailyTime="22:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
<AutoReportXML ReportName="ShiftC" ReportID="63" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt FROM ab_Test t1 WHERE t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="22:00:00 PM" EndTime="06:00:00 AM" DailyTime="18:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
<AutoReportXML ReportName="ShiftB" ReportID="62" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt FROM ab_Test t1 WHERE t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="14:00:00 PM" EndTime="22:00:00 PM" DailyTime="22:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
<AutoReportXML ReportName="shiftA" ReportID="61" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt FROM ab_Test t1 WHERE t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="06:00:00 AM" EndTime="14:00:00 PM" DailyTime="14:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
<AutoReportXML ReportName="testreport1" ReportID="59" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt FROM sampletest t0 WHERE t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="17:00:00 PM" EndTime="11:59:59 PM" DailyTime="00:01:00 AM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
<AutoReportXML ReportName="testreport" ReportID="58" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt FROM sampletest t0 WHERE t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="09:00:00 AM" EndTime="17:00:00 PM" DailyTime="17:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
<AutoReportXML ReportName="somereport" ReportID="57" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt FROM sampletest t0 WHERE t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="02:00:00 AM" EndTime="05:37:47 AM" DailyTime="06:37:47 AM" Daily="True" Weekly="True" Monthly="True" Yearly="True" Loc="C:\Users\khasim\Desktop\testAutoGenerated">57</AutoReportXML>
</ArrayOfAutoReportXML>
I need suggestions how to handle this or what I am doing wrong.