Simple Graph Chart Required For Asp.net c# MS SQL

RAVI 896 Reputation points
2024-03-28T05:39:53.7333333+00:00

Hello

This is my table data

 
/****** Object:  Table [dbo].[Table_2]    Script Date: 03/28/2024 11:06:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_2](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[JAN] [float] NULL,
	[FEB] [float] NULL,
	[MAR] [float] NULL,
	[APR] [float] NULL,
 CONSTRAINT [PK_Table1] 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 IDENTITY_INSERT [dbo].[Table_2] ON
INSERT [dbo].[Table_2] ([ID], [JAN], [FEB], [MAR], [APR]) VALUES (1, 10000, 5000, 15000, 30000)
SET IDENTITY_INSERT [dbo].[Table_2] OFF

I need to show simple graph like this below that will be free and easy to use in c# our aspx page is in old asp.net version

12345

Thanking you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,255 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,556 Reputation points Microsoft Vendor
    2024-04-02T06:55:28.61+00:00

    Hi @RAVI,

    You are using the code I provided and I tested it to work. I guess this might be a version issue. What version are you currently using?

    I retested a method, you can try it.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
            var chartData = <%= GetChartDataJson() %>;
            google.load("visualization", "1", { packages: ["corechart"] });
            google.setOnLoadCallback(drawChart);
            function drawChart() {          
                var options = {
                    title: 'Test',
                    width: 600,
                    height: 400,
                    bar: { groupWidth: "70%" },
                    legend: { position: "none" },
                    isStacked: true,
                };
                var data = google.visualization.arrayToDataTable(chartData);
                var chart = new google.visualization.ColumnChart($("#chart")[0]);
                chart.draw(data, options);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <div id="chart" style="width: 900px; height: 500px;">
                </div>
            </div>
        </form>
    </body>
    </html>
    
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    namespace WebApplication4
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            protected string GetChartDataJson()
            {
                List<object> chartData = GetChartData();
                return JsonConvert.SerializeObject(chartData);
            }
            public static List<object> GetChartData()
            {
                string query = "SELECT * from Table2_Month_Qty";
                string constr = ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString;
                List<object> chartData = new List<object>();
                chartData.Add(new object[]
             {
     "Month", "Quantity"
             });
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                chartData.Add(new object[]
                             {
                     sdr["Month"], sdr["Quantity"]
                             });
                            }
                        }
                        con.Close();
                        return chartData;
                    }
                }
            }
        }
    }
    

    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 additional answers

Sort by: Most helpful