asp net generic handler return json

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Generic Handler Code

namespace Demo
{
public class EmployeeDataHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int displayLength = int.Parse(context.Request["iDisplayLength"]);
int displayStart = int.Parse(context.Request["iDisplayStart"]);
int sortCol = int.Parse(context.Request["iSortCol_0"]);
string sortDir = context.Request["sSortDir_0"];
string search = context.Request["sSearch"];

int filteredRows = 0;

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
List[Employee] employees = new List[Employee]();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter paramDisplayLength = new SqlParameter()
{
Value = displayLength
};
cmd.Parameters.Add(paramDisplayLength);

SqlParameter paramDisplayStart = new SqlParameter()
{
Value = displayStart
};
cmd.Parameters.Add(paramDisplayStart);

SqlParameter paramSortCol = new SqlParameter()
{
Value = sortCol
};
cmd.Parameters.Add(paramSortCol);

SqlParameter paramSortDir = new SqlParameter()
{
Value = sortDir
};
cmd.Parameters.Add(paramSortDir);

SqlParameter paramSearchString = new SqlParameter()
{
Value = string.IsNullOrEmpty(search) ? null : search
};
cmd.Parameters.Add(paramSearchString);

con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
filteredRows = Convert.ToInt32(rdr["TotalCount"]);
Employee employee = new Employee();
employee.Id = Convert.ToInt32(rdr["Id"]);
employee.FirstName = rdr["FirstName"].ToString();
employee.LastName = rdr["LastName"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.JobTitle = rdr["JobTitle"].ToString();
employees.Add(employee);
}
}

var result = new
{
iTotalRecords = GetEmployeeTotalCount(),
iTotalDisplayRecords = filteredRows,
aaData = employees
};

JavaScriptSerializer js = new JavaScriptSerializer();
context.Response.Write(js.Serialize(result));
}

private int GetEmployeeTotalCount()
{
int totalEmployees = 0;
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select count(*) from tblEmployees", con);
con.Open();
totalEmployees = (int)cmd.ExecuteScalar();
}
return totalEmployees;
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

Notice that from the Request object we are retrieving the sorting, paging and search parameter values. These will be sent by the jQuery datatables plugin to the server.
iDisplayStart
iDisplayLength
iSortCol_0
sSortDir_0
sSearch

With the above data the server server should return a JSON object, with the following parameters.
iTotalRecords
iTotalDisplayRecords
aaData
Рекомендации по теме
Комментарии
Автор

This is awesome, thanks for making these! I was getting really frustrated implementing server side datatables in ASP and this is the best explanation I've found.

nccsa
Автор

Oh god, you saved my day mate !

Greetings from France !

lordtau
Автор

Videos 109, 110, 111 and 112 are excellent. Thanks a lot. Appreciate if you add another video number 114 explaining how to implement customer column search with server side jquery datatable. This is will be perfect.

imadabab
Автор

Wonderful explanation and debatable binding and pagination logic you told to see in the next video, I want to see that video also. So please ping me the link

positivethinkingdream
Автор

It is Very useful sir.
In which scenario, we have to go for HTTP Generic Handler and WCF web service to return data as json or xml.

pascamary
Автор

Great video, I would like to know when we should use this approach vs web service, vs wcf vs web api. There are so many ways of doing the same thing and its hard to know which path to go down.

gt
Автор

Can you show us an example if we have large number of records in the database how the jquery datatable works say for example
25, 000 or more records.

sanujajameela
Автор

Sir, can we use Webservices instead of asp.net Generic Handler for retreiving JSON data in video Part110?

kurakulaprasad
Автор

If I add "&sSearch=Male" at the end, I get 14 employees?

josephregallis
Автор

If u Put Code of Store Procedure its will be complete reff

shajeealikhan
welcome to shbcf.ru