Click here to Skip to main content
12,999,417 members (84,377 online)
Rate this:
Please Sign up or sign in to vote.
I am create one application that application one file send to user at specific time and specific day export sql data into excel ,sending email that functions working fine but problems is that it cannot execute particular time and particular day I am paste here my code please help me out

private void Form1_Load(object sender, EventArgs e)
            if ((DateTime.Now.DayOfWeek== DayOfWeek.Saturday) &&(DateTime.Now.Hour==19) &&(DateTime.Now.Minute==15))

<pre>public void exportsqldatatoexcle()
            string con1 = "Data Source=ADMIN\\SQLEXPRESS;Initial Catalog=PhysioCure; Integrated Security=true";
            SqlConnection connection = new SqlConnection(con1);
            sda = new SqlDataAdapter("select PationName,RegistrationDate,ContactNo,Age,Sex,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable order by RegistrationDate desc", con1);
            dt = new DataTable();
            DataColumnCollection dccollection = dt.Columns;
            Microsoft.Office.Interop.Excel.ApplicationClass excelapp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            for (int i = 1; i < dt.Rows.Count + 1; i++)
                for (int j = 1; j < dt.Columns.Count + 1; j++)
                    if (i == 1)
                        excelapp.Cells[i, j] = dccollection[j - 1].ToString();
                        excelapp.Cells[i, j] = dt.Rows[i - 1][j - 1].ToString();
            excelapp.ActiveWorkbook.SaveCopyAs("E:\\Winform n console\\PhysioCure\\PhysioCure\\Patient_Details.xls");
            excelapp.ActiveWorkbook.Saved = true;
            object m = Type.Missing;
            excelapp.ActiveWorkbook.Close(m, m, m);
            MessageBox.Show("Detail file successfully create!");
        private void emailattachment()
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("");
                mail.From = new MailAddress("my email id");
                mail.To.Add("my friend email id");
                mail.Subject = "Sending you Patient_Details File - 1";
                mail.Body = "Please Check Mail With Attachment dear..... ";
                System.Net.Mail.Attachment attachment;
                attachment = new Attachment("E:\\Winform n console\\PhysioCure\\PhysioCure\\Patient_Details.xls");
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("my email id", "password");
                SmtpServer.EnableSsl = true;
                MessageBox.Show("Details File send on your mail please check your mail");
            /*catch (Exception ex)
                MessageBox.Show("Mail cannot send please contact developer!!");

Posted 26-Jan-13 3:56am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Take the time checking out of your program and use Windows Task Scheduler to start your program at the desired time.
Atul Rokade 26-Jan-13 11:34am
mike sir im done that but not working in form load i done exctly what u told me but how to use windows task scheduler because after completing this application i'll install in client side so this time this window's task work fine?
Mike Meinz 26-Jan-13 11:46am
When you set it up on client's computer, you or the client starts the Task Scheduler and creates a task to run your program. That only needs to be one time after the program is installed.
Atul Rokade 26-Jan-13 12:00pm
ok mike sir .. but i done code time checking but it won't work for checking specific time and specific day private void Form1_Load(object sender, EventArgs e)

if ((DateTime.Now.DayOfWeek== DayOfWeek.Saturday) &&(DateTime.Now.Hour==19) &&(DateTime.Now.Minute==15))


Mike Meinz 26-Jan-13 12:14pm
Do this:
1. Remove the "if" statement
2. Use Task Scheduler to schedule the program to execute the program on the required day at the required time.
Atul Rokade 26-Jan-13 12:57pm
ok sir i'll try this
Mike Meinz 26-Jan-13 13:00pm
What I have advised you to do is best practice to help you learn. To hard code a day and time in a program to run a task at a specific time is unprofessional conduct.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 26 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100