0

I got this error in my coding. And my code is given below and i need to pass the value to jquery to display data. What is this Error "An object reference is required for the non-static field, method, or property 'System.Web.UI.Control.Context.get'" ?

[WebMethod]
public static string GetData()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["tempsConnectionString"].ConnectionString);
    List<Glassrecord> glass = new List<Glassrecord>();
    SqlCommand cmd = new SqlCommand("Select Prod_date,GreenTime,YellowTime  from CrystalProdTracker  ", con);
    con.Open();

    SqlDataReader sdr = cmd.ExecuteReader();

    while (sdr.Read())
    {
        Glassrecord glassrec = new Glassrecord();
        glassrec.Date = sdr["Prod_date"].ToString();
        glassrec.GreenTime = sdr["GreenTime"].ToString();
        glassrec.YellowTime = sdr["YellowTime"].ToString();

        glass.Add(glassrec);

    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    Context.Response.Write(js.Serialize(glass));
}

and i also attached my jquery code with this post.How to proceed further

$.ajax({
        url: "Add_Data.aspx/GetData",
        method: "post",
        datatype: "json",
        data: '',
        contentType: "application/json; charset=utf-8",

        success :function(data){
            $('$tblglass').dataTable(
                {
                    data:data,
                    columns: [
                        { 'data': 'Date' }, { 'data': 'GreenTime' }, { 'data': 'YellowTime' }
                    ]
                })
        },

        error: function (err) {
            alert('Object Not Found');
        }
    });
};
st4hoo
  • 2,196
  • 17
  • 25
Antony Raj
  • 71
  • 10

1 Answers1

1

The error occurred because GetData() web service method set as static, just simply set it as non-static one. Also you may need to decorate the method using [ScriptMethod(ResponseFormat = ResponseFormat.Json)] attribute and change return type to void (currently you're not return anything with return statement but return type sets as string) as in below example:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetData()
{
    List<Glassrecord> glass = new List<Glassrecord>();

    // database connection code here

    JavaScriptSerializer js = new JavaScriptSerializer();

    Context.Response.ContentType = "application/json";
    Context.Response.Write(js.Serialize(glass));
}

Related issue: How to get JSON response from a 3.5 asmx web service

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61