So, I have some data in my database and I want to display them in a Line chart. I am getting 2 errors when using 2 different approaches. Please refer below.
Am stuck, please help.
In my database I have data such as
| month A | total_collected_amount B | Admincut |
|---|---|---|
| November | 78.6000 | 2541.4000 |
| October | 24.0000 | 776.0000 |
| September | 2522.4000 | 81557.6000 |
And the SQL is
SELECT
DATENAME(MONTH, pay_date) AS month,
SUM(pay_admin_fee_mur) AS total_collected_amount,
SUM(pay_amount_mur) AS Admincut
FROM
tbl_payment
WHERE
tbl_payment.cha_id IS NOT NULL
AND
FORMAT(pay_date,'yyyy') = '2023'
GROUP BY
DATENAME(MONTH, pay_date), YEAR(pay_date)
ORDER BY
month;
My ASPX page is
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
<ajaxToolkit:LineChart ID="LineChart1" runat="server" BackColor="64, 0, 0"
BackGradientStyle="LeftRight" Height="550px" Palette="None"
PaletteCustomColors="192, 0, 0" Width="550px" ChartType="Basic" BaseLineColor="black">
<Series>
<ajaxToolkit:LineChartSeries LineColor="Darkblue" Name="Amount collected" Data=""/>
<ajaxToolkit:LineChartSeries LineColor="Maroon" Name="Admin cut" Data=""/>
</Series>
</ajaxToolkit:LineChart>
</asp:Content>
The Code-behind: Try 1 with error.
public partial class charityeventanalysis : System.Web.UI.Page
{
private string _conString = WebConfigurationManager.ConnectionStrings["FARMSdb"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Fetch data from the database
using (SqlConnection connection = new SqlConnection(_conString))
{
string query = "SELECT DATENAME(MONTH, pay_date) AS month, SUM(pay_admin_fee_mur) AS total_collected_amount, SUM(pay_amount_mur) AS Admincut FROM tbl_payment WHERE tbl_payment.cha_id IS NOT NULL AND FORMAT(pay_date,'yyyy') = '2023' GROUP BY DATENAME(MONTH, pay_date), YEAR(pay_date) ORDER BY month;";
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string month = reader["month"].ToString();
decimal totalCollectedAmount = Convert.ToDecimal(reader["total_collected_amount"]);
decimal adminCut = Convert.ToDecimal(reader["Admincut"]);
// Add data to the LineChart series ******* ERROR HERE *******
LineChart1.Series[0].Points.AddXY(month, totalCollectedAmount);
LineChart1.Series[1].Points.AddXY(month, adminCut);
}
}
}
}
LineChart1.ChartTitle = "Yearly collection amount MUR (Rs)";
}
}
}
Error underline for both at Points: saying "LineChartSeries does not contain a definition for Points and no accessible extension method Points accepting a first argument of type LineChartSeries could be found (are you missing a using directive or an assemble reference?)"
The Code-behind: Try 2 with error.
if (!IsPostBack)
{
// Fetch data from the database
using (SqlConnection connection = new SqlConnection(_conString))
{
string query = "SELECT DATENAME(MONTH, pay_date) AS month, SUM(pay_admin_fee_mur) AS total_collected_amount, SUM(pay_amount_mur) AS Admincut FROM tbl_payment WHERE tbl_payment.cha_id IS NOT NULL AND FORMAT(pay_date,'yyyy') = '2023' GROUP BY DATENAME(MONTH, pay_date), YEAR(pay_date) ORDER BY month;";
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string month = reader["month"].ToString();
decimal totalCollectedAmount = Convert.ToDecimal(reader["total_collected_amount"]);
decimal adminCut = Convert.ToDecimal(reader["Admincut"]);
// Add data to the LineChart series ******* ERROR HERE *******
LineChart1.Series[0].Data += totalCollectedAmount + ",";
LineChart1.Series[1].Data += adminCut + ",";
}
}
}
}
// Remove the trailing comma from the Data property ******* ERROR HERE *******
LineChart1.Series[0].Data = LineChart1.Series[0].Data.TrimEnd(',');
LineChart1.Series[1].Data = LineChart1.Series[1].Data.TrimEnd(',');
LineChart1.ChartTitle = "Yearly collection amount MUR (Rs)";
}
Error saying: Error "cannot implicitly convert string to decimal for LineChart1.Series[0].Data += totalCollectedAmount + ","; LineChart1.Series1.Data += adminCut + ",";"
AND error:
"decimal[] does not contain a definition for TrimEnd and no accessible extension method TrimEnd accepting a first argument of type decimal[] could be found(are you missing a using directive or an assemble reference?)"


