JQGrid Get page level data at a time - c#

Currently i have implemented jqgrid which fetches data from DB and returning the JSON data to JQGRID.
JQGrid call
rowNum: 10,
rowList: [5, 10],
url: "/Home/GetDataFromEntity"
Data returned from C#
return Json(result, JsonRequestBehavior.AllowGet);
What am trying is if the page has 10 records only that 10 records i want to bring from DB and if they click to next page i want to get the next 10 data because if the data is huge i dont want to bring all data to memmory which i think will be a performance Hit.
How can i implement this ?
Thanks

This is fairly straightforward. Basically all you need to do is implement paging, where you are only asking the DB for the page of data that you are going to display. You will see jqGrid will provide this information to your controller so you can use then when retrieving data.
The controller will take this data in via something like (I don't know your back end tech stack so here is C# code) this:
public ActionResult GridDataFetch(string sidx, string sord, int page, int rows, bool _search, string filters)
{
....
Then when you go to retreive your data, you can ask the database for the page of data that your user wants without having to retreive the whole dataset. This can be more complicated then it seems but for the basics it is as simple as somthing like (again C# code)
var pagedQuery = dataset.OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows);
You can see above we order the data in the manner that the user specified and jqGrid passed along with sidx & sord and then we skip all the records before the page we are interested in via the skip, and then we take the rows we are interested in. This again was a C# method for grabbing a page of data but the basics should be there for any setup. As a side note if you do any filtering via the grid, or other logic you would have filtered your dataset prior to this call.
You would then pass this paged query same as you would usually do in JSON.

You need to use paging for the same. You can use .Take and .Skip methods at server side
After you get your resultset you can do as following
var smallResultSet = fullResultSet.Skip(request.PageIndex * request.RecordsCount).Take(request.RecordsCount).ToList();
here I've made assumption that you are fetching your resultset in fullResultSet variable than filter it and store it in smallResultSet. request parameter will be passed when you bind grid to controller action.
After that iterate through smallResultSet and create your JSONResult.

you have to implement server side pagination to achieve this.
I have implemented in java like this: (you are using c#)
int limit = Integer.parseInt(request.getParameter("rows")); // get how many rows we want to have into the grid
String sidx = request.getParameter("sidx"); // get index row - i.e. user click to sort
String sord = request.getParameter("sord"); // get the direction
int start = (limit* page) - limit;
String rows = request.getParameter("rows");
String query = "select * from ( select a.*, ROWNUM rnum from ( select * from CRM_PROT_STAGES where PROTOCOL_ID = '"+param +"' ) a where ROWNUM <= "+ limit +")where rnum >="+start;
use above paarameters, and then put the condition in query as above

Related

How to display millions of rows in a DataGridView? [duplicate]

I have a web application in which I get data from my database and show in a datatable. I am facing an issue doing this as the data that I am fetching has too many rows(200 000). So when I query something like select * from table_name;
my application gets stuck.
Is there a way to handle this problem with JavaScript?
I tried pagination but I cannot figure how would i do that as datatable creates pagination for already rendered data?
Is there a way through which I can run my query through pagination at
the backend?
I have come across the same problem when working with mongodb and angularjs. I used server side paging. Since you have huge number of records, You can try using the same approach.
Assuming a case that you are displaying 25 records in one page.
Backend:
Get the total count of the records using COUNT query.
select * from table_name LIMIT 25 OFFSET
${req.query.pageNumber*25} to query limited records based on the page number;
Frontend:
Instead of using datatable, display the data in HTML table it self.
Define buttons for next page and previous page.
Define global variable in the controller/js file for pageNumber.
Increment pageNumber by 1 when next page button is clicked and
decrement that by 1 when prev button is pressed.
use result from COUNT query to put upper limit to pageNumber
variable.(if 200 records are there limit will be 200/25=8).
So basically select * from table_name LIMIT 25 OFFSET
${req.query.pageNumber*25} will limit the number of records to 25. when req.query.pageNumber=1, it will offset first 25records and sends next 25 records. similarly if req.query.pageNumber=2, it will offset first 2*25 records and sends 51-75 records.
There are two ways to handle.
First way - Handling paging in client side
Get all data from database and apply custom paging.
Second way - Handling paging in server side
Every time you want to call in database and get records according to pagesize.
You can use LIMIT and OFFSET constraints for pagination in MySQL. I understand that at a time 2 lacs data makes performance slower. But as you mention that you have to use JS for that. So make it clear that if you wants js as frontend then it is not going to help you. But as you mention that you have a web application, If that application is on Node(as server) then I can suggest you the way, which can help you a lot.
use 2 variables, named var_pageNo and var_limit. Now use the row query of mysql as
select * form <tbl_name> LIMIT var_limit OFFSET (var_pageNo * var_limit);
Do code according to this query. Replace the variable with your desire values. This will make your performance faster, and will fetch the data as per your specified limit.
hope this will helpful.

Server-Side Paging MVC 6.0

I have MVC project with WCF service.
When I display a list of data, I do want to load everything from the database/service and do a client paging. But I do want a server-side paging. If I have 100 records and my page size is 10, then when a user clicks on page 1, it will only retrieve the first 10 records from the database and if a user clicks on Page 3, then it will only retrieve the corresponding ten records.
I am not using Angular or any other bootstrap.
Can someone guide me how to do it?
public ActionResult Index(int pageNo = 1)
{
..
..
..
MyViewModel[] myViewModelListArray = MyService.GetData();
//when I create this PageList, BLL.GetData have to retreive all the records to show more than a single page no.
//But if the BLL.GetData() was changed to retrieve a subset, then it only shows a single page no.
//what I wanted to do is, show the correct no of pages (if there are 50 records, and pageSize is 10, then show
//page 1,2,3,4,5 and only retrieve 10 records at a time.
PagedList<MyViewModel> pageList = new PagedList<<MyViewModel>(myViewModelListArray, pageNo, pageSizeListing);
..
..
..
return View(pageList);
}
The best approach is to use LINQ to Entities operators Skip & Take.
For example, to page
int items_per_page = 10;
MyViewModel[] myViewModelListArray = MyService.GetData().OrderBy(p => p.ID).Skip((pageNo - 1) * items_per_page).Take(items_per_page).ToArray();
NOTE: The data must be ordered, so the pages have some consistency (but I did by an arbitrary field ID). Also some databases required 'order by' to apply 'limit' or 'top' (which is how Take/Skip are implemented).
I put it that way, because I dont know how you are retrieving the data.
But instead retrieving the full list with GetData and then filtering out, better include the pagination in the query inside GetData (so you don't retrieve unnecessary data).
Add paramters page size and page number to your service method and make the result an object which returns TotalCount and a List Items (Items being the items on the current page). Then you can use those values to create the PagedList.
Inside your business logic code you will do two queries one for the count of items and one for the items on the page.
Also if you are starting the project now do yourself a favor and remove the useless WCF service from your architecture.

How do I get a dynamic value for a WHERE query into a data grid in ASP.NET/C#?

I am creating a web page that needs to display a data grid with data pulled from a database. I am building this in MS VS as an ASP.NET/C# application. In my view, I am using a GridView element. I have already connected the GridView to the database and it's pulling the data correctly.
However, I need to restrict the data being pulled by the user ID so only the data for a given user will be displayed. I know how to hard code the user ID value in the GridView element design UI but I need the value to be dynamic. Specifically, it will be read from the url that the user enters.
So what I am trying to accomplish is to extract the user ID from the url string (not the issue, I know how to do this) and then add it dynamically as the WHERE query value in my database query string. It should look something like this:
SELECT * FROM [DatabaseTableName] WHERE ([customerID] = IDExtractedFromURL)
How can I add the dynamic customer ID value ("IDExtractedFromURL") to the GridView element? Is it possible to pass it as a variable? If so, what's the syntax for that?
In common, I'd not pass user IDs in query string, but to accomplish what you want, I'd do:
1) get user ID from query-string
var userId = int.Parse(this.Request.QueryString("userId"));
2) add where filter to your SQL
sql += "\n WHERE [CustomerID] = #userId";
3) add parameter to your grid data source (wherever it's located)
dataSource.SelectParameters.Add(new Parameter("userId", TypeCode.Int32, userId)))

How to limit the number of elements in a checkboxlist?

I have a checkboxlist in my C#/asp.net project and I'm populating it with a dataTable that gets data from a query to my database. The query returns a large amount of data and I want to restrict the number of elements that it shows initially before I filter the data. (To, say, the top 1000). How would I go about doing this?
There are two places where you can limit the number of data.
In the database (assuming you use SQL Server) you can modify the query to return the top 1000 rows.
SELECT TOP 1000 * FROM SomeTable
Or you can filter the data after it arrives using Linq.
var newData = dataTable.AsEnumerable().Take(1000);
I would prefer the first method, so you don't truck around useless data. But the second definitely works as well if you need that data elsewhere.
You can use the Take<> generic IEnumerable method:
var data = someQuery.Exec();
var limitedData = data.Take(1000).ToArray();

Server Side Pagination

I am loading data into my DataGrid via the ItemsSource property. I have a DataPager as well for pagination. The Grid is populated by calling a WCF service which returns a List.
public void webService_GetProductsCompleted(object sender, GetServiceReference.GetProductsCompletedEventArgs e)
{
PagedCollectionView pagingCollection = new PagedCollectionView(e.Result);
pgrProductGrids.Source = pagingCollection;
grdProductGrid.ItemsSource = pagingCollection;
}
Now there is a new requiremement that I want to load data with server side pagination. I'm a newbie learning Silverlight and for me the concept of server side paging is completely new too. So I came here to know what's required for server side pagination. Any good examples, tutorials,step-by-step guidelines that can give me a direction? I have to complete this task in a limited time. Please guide seniors
I typically use 1 of these 2 options:
1: If your data is sorted/paged by a field that has a unique value in it, let the database do the heavy lifting by utilizing the TOP feature and an ORDER BY. This way the smallest amount of data is returned from the server to the page. In the following example, MyTable has a field "NAME" that is unique and is how I want the data sorted. I am getting 10 records per page.
SELECT TOP 10 * FROM MyTable Where Name> [The last name in the previous 10 results] ORDER BY Name
You could use this for multiple fields if you wanted to use a windowing function like ROW_NUMBER() (I don't know what database you are using, this assumes SQL Server)
2: If this is not the case, then it gets ugly. You need to get all the data and get all the records between [The last page * Number per page] and the number of records per page in some iterative code. (Ugly, slow, lots of memory, not possible on large data sets.)

Categories