I am new to ms chart i am trying to build column chart form jquery(HighChart) and Mschart control using vs 2010 using same data
I am able to build column chart in jquery(high chart)
but getting Null reference exception on ms chart control
in my build_chart() function
Chart1.DataSource = dt1;
i am using jquery ajax on html button click it will invoke webmethod selectRecord()
$.ajax({
type: "POST",
url: "Default.aspx/selectRecord",
data: '{}',
contentType: "application/json;charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnErrorCall
});
<pre> <div>
<button id="btn">load chart</button>
</div>
<form id="form1" runat="server">
<div>
<h1>Microsoft Chart</h1>
<asp:Chart ID="Chart1" runat="server"><Series><asp:Series Name="Series1"></asp:Series></Series><ChartAreas><asp:ChartArea Name="ChartArea1"></asp:ChartArea></ChartAreas></asp:Chart>
</div>
<div>
<h1>jquery Chart</h1>
<div id="container1" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</div>
</form>
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Web.Services;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.UI.DataVisualization.Charting;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Chart1.Series["Series1"].ChartType = SeriesChartType.Column;
}
[WebMethod]
public static string selectRecord()
{
_Default d = new _Default();
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("Data Source=MYSERVER;Initial Catalog=Layout;User ID=admin123;Password=123");
conn.Open();
SqlCommand cmd = new SqlCommand("selectage", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
string query = "selectage";
sda.Fill(dt);
d.build_chart(query);
string result = DataTableToJsonWithJavaScriptSerializer(dt);
return result;
}
public static string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
public void build_chart(string query)
{
try
{
DataTable dt1 = new DataTable();
SqlConnection conn = new SqlConnection("Data Source=MYSERVER;Initial Catalog=Layout;User ID=admin;Password=123");
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt1);
Chart1.DataSource = dt1;
Chart1.Series[0].ChartType = SeriesChartType.Column;
Chart1.Series[0].XValueMember = "name";
Chart1.Series[0].YValueMembers = "age";
Chart1.DataBind();
}
catch (NullReferenceException e)
{
Console.WriteLine(e.Message);
}
}
}