Hi aakhan314,
In the appSettings ChartImageHandler value add deleteAfterServicing to false.
appSettings
<appSettings>
<add key="ChartImageHandler" value="storage=file;timeout=20;deleteAfterServicing=false"/>
</appSettings>
Using the below article i have created the example.
Populate Bar and Column Charts from database using ASP.Net Chart control
Refer the complete sample.
HTML
<div id="cChart">
<asp:Chart ID="grChart" runat="server" Height="300px" Width="400px">
<Series>
<asp:Series Name="Series1" Legend="Legend1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Name="Legend1">
</asp:Legend>
</Legends>
</asp:Chart>
</div>
<input type="button" value="Print" onclick="printChart()" style="width: 99px; height: 26px;" />
<script type="text/javascript">
function printChart() {
var html = '<HTML>\n<HEAD>\n';
html += '<link rel="stylesheet" type="text/css" href="print.css" media="print"> \n';
html += '\n</HEAD>\n<BODY>\n';
html += '\n<div>';
var printReadyElement = document.getElementById("cChart");
if (printReadyElement != null) {
html += printReadyElement.innerHTML;
} else {
alert("Trouble printing Chart");
return;
}
html += '\n</div>';
html += '\n</BODY>\n</HTML>';
var printWin = window.open("Yearly Comparison", "printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
printWin.print();
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "SELECT ShipCity, COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry = 'France' GROUP BY ShipCity";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
grChart.DataSource = dt;
grChart.Series[0].ChartType = SeriesChartType.Bar;
grChart.Legends[0].Enabled = false;
grChart.Series[0].XValueMember = "ShipCity";
grChart.Series[0].YValueMembers = "Total";
grChart.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim query As String = "SELECT ShipCity, COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry = 'France' GROUP BY ShipCity"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
grChart.DataSource = dt
grChart.Series(0).ChartType = SeriesChartType.Bar
grChart.Legends(0).Enabled = False
grChart.Series(0).XValueMember = "ShipCity"
grChart.Series(0).YValueMembers = "Total"
grChart.DataBind()
End Using
End Using
End Using
End If
End Sub
Web.config
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="ChartImageHandler" value="storage=file;timeout=20;deleteAfterServicing=false"/>
</appSettings>
<connectionStrings>
<add name="constr" connectionString="Data Source=.\SQL2019;database=Northwind;user id=sa;password=password"/>
</connectionStrings>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="ChartImageHandler"/>
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.8">
<assemblies>
<add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<httpHandlers>
<add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<pages controlRenderingCompatibilityVersion="4.0">
<controls>
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
</system.web>
</configuration>
Screenshots
The Form
The Print Window