0

i have database of transaction and table name is LOg_header and it have columns one is Resp_code and 2nd is Date

my job is to select failure transaction 3 in a row. like '01' and alert email to email address that 3 transaction is failed in a row. and if there is no failure transaction all are successfull like '00' email alert too that all things going fine.

through C# console applicaiton.

here is my code i m stuck what to do kindly help.

namespace Email
{
    class Program
    {
        public static void email_send()
        {    
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("hh@hotmail.com");
            mail.To.Add("ushhh@hotmail.com");
            mail.Subject = "Test Mail - 1";
            mail.Body = "mail with attachment";

            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(@"C:/ProductId.txt");
            mail.Attachments.Add(attachment);

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("usm@.com", "*******");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

        }
        static void Main(string[] args)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MySwipe"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("select  TOP 3 [Resp_Code],JV_Date from dbo.log_jv_header order by JV_Date desc", connection))
                {
                    using (SqlDataReader dataReader = command.ExecuteReader())
                    {
                        if (!dataReader.HasRows)
                        {
                          //NO idea what to do..
                        }
                    }
                }
            }

            email_send();
            Console.WriteLine("Email Send to Operations Department.");
            Console.ReadLine();
        }
    }
}
Moumit
  • 8,314
  • 9
  • 55
  • 59
  • Send failure Rows as Html Table in mail Example : http://stackoverflow.com/questions/8628683/how-to-send-html-formated-email – Ahmed Galal Jan 13 '16 at 12:35

1 Answers1

-1
 static bool HasFaildSessions()
    {
        int counter = 0;
        bool result = false;
        string connectionString = ConfigurationManager.ConnectionStrings["MySwipe"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand("select  TOP 3 [Resp_Code],JV_Date from dbo.log_jv_header order by JV_Date desc", connection))
            {
                using (SqlDataReader dataReader = command.ExecuteReader())
                {


                    while (dataReader.Read())
                    {
                        if (string.Equals(dataReader.GetString(0), "01"))
                            counter++;
                    }
                }
            }
        }

        if (counter == 3)
            result = true;

        return result;
    }

    static void Main(string[] args)
    {
        While(true) //infinite loop
        {
        if (HasFaildSessions())
        {
            email_send();

            Console.WriteLine("Email Send to Operations Department.");
            Console.ReadLine();
        }
        Thread.Sleep(300000);
        }
    }
Max Kilovatiy
  • 798
  • 1
  • 11
  • 32
  • What does this do and how does this answer the question? Never mind that without a loop, counter will always be either 0 or 1 – Panagiotis Kanavos Jan 13 '16 at 12:58
  • if datareader has 3 rows with code '01' we are sending a message. Whats wrong with my answer? – Max Kilovatiy Jan 13 '16 at 13:15
  • Not with this code. You'd need a loop to increment the counter after reading each line. This code though doesn't loop, only tries to read results if there are *no* rows (and will result in a crash). So actually, this will either crash or not send anything – Panagiotis Kanavos Jan 13 '16 at 13:19
  • What do you think now? – Max Kilovatiy Jan 13 '16 at 13:32
  • how to add time delay of 5 minutes that every 5 minutes email alert goes to particular email id? that all things going fine or transaction failure? – user3564076 Jan 13 '16 at 13:51
  • email is not sending i have done debug but email function is not reading. if (HasFaildSessions()) after this console screen disappear. – user3564076 Jan 13 '16 at 13:59