Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all im creating one application in there i want to show daily report should be download at specific time i written code also but it wont work
Actually what i want report should download automatically in time which is specified

What I have tried:

On form load i written
C#
 private void Form3_Load(object sender, EventArgs e)
        {
             if((DateTime.Now==DateTime.Today) &&(DateTime.Now.Hour==22) &&(DateTime.Now.Minute==00))
            {
                report_data();
            }
}

made report_data method
C#
private void report_data()
      {
          OleDbConnection cnn = new OleDbConnection();
          string sql = null;
          string data = null;
          int i = 0;
          int j = 0;

          Excel.Application xlApp;
          Excel.Workbook xlWorkBook;
          Excel.Worksheet xlWorkSheet;
          object misValue = System.Reflection.Missing.Value;

          xlApp = new Excel.Application();
          xlWorkBook = xlApp.Workbooks.Add(misValue);
          xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
          string connectionString = null;
          connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
          cnn.ConnectionString = connectionString;
          cnn.Open();
          sql = "SELECT User_name,Sales_count from LOGIN  order by User_ID desc";
          OleDbDataAdapter dscmd = new OleDbDataAdapter(sql, cnn);
          DataSet ds = new DataSet();
          dscmd.Fill(ds);
          cnn.Close();

          for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
          {

              for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
              {
                  xlWorkSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].Caption;
                  data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                  xlWorkSheet.Cells[i + 2, j + 1] = data;
              }
          }

          System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
          saveDlg.InitialDirectory = @"C:\";
          saveDlg.Filter = "Excel files (*.xls)|*.xls";
          saveDlg.FilterIndex = 0;
          saveDlg.RestoreDirectory = true;
          saveDlg.Title = "Export Excel File To";
          xlWorkBook.Close(true, misValue, misValue);
          //xlWorkSheet.Name = "Daily_report" + DateTime.Now.ToString("ddMMyyyHHmmss"
          MessageBox.Show("File Downloaded successfully...");
          xlApp.Quit();
          releaseObject(xlWorkSheet);
          releaseObject(xlWorkBook);
          releaseObject(xlApp);

      }
Posted
Updated 4-Aug-16 9:27am
Comments
Richard Deeming 3-Aug-16 9:26am    
Your code sample is incomplete - it never shows the SaveFileDialog, and it never saves the Excel workbook.

If you want to save the workbook to a predefined path without showing the dialog, then remove the SaveFileDialog code, and simply pass the predefined path to the Save method.
Maciej Los 4-Aug-16 10:56am    
Have you tried to use task scheduler for Windows OS? See: Using the Task Scheduler

1 solution

I'd suggest to create task for windows scheduler, which is able to run your application at any time, instead of checking the time in your application.

For further details, please see: Schtasks: Management Services[^]

So, change your app to run report_data subroutine and when the report is finished, the application finishes itself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900