Hi akhter,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Chart ID="EmployeeChartInfo" runat="server" OnClick="EmployeeChartInfo_Click">
<Titles>
<asp:Title ShadowOffset="3" Name="Items" />
</Titles>
<Legends>
<asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
LegendStyle="Row" />
</Legends>
<Series>
<asp:Series Name="Default" />
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="1" />
</ChartAreas>
</asp:Chart>
Default
<table>
<tr>
<td>Name : </td>
<td><asp:Label ID="lblName" runat="server" /></td>
</tr>
<tr>
<td>Value : </td>
<td><asp:Label ID="lblValue" runat="server" /></td>
</tr>
</table>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetEmployeeChartInfo();
}
}
private void GetEmployeeChartInfo()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT shipcity as Sections, count(orderid) AS Total FROM orders where shipcountry = 'UK' group by shipcity", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
string[] x = new string[dt.Rows.Count];
int[] y = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
EmployeeChartInfo.Series[0].Points.DataBindXY(x, y);
EmployeeChartInfo.Series[0].ChartType = SeriesChartType.Pie;
EmployeeChartInfo.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
EmployeeChartInfo.Legends[0].Enabled = true;
EmployeeChartInfo.Series[0].ToolTip = "Total : #VALY";
EmployeeChartInfo.Series[0].PostBackValue = "#VALY-#VALX";
}
protected void EmployeeChartInfo_Click(object sender, ImageMapEventArgs e)
{
Response.Redirect("Default.aspx?Name=" + e.PostBackValue.Split('-')[1] + "&Value=" + e.PostBackValue.Split('-')[0]);
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblName.Text = Request.QueryString["Name"];
lblValue.Text = Request.QueryString["Value"];
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GetEmployeeChartInfo()
End If
End Sub
Private Sub GetEmployeeChartInfo()
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
con.Open()
Dim cmd As SqlCommand = New SqlCommand("SELECT shipcity as Sections, count(orderid) AS Total FROM orders where shipcountry = 'UK' group by shipcity", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
End Using
Dim x As String() = New String(dt.Rows.Count - 1) {}
Dim y As Integer() = New Integer(dt.Rows.Count - 1) {}
For i As Integer = 0 To dt.Rows.Count - 1
x(i) = dt.Rows(i)(0).ToString()
y(i) = Convert.ToInt32(dt.Rows(i)(1))
Next
EmployeeChartInfo.Series(0).Points.DataBindXY(x, y)
EmployeeChartInfo.Series(0).ChartType = SeriesChartType.Pie
EmployeeChartInfo.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
EmployeeChartInfo.Legends(0).Enabled = True
EmployeeChartInfo.Series(0).ToolTip = "Total : #VALY"
EmployeeChartInfo.Series(0).PostBackValue = "#VALY-#VALX"
End Sub
Protected Sub EmployeeChartInfo_Click(ByVal sender As Object, ByVal e As ImageMapEventArgs)
Response.Redirect("Default.aspx?Name=" & e.PostBackValue.Split("-"c)(1) & "&Value=" + e.PostBackValue.Split("-"c)(0))
End Sub
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
lblName.Text = Request.QueryString("Name")
lblValue.Text = Request.QueryString("Value")
End If
End Sub
Screenshot