1

when I click the edit button. editable data will be passing into relevant textboxes for editing. but data is not displaying. what i tried so far i attached below.

Response.Write(JsonConvert.SerializeObject(employees)); an object reference required for non-static field

this is a button

  {
     "sTitle": "Edit",
     "mData": "id",
     "render": function (mData, type, row, meta) {
      return '<button class="btn btn-xs btn-success" 
      onclick="get_category_details(' + mData + ')">Edit</button>';
   }

if i click the edit button id will be successfully passing to get_category_details method.then post to the edit_return.aspx/doSome page but data is not retrieved.i think get the error Edit_retun.aspx.

 function get_category_details(id) {
            $.ajax({
                type: 'POST',
                url: 'edit_return.aspx/doSome',
                dataType: 'JSON',
                data: "{id: '" + id + "'}",
                contentType: "application/json; charset=utf-8",
                success: function (data) {                            

                    $("html, body").animate({ scrollTop: 0 }, "slow");
                    isNew = false
                    id = data.id     
                    $('#fname').val(data.fname);
                    $('#age').val(data.age);               

                },
                error: function (xhr, status, error) {
                    alert(xhr.responseText);            
                }

            });
        }

Edit_retun.aspx

public class Employee
        {

            public string id { get; set; }
            public string fname { get; set; }
            public string age { get; set; }
        }

 [WebMethod]
            public static string doSome(int id)
            {
                SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
                string sql = "select * from record where id='" + id + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                List<Employee> employees = new List<Employee>();

                employees = dt.AsEnumerable()
                        .Select(x => new Employee()
                        {
                            id = x.Field<int>("id").ToString(),
                            fname = x.Field<string>("name"),
                            age = x.Field<int>("age").ToString(),
                        }).ToList();
                Response.Write(JsonConvert.SerializeObject(employees));
                return JsonConvert.SerializeObject(employees);
            }

HTML FORM

  <form  id="frmProject" runat="server">
            <div>
                <label class="form-label">First Name</label>     
               <input type="text" id="fname" name="fname" class="form-control"  required />

            </div>
            <div class="form-group" align="left">
             <label class="form-label">Age</label>
                <input type="text" id="age" name="age" class="form-control"  required />
            </div>
            <div> 
               <input type="button" id="b1" value="add" class="form-control" onclick="addProject()" />

            </div      
        </form>
17
  • add this to method top [ScriptMethod(ResponseFormat = ResponseFormat.Json)] Commented Dec 26, 2018 at 5:30
  • ScriptMethod namespace error sir Commented Dec 26, 2018 at 5:33
  • Response.Write(JsonConvert.SerializeObject(employees)); Response keyword getting error sir an object reference required for non-static field. how to write in proper way if you write it . it is more helpful for me. Commented Dec 26, 2018 at 5:35
  • Why are you using Response.Write when you are returning the JSON response? Namespce for ScriptMethod is System.Web.Script.Services. Commented Dec 26, 2018 at 5:37
  • 1
    Can you please try alert (data.d); in success part of the ajax. Commented Dec 26, 2018 at 5:47

2 Answers 2

1

You can not use Response.Write in a static method. Your doSome method need to be a non-static method. Or you can remove Response.write from your method as it is not needed here i guess.

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

1 Comment

thanks sir i can get data in to ajex function i couldn't transform in to the relavent textboxs can you check the ajex success function above code
0
 [WebMethod]
    public List<Employee> doSome(int id)
    {
        SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
        string sql = "select * from record where id='" + id + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        con.Open();
        cmd.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        List<Employee> employees = new List<Employee>();

        employees = dt.AsEnumerable()
                .Select(x => new Employee()
                {
                    id = x.Field<int>("id").ToString(),
                    fname = x.Field<string>("name"),
                    age = x.Field<int>("age").ToString(),
                }).ToList();
        return employees;
    }

Your List<Employee> will be serialize automatically no need to serialize.
In ajax success 
  $.ajax({

            success: function(data){
          data=data.d;
     // now data is JavaScript array of employee object 
},

        });

7 Comments

check full code. i think you are unable to get response from web method , i have solve the return of object type from web service and catching it in ajax success
console.log(data); i didn't retrieve any data sir ajax succes
unknown web method dosome parameter name method name error display when i click edit button
in $.ajax() call only success param is in my code. in your code replace success method only by my success code and in webservice file check // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService]
your service file should be edit_return.asmx webservice file but you are using edit_return.aspx which is wrong check once in $.ajax({ url: 'edit_return.aspx/doSome',)
|

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.