A community member has associated this post with a similar question:
why chart not showing in my old asp.net application

Only moderators can edit this content.

chart that show data from sql to asp.net

RAVI 1,076 Reputation points
2023-10-30T08:08:58.17+00:00

Hello

This is my sql database table with data

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_1](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](50) NULL,
	[value] [int] NULL,
 CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Table_1] ON
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (1, N'aa', 10)
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (2, N'bb', 20)
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (3, N'cc', 30)
INSERT [dbo].[Table_1] ([ID], [name], [value]) VALUES (4, N'dd', 40)
SET IDENTITY_INSERT [dbo].[Table_1] OFF

I want to show this data in doughnut chart using asp.net old 2.0 version with c#

kindly let me know how to do so with one small example using my table data

example like this design

User's image

Microsoft 365 and Office Development Office JavaScript API
Developer technologies ASP.NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-10-31T03:39:46.14+00:00

    Hi @RAVI,

    Unfortunately, ASP.NET 2.0 has been unsupported. detail:ASP.NET Support Policy

    I think you'd better migrate your project to the latest version.

    How to: Configure an App to Support .NET Framework 4 or later versions

    It's best to create a new project with the latest version, as a lot of issues can arise with the migration.

    Then use the following code.

    You can use the ASP.Net Chart control to achieve the results you want.

    Below is an example of a donut chart based on your data.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="NewDemoWebForm.WebForm4" %>
    <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
               
                 <asp:Chart ID="Chart1" runat="server">            
                     <Legends>  
                        <asp:Legend Alignment="Center" Docking="Bottom" Name="Default"  
                            LegendStyle="Row" />  
                    </Legends>  
                    <Series>
                        <asp:Series Name="Default"  ></asp:Series>
                    </Series>
                    <ChartAreas>
                        <asp:ChartArea Name="ChartArea1" ></asp:ChartArea>
                    </ChartAreas>
                </asp:Chart>
           
            </div>
        </form>
    </body>
    </html>
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //get connection string from web config
        string constr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    
        using (SqlConnection con = new SqlConnection(constr))
        {
            //query to fetch Employee Salary and getting Count of each salary in table, the group by with Emp Salary
            using (SqlCommand cmd = new SqlCommand("SELECT name,value  FROM Table_1 "))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
    
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    DataTable ChartData = ds.Tables[0];
                    //storing total rows count to loop on each Record                          
                    string[] XPoints = new string[ChartData.Rows.Count];
                    int[] YPOints = new int[ChartData.Rows.Count];
                    for (int count = 0; count < ChartData.Rows.Count; count++)
                    {
                        // store values for X axis  
                        XPoints[count] = ChartData.Rows[count]["name"].ToString();                       
                        //store values for Y Axis  
                        YPOints[count] = Convert.ToInt32(ChartData.Rows[count]["value"]);                         
                    }
                    //binding chart control  
                    Chart1.Series[0].Points.DataBindXY(XPoints, YPOints);
                    Chart1.Series[0].Label = "#PERCENT{P0}";
                    Chart1.Series[0].BorderWidth = 2;
                    Chart1.Series[0].BorderColor = Color.White;
                    //setting Chart type   
                    Chart1.Series[0].ChartType = SeriesChartType.Doughnut;
                    //enable to show legend
                    Chart1.Series[0].LegendText = "#VALX";
                    Chart1.Legends[0].Enabled = true;                     
                    //show chart in 3d
                    Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;                                             
                }
            }
        }
    }
    

    User's image

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments