Click here to Skip to main content
15,914,384 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have a list view (which is retrieving data from a database) it has columns (id,name,price,quantity)

I want to check the quantity value and if it is var<5 to send an e-mail with products characteristics (id,name,price) to administrator.

I found the following code which works it sends email:
C#
MailMessage mailObj = new MailMessage(
            "from", "to", "Product low quantity", "your product has low quantity");

        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();
        } 


so i want to check somehow the quantity of the product

Any help?

Thanks in advance!!

Jason
Posted
Updated 3-Oct-13 2:45am
v2
Comments
Azee 3-Oct-13 8:31am    
do you want to check quantity for each row of listview?
JasonTsoum77 3-Oct-13 8:59am    
yes
Tejas Vaishnav 3-Oct-13 8:46am    
have you tried any thing for checking the value of quantity?
JasonTsoum77 3-Oct-13 9:00am    
yes but how can i retrieve it to c# from asp
Tejas Vaishnav 3-Oct-13 9:03am    
can you share your code over your post, just use improve question option and add you code with formatted with code blocks

1 solution

Hey there,

Try this:
Add DataKeyNames property of ListView first, like: DataKeyNames="quantity"
and Use this code to check quantity:
C#
foreach (ListViewDataItem item in ListView1.Items)
            {
                int quantity = Convert.ToInt32(ListView1.DataKeys[item.DisplayIndex]["quantity"].ToString());
                if (quantity < 5)
                {
                    //Send Email here
                }
            }


You can get other values of a specific row similar to Quanity.

Hope it helps

Azee...
 
Share this answer
 
Comments
JasonTsoum77 3-Oct-13 11:25am    
well i try it but i receive null reference exception
here is my whole code

----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;


public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Btn_SendMail_Click(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
string asql = "SELECT quantity from products";
int val = Convert.ToInt32(Request.QueryString["quantity"]);
try
{
con.Open();
SqlCommand query = new SqlCommand(asql, con);

foreach (ListViewDataItem item in ListView1.Items)
{
int quantity = Convert.ToInt32(ListView1.DataKeys[item.DisplayIndex]["quantity"].ToString());
if (quantity < 5)
{
MailMessage mailObj = new MailMessage(
"from", "to", "Product low quantity", "your product has low quantity");

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();
}



}
}

}
finally
{
con.Close();
}//end of reduce quantity
}

}
----------------------------------------------------
JasonTsoum77 3-Oct-13 13:17pm    
it workded!!!! Thank you for your help
Azee 3-Oct-13 13:29pm    
Great, cheers :)

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