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');
        }
    });
};

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Its never pass in jquery statement, it shows undefined
Are you reaching the method and success result (if the success reached, data should contain JSON string)? Which part that returns undefined?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.