Hi Tevin,
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="PieChartMonthToDate" BorderlineColor="Black" BorderlineDashStyle="Solid"
Visible="true" ImageType="Png" runat="server" Height="400px" Width="400px" PaletteCustomColors="128, 255, 128; 255, 128, 0">
<Titles>
<asp:Title TextStyle="Frame">
</asp:Title>
</Titles>
<Legends>
<asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
LegendStyle="Column">
</asp:Legend>
</Legends>
<Series>
<asp:Series Name="Series1" ChartType="Pie" YValuesPerPoint="2">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea IsSameFontSizeForAllAxes="true" BorderWidth="0" Name="ChartArea1">
<Area3DStyle Enable3D="true" />
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select shipcity, count(orderid) Total from orders where shipcountry = 'Brazil' group by shipcity", conn))
{
Series series = PieChartMonthToDate.Series["Series1"];
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
series.Points.AddXY(rdr["shipcity"].ToString(), rdr["Total".ToString()]);
PieChartMonthToDate.Series["Series1"].Label = "#PERCENT{P2}";
PieChartMonthToDate.Series["Series1"].LegendText = "#VALX";
PieChartMonthToDate.Legends[0].LegendStyle = LegendStyle.Column;
PieChartMonthToDate.Legends[0].Docking = Docking.Right;
PieChartMonthToDate.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
}
rdr.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
Using cmd As New SqlCommand("select shipcity, count(orderid) Total from orders where shipcountry = 'USA' group by shipcity", conn)
Dim series As Series = PieChartMonthToDate.Series("Series1")
conn.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
series.Points.AddXY(rdr("shipcity").ToString(), rdr("Total".ToString()))
PieChartMonthToDate.Series("Series1").Label = "#PERCENT{P2}"
PieChartMonthToDate.Series("Series1").LegendText = "#VALX"
PieChartMonthToDate.Legends(0).LegendStyle = LegendStyle.Column
PieChartMonthToDate.Legends(0).Docking = Docking.Right
PieChartMonthToDate.Legends(0).Alignment = System.Drawing.StringAlignment.Center
End While
rdr.Close()
End Using
End Using
End If
End Sub
Screenshot