Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
Chart1.DataSource = dt1; // here I am getting null reference exception


i am using jquery ajax on html button click it will invoke webmethod selectRecord()
JavaScript
$.ajax({
       type: "POST",
       url: "Default.aspx/selectRecord",
       data: '{}',
       contentType: "application/json;charset=utf-8",
       dataType: "json",
       success: OnSuccess,
       error: OnErrorCall
   });

ASP.NET
<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:

C#
    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();

        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();
            //_Default d1 = new _Default();
            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;  // here i am getting Null Reference Exception 
           
            Chart1.Series[0].ChartType = SeriesChartType.Column;

            Chart1.Series[0].XValueMember = "name";
            Chart1.Series[0].YValueMembers = "age";
            
            Chart1.DataBind();
        }
        catch (NullReferenceException e)
        {
            //eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported.", new TaxId(line.TaxId).Masked));
            //Response.Write(e.Message);
            Console.WriteLine(e.Message); 
        }

    }
}
Posted
Updated 11-Apr-17 1:02am
v5
Comments
CHill60 11-Apr-17 6:10am    
It looks as if your Stored Procedure selectage is not returning anything
Member 13120628 11-Apr-17 6:16am    
no its working fine i debug my code its able to fill DataTable dt1

Chart1.DataSource = dt1; // here I am getting null reference exception

any idea why?
CHill60 11-Apr-17 6:50am    
Use the debugger to see if either Chart1 or dt1 is null
Member 13120628 11-Apr-17 6:56am    
Chart1 is null why?
CHill60 11-Apr-17 7:00am    
To be honest, I don't know why. This code is being called from a button click apparently so the page should have been rendered. You might want to update your question with the information that Chart1 is null - someone else may be able to work out why

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900