0

I am trying to return a list of data along with the total number of records in a json format for extjs paging toolbar. extjs looks for "count" in order to calculate the total number of paging. This is the format I want.

{
  "count": 1,
  "listData": [
      {
          "LastName": "Beckham",
          "FirstName": "David"
      }
  ]
}

This is my method

    public static List<myList> newRecords(int start, int limit)
    {
        List<myList> listData = new List<myList>();
        using (SqlConnection con = SqlTools.GetSqlConnection())
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "id1_GetData";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@limit", SqlDbType.Int).Value = limit;
            cmd.Parameters.Add("@start", SqlDbType.Int).Value = start;

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                myList list1 = new myList();

                list1.LastName = reader["LastName"].ToString().Trim();
                list1.FirstName = reader["FirstName"].ToString().Trim();
                listData.Add(list1);
            }
            con.Close();
            int count = countRecords(); //counts total number of records in db
            var pagingData = (new
            {
                count,
                listData
            });

            //return myList;
            return new List<myList>(pagingData);
        }
    }

This way I get nothing... If I return myList, I only get the first page. please help

EagleFox
  • 1,367
  • 10
  • 34
  • 58
  • EagleFox are you are doing this king of wrong...what do you want to be in the list `LastName " " + FirstName`..? – MethodMan Mar 22 '13 at 20:08
  • 1
    How about returning `return new { count = count, listData = listData };`? I don't see how what you pasted above can compile. – Nikola Radosavljević Mar 22 '13 at 20:12
  • DJ KRAZE... I am return a whole bunch of data.. firstname and lastname is just sample... it's basically returning a JSON string – EagleFox Mar 22 '13 at 20:14
  • I got gotcha It would be nice to see what myList is defined as I was just trying to think of a way that you could a an array {} of new items to the list – MethodMan Mar 22 '13 at 20:22

1 Answers1

0

I think what you want to do is just do this:

return pagingData;

Then you can call a JSON Serializer to create the JSON string.

SBurris
  • 7,378
  • 5
  • 28
  • 36