Click here to Skip to main content
15,914,323 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys continuing to have problems with asp.net/c# web site where i' implemeting.
I have a database with fields (Id,Name,Price,Quantity) i want to sen an email to administrator with products id and name when the quantity of the product is less<5 with the following code i send an email when only the first product has low quantity if any other products has low quantity it didn't send anything what should i change in order to works properly?

Thnx in advance!!!!

-------------------------------------------------------
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select Id, quantity from products", con);
        int var = 0;
        string pro = "";
        con.Open();
        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string numquant = read["quantity"].ToString();
                string Id1;
                Id1 = read["Id"].ToString();
                Response.Write(numquant);
                pro ="You product with low quantity is :"+ Convert.ToString(Id1);
                GridView1.DataSource = read;
                GridView1.DataBind();
                var = Convert.ToInt32(numquant);
            }
           
            
            int quantity = var;
            if (quantity < 5)
            {

                        
                MailMessage mailObj = new MailMessage(
                "from", "to", "product low quantity",pro);

                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Host = "smtp.gmail.com";
                    SMTPServer.Port = 587;
                    SMTPServer.Credentials = new NetworkCredential("email", "password");
                    SMTPServer.EnableSsl = true;
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    // Label1.Text = ex.ToString();
                }
                read.Close();
                con.Close();
            }
        }
    }      
}

-----------------------------------------------------------------------
Posted
Updated 3-Oct-13 8:45am
v2

Change your code something like and try :

C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select Id, quantity from products", con);
        int var = 0;
        string pro = "";
        con.Open();
        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string numquant = read["quantity"].ToString();
                string Id1;
                Id1 = read["Id"].ToString();
                Response.Write(numquant);
                pro ="You product with low quantity is :"+ Convert.ToString(Id1);
                
                var = Convert.ToInt32(numquant);
                if (var < 5)      
                  sendmail(pro);
            }
           
            GridView1.DataSource = read;
            GridView1.DataBind();
            read.Close();
            con.Close();
        }
    } 

    private void sendmail(string msg)     
    {
                 MailMessage mailObj = new MailMessage(
                "from", "to", "product low quantity",msg);
 
                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Host = "smtp.gmail.com";
                    SMTPServer.Port = 587;
                    SMTPServer.Credentials = new NetworkCredential("email", "password");
                    SMTPServer.EnableSsl = true;
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    // Label1.Text = ex.ToString();
                }
    }
}
 
Share this answer
 
Comments
JasonTsoum77 4-Oct-13 15:15pm    
It worked!!! Thank you very much for your help
Hey there,

If I remember correctly, I gave you another solution to send email using DataKeyNames.

Anyway, try moving the code that sends email inside the while loop.

Let me know if it works.

Azee...
 
Share this answer
 
v2
Comments
JasonTsoum77 3-Oct-13 15:11pm    
Hi Azee thank you for your response and your help
I remove (while loop) but now it didn't send nothing!
what else i shooul try?

thank you again for your help!
Azee 3-Oct-13 15:18pm    
Hey, dont remove the while loop, just move the email code:
int quantity = var;
if (quantity < 5)
{


MailMessage mailObj = new MailMessage(
"from", "to", "product low quantity",pro);

SmtpClient SMTPServer = new SmtpClient("localhost");
try
{
SMTPServer.UseDefaultCredentials = false;
SMTPServer.Host = "smtp.gmail.com";
SMTPServer.Port = 587;
SMTPServer.Credentials = new NetworkCredential("email", "password");
SMTPServer.EnableSsl = true;
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
// Label1.Text = ex.ToString();
}
}
Inside the loop.

and keep these two lines outside the loop.
read.Close();
con.Close();
JasonTsoum77 4-Oct-13 15:16pm    
It worked thank you very much for your help and your response!!! Azee
Hi,

Please check this.

Sending low product quantity details in a single mail
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select Id from products where quantity<5", con);
        int var = 0;
        string pro = "You product with low quantity are :";
        con.Open();
        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string Id1;
                Id1 = read["Id"].ToString();
                pro += Convert.ToString(Id1)+", ";                
            }
                pro = pro.Substring(0,pro.Length - 2);       
                MailMessage mailObj = new MailMessage(
                "from", "to", "product low quantity",pro);
 
                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Host = "smtp.gmail.com";
                    SMTPServer.Port = 587;
                    SMTPServer.Credentials = new NetworkCredential("email", "password");
                    SMTPServer.EnableSsl = true;
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    // Label1.Text = ex.ToString();
                }
                read.Close();
                con.Close();
        }
    }      
}
 
Share this answer
 
v3

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