Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send the chart via email

i tried this code but when i open the email chart.png has 0K and don't open.

C#
protected void Page_Load(object sender, EventArgs e)
  {

     SqlConnection conn = new SqlConnection(constr);
        conn.Open();
        string sql = "SELECT * from CareerIntrestTestChart where EmailId='mailid'";

        //SqlCommand cmd = new SqlCommand(sql, conn);
        //SqlDataAdapter mySQLadapter = new SqlDataAdapter(cmd);

        //Chart1.DataSource = cmd;

        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter mySQLadapter = new SqlDataAdapter(cmd);
        mySQLadapter.Fill(ds);
        Chart1.DataSource = ds;
       
        Chart1.Titles.Add("Career Intrest Chart");
        // set series members names for the X and Y values 
        Chart1.Series["Series1"].XValueMember = "ChOption";
        Chart1.Series["Series1"].YValueMembers = "ChValues";
       
        // data bind to the selected data source
        Chart1.DataBind();

       
        cmd.Dispose();


        
      
        MailMessage mail = new MailMessage();
        mail.To.Add("MailId");
        mail.From = new MailAddress("MailId");
        mail.Subject = "Chart Control Image";
        mail.IsBodyHtml = true;
        mail.Body = "Heello";
       
         MemoryStream s = new MemoryStream();
         Chart1.SaveImage(s);
       
        Attachment attach= new Attachment(s, "chart.png");
        mail.Attachments.Add(attach);
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.host.com";
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential
             ("SendMailId", "Pwd");
        smtp.EnableSsl = false;
        smtp.Send(mail);
}
Posted
Updated 25-Jul-13 2:19am
v2
Comments
ZurdoDev 25-Jul-13 8:19am    
What do you mean OK and don't open?
Thomas Daniels 25-Jul-13 8:24am    
Not 'OK', but '0K' (zero kilobytes).
ZurdoDev 25-Jul-13 8:29am    
Sorry. Likely because it can't find the file. As aspnet has pointed out give it the whole path to make sure.
Thomas Daniels 25-Jul-13 8:30am    
I don't think it's necessary to give the whole path, because the data comes from a MemoryStream (so there's actually no whole path). Please see my answer.
ZurdoDev 25-Jul-13 8:37am    
Good call.

1 solution

Hi,

After saving the image to the memory stream, set the position of the stream to zero:
C#
MemoryStream s = new MemoryStream();
Chart1.SaveImage(s, ChartImageFormat.Png);
s.Position = 0;

I also recommend to choose the PNG format when saving the chart as image (otherwise it's perhaps not saved as PNG).

Hope this helps.
 
Share this answer
 
Comments
Manojkushwaha 25-Jul-13 8:32am    
Thank you very much its work fine....
Thomas Daniels 25-Jul-13 8:33am    
You're welcome!
Member 13393974 18-Sep-17 0:05am    
its not working plzz send entire source page plzz

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