AdminHome.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/AdminMasterPage.master" AutoEventWireup="true" CodeFile="AdminHome.aspx.cs" Inherits="AdminHome" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<br />
<br />
<br />
<br />
<br />
<table style="width: 100%; height:203px" >
<tr>
<td style="border-style: solid; border-color: #800080; width: 342px; font-family: 'Britannic Bold'; font-size: large; font-weight: bold; color: #000000;" class="center1">
<br />
<asp:LinkButton ID="LinkButton1" runat="server" ForeColor="#993399">PESANAN</asp:LinkButton>
<br />
<asp:TextBox ID="txtOrders" runat="server" BorderStyle="None" CssClass="center" Font-Bold="True" Font-Size="X-Large" ></asp:TextBox>
</td>
<td style="border-style: solid; border-color: #800080; width: 342px; font-family: 'Britannic Bold'; font-size: large; font-weight: bold; color: #000000;" class="center1">
<br />
<asp:LinkButton ID="LinkButton2" runat="server" ForeColor="#993399">PELANGGAN</asp:LinkButton>
<br />
<asp:TextBox ID="txtUsers" runat="server" BorderStyle="None" CssClass="center" Font-Bold="True" Font-Size="X-Large"></asp:TextBox>
</td>
<td style="border-style: solid; border-color: #800080; width: 342px; font-family: 'Britannic Bold'; font-size: large; font-weight: bold; color: #000000;" class="center1">
<br />
<asp:LinkButton ID="LinkButton3" runat="server" ForeColor="#993399">PRODUK KECANTIKAN</asp:LinkButton>
<br />
<asp:TextBox ID="txtKecantikan" runat="server" BorderStyle="None" CssClass="center" Font-Bold="True" Font-Size="X-Large"></asp:TextBox>
</td>
<td style="border-style: solid; border-color: #800080; width: 342px; font-family: 'Britannic Bold'; font-size: large; font-weight: bold; color: #000000;" class="center1">
<br />
<asp:LinkButton ID="LinkButton4" runat="server" ForeColor="#993399">PRODUK KESIHATAN</asp:LinkButton>
<br />
<asp:TextBox ID="txtKesihatan" runat="server" BorderStyle="None" CssClass="center" Font-Bold="True" Font-Size="X-Large"></asp:TextBox>
</td>
</tr>
</table>
<br />
<br />
<br />
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
<title>jQuery Google Pie Chart Example in asp.net</title>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('visualization', '1', { packages: ['corechart'] });
</script>
<script type="text/javascript">
$(function () {
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'GoogleChart.aspx/GetChartData',
data: '{}',
success:
function (response) {
drawchart(response.d);
},
error: function () {
alert("Error loading data! Please try again.");
}
});
})
function drawchart(dataValues) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Column Name');
data.addColumn('number', 'Column Value');
for (var i = 0; i < dataValues.length; i++) {
data.addRow([dataValues[i].Countryname, dataValues[i].Total]);
}
new google.visualization.PieChart(document.getElementById('chartdiv')).
draw(data, { title: "Show Google Chart in Asp.net" });
}
</script>
</head>
<body>
<div id="chartdiv" style="width: 600px; height: 350px;">
</div>
</body>
</html>
</asp:Content>
AdminHome.aspx.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using System.Web.Services;
public partial class AdminHome : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=db;Integrated Security=true");
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Display();
}
}
private void Display()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT count(Name) as Name FROM tblUsers WHERE NOT Uid=1";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
txtUsers.Text = dr["Name"].ToString();
}
con.Close();
con.Open();
SqlCommand cmd2 = con.CreateCommand();
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = "SELECT count(OrderID) as OrderID FROM tblOrderProducts";
cmd2.ExecuteNonQuery();
DataTable dt2 = new DataTable();
SqlDataAdapter da2 = new SqlDataAdapter();
da2.SelectCommand = cmd2;
da2.Fill(dt2);
foreach (DataRow dr in dt2.Rows)
{
txtOrders.Text = dr["OrderID"].ToString();
}
con.Close();
con.Open();
SqlCommand cmd3 = con.CreateCommand();
cmd3.CommandType = CommandType.Text;
cmd3.CommandText = "SELECT count(PID) as PID FROM tblProducts WHERE PCategoryID=1";
cmd3.ExecuteNonQuery();
DataTable dt3 = new DataTable();
SqlDataAdapter da3 = new SqlDataAdapter();
da3.SelectCommand = cmd3;
da3.Fill(dt3);
foreach (DataRow dr in dt3.Rows)
{
txtKecantikan.Text = dr["PID"].ToString();
}
con.Close();
con.Open();
SqlCommand cmd4 = con.CreateCommand();
cmd4.CommandType = CommandType.Text;
cmd4.CommandText = "SELECT count(PID) as PID FROM tblProducts WHERE PCategoryID=8";
cmd4.ExecuteNonQuery();
DataTable dt4 = new DataTable();
SqlDataAdapter da4 = new SqlDataAdapter();
da4.SelectCommand = cmd4;
da4.Fill(dt4);
foreach (DataRow dr in dt4.Rows)
{
txtKesihatan.Text = dr["PID"].ToString();
}
con.Close();
}
[WebMethod]
public static List<countrydetails> GetChartData()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-R14HL5A\\SQLEXPRESS;Integrated Security=true;Initial Catalog=db"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select PName, PQuantity from tblProducts", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
List<countrydetails> dataList = new List<countrydetails>();
foreach (DataRow dtrow in dt.Rows)
{
countrydetails details = new countrydetails();
details.Countryname = dtrow[0].ToString();
details.Total = Convert.ToInt32(dtrow[1]);
dataList.Add(details);
}
return dataList;
}
}
public class countrydetails
{
public string Countryname { get; set; }
public int Total { get; set; }
}
What I have tried:
I have tried when I get rid the content in page load, the chart will give output. When I put the content back, the private void Display() only will execute. Why?