I have an ASP chart nested inside a table and the chart is being generated based on the values available in the dropdown
I am able to export the contents in the table but the chart is not being exported.Can anyone help me out on this.
Please find the code below:
ASPX:
C Sharp Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = SQLConnection();
SqlCommand command = new SqlCommand("Select distinct color from [AdventureWorks2008R2].[Production].[Product]", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "color";
DropDownList1.DataValueField = "color";
DropDownList1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedvalue = DropDownList1.SelectedValue;
SqlConnection connection = SQLConnection();
SqlCommand command = new SqlCommand("SELECT [Name],[SafetyStockLevel],[ReorderPoint] FROM [AdventureWorks2008R2].[Production].[Product] where [Color] = '" + selectedvalue + "'", connection);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
Chart1.DataSource = ds;
Chart1.Series["Series1"].XValueMember = "Name";
Chart1.Series["Series1"].YValueMembers = "SafetyStockLevel";
//Chart1.Series["Series2"].XValueMember = "Name";
Chart1.Series["Series2"].YValueMembers = "ReorderPoint";
Chart1.DataBind();
connection.Close();
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (MemoryStream stream = new MemoryStream())
{
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75f);
pdfDoc.Add(chartImage);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.