1

i am creating the simple crud system using asp.net ajax JSON, i created the function get all to retrieve the values from the all_data.aspx page as type as JSON. but I couldn't retrieve the data.what i tried so far i added below

Table Design

 <table id="tbl-category" class="table table-responsive table-bordered" cellspacing="0" width="100%">              
                        <thead>
                        <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                        </tr>
                    </table>

ajex function

function get_all() {
                    $('#tbl-category').dataTable().fnDestroy();
                    $.ajax({
                        url: 'all_data.aspx',
                        type: "GET",
                        dataType: "JSON",
                        success: function (data) {
                            $('#tbl-category').dataTable({
                                "aaData": data,
                                "scrollX": true,
                                "aoColumns": [
                                    { "sTitle": "fname", "mData": "fname" },
                                   { "sTitle": "age", "mData": "age" },

                                    {
                                        "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>';
                                        }
                                    },
                                    {
                                        "sTitle": "Delete",
                                        "mData": "id",
                                        "render": function (mData, type, row, meta) {
                                            return '<button class="btn btn-xs btn-primary" onclick="RemoveCategory(' + mData + ')">Delete</button>';

                                        }
                                    }

                                ]

                            });

                    },

record table consists of first name, age columns only here how to set this column as JSON type I don't know do it please some help me to do this i attached below what I tired so far

**all_data.aspx*

    string sql = "select * from records";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter();
     da.Fill(dt);

    string sql = "{\"fname\":\"fname\",\"age\":\"age\"}";
    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(json);
    Response.End();
8
  • Make one class with those property and return from above method Commented Dec 24, 2018 at 5:22
  • can you write code for me sir it is more helpful Commented Dec 24, 2018 at 5:24
  • Here is one example, try and let me know! ->stackoverflow.com/questions/14623327/… Commented Dec 24, 2018 at 5:30
  • ok thanks sir can you write the code helpful for me thanks Commented Dec 24, 2018 at 5:34
  • public static string doSomething() { string sql = "select * from records"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); cmd.ExecuteNonQuery(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(); da.Fill(dt); string sql = "{\"fname\":\"fname\",\"age\":\"age\"}"; Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; return "hello"; } is it correct sir Commented Dec 24, 2018 at 5:44

1 Answer 1

1

Install using Newtonsoft.Json;. How to install Newtonsoft.Json

Create one class:

public class Employee
{
  public string fname {get; set;}
  public int age {get; set;}
}

In method:

public string GetEmployees()
{
   string sql = "select * from records";
   SqlCommand cmd = new SqlCommand(sql, con);
   con.Open();
   cmd.ExecuteNonQuery();
   DataTable dt = new DataTable();
   SqlDataAdapter da = new SqlDataAdapter();
   da.Fill(dt);
   List<Employee> employees = new List<Employee>();

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

   return JsonConvert.SerializeObject(employees);
}

If you are getting proper data from C# method then append data like:

$.ajax({
 type: "GET",
 url: "https://jsonplaceholder.typicode.com/todos/1",
 success: function(res) {
  $.each(res, function(i, data) {
   $("table.table").append("<tr><td>" + res.userId + "</td><td>" + res.title + "</td></tr>");
  })
 },
 error: function(xhr, status, errorThrown) {
  alert("An error occered, " + errorThrown);
 }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl-category" class="table table-responsive table-bordered" cellspacing="0" width="100%">
   <thead>
      <tr>
         <th>Id</th>
         <th>Name</th>
      </tr>
</table>

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

2 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
shall i send the project to you sir can you tell the email id you fix and send it back

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.