Paging in ASP web page? - c#

I'm new to this so forgive me if this is trivial. Anyway, I'm creating a simple ASP web page that reports data from a table in a database that has 5 items in it. I use a GridView to display the data and that works fine, but I'd like to limit the results to three items so that I can allow paging. I tried configuring the select statement from something like "select * from country" to
select * from country limit 3
but I get a "There was an error executing the query" message when I try to test it. Is there some other way that I have to do this?

set Gridview's property PageSize=3

Try to use TOP keyword instead if you use SQL Server database:
SELECT TOP 3 * FROM COUNTRY;

To do this in SQL Server you need to use top
select top 3 * from country

Depending on the total number of records you are expecting to have in the country table a simple solution would be to use the asp:DataPager control without any limits on the number of rows returned from the SQL.
This will handle all the paging for you, however the full data set is retained in the page view state and so is not an appropriate solution is you are expecting 1000's of records.

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.

Firing many many sql queries at once

We have a 4 column data grid display on a page. I would like to perform a query based on each of the values within each cell of the grid i.e. 4 queries for each row.
This is so I can populate the cell with count of records in db that matches that value.
When each row gets populated by jqgrid, it fires off an ajax call for each cell.
I think it's a very bad idea since I have already discovered the browser limits the number of ajax calls to the same server.
Are there similar limits for ado.net?
I would like to batch these queries together so I do fewer calls to the db, is this what you would do?
How would you approach this?
You could combine your AJAX calls into one, the resulting object contains an array, or multiple properties for each result set, and then you run your SQL in parallel on the server.
Check out this QA for how to use options on how to use TPL and SQL.
Parallel.Foreach SQL querying sometimes results in Connection
me would suggest you to select the associated id data in the first request and populate the values in the UI.
You could either use join or left join in your first query based on your requirement and architecture and fetch the specific column value/ count(id) ( Here you mentioned as the count of records ).

Issue with Large sql query in asp.net webpages

Hi Hope someone can help.
I am trying to render a list of all my product url's I am using ASP.NET WEBpages (not WebForms or MVC).
But if the Database query is over a certain amount of records it gives me the following error.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform
runtime binding on a null reference
If I put into the SQL query Top 500 it works fine.
my db query
var db = Database.Open("MyConnectionString");
var Products = "SELECT Top 500* FROM shop_products WHERE site_id = '99' AND product_active = 'Y' ORDER BY product_name ASC";
I used to be able to do this in Classic ASP.
Might there be a limit on SQL query sizes in asp.net, If so how do i get around this.
Help
The key phrase in the error message is "cannot perform runtime binding on a null reference".
I suspect that the top 500 rows in the "shop_products" table did not have a column value with a NULL value. That is why that select worked and the select without the "top 500" qualifier did not.
Your code needs to detect DBnull values and assign a default value for these cases or do some sort of error processing to avoid this runtime error.
To solve this I split this down into smaller SQL query's, split out by the category's the products where in.

Selecting the same column from the same table multiple times in the same statement

I am converting a VB6 app to C# with an SQL Server back end. The app includes a very general query editor that allows the user to write any select query and return the results visually in a grid control. Some of the tables have several hundred columns (poor design, I know but I have no control over this). A typical use case for an admin user would be to
select * from A_Table_With_Many_Columns
However, while they want to be able to view all the data, they are particularly interested in 2 columns and they want these to be displayed as the first 2 columns in the grid (instead of 67th and 99th for example) so instead they execute the following statement:
select First_Interesting_Field, Second_Interesting_Field, *
from A_Table_With_Many_Columns
Then they will go and modify the data in the grid. However, when saving this data, it results in a concurrency violation (DBConcurrencyException). This worked fine with the connected RecordSets of VB6 but not so well in C#. I have tried a myriad of solutions to no avail.
Does anyone know how to handle this exception in a generic way? (Remember, the user can type ANY select statement or join etc. into the query editor)
Does anyone know how I might manipulate the columns returned such that I delete the 2 columns that appear further on in the list? (My difficulty here is that if the column name in the database is EMail so I do select Email, * from Blah the 2 pertinent columns returned are EMail and ADO.NET or C# aliases the second EMail column from the * portion of the query as EMail1 so I am not able to detect the second column as a duplicate and remove it)
Does anyone have an alternate solution I have not thought of?
Thank you very much
Actually, you could rename all variables to something like email_userdefined by doing something like this:
SELECT First_Interesting_Field as First_Interesting_Field_userdefined, Second_Interesting_Field as Second_Interesting_Field_userdefined, *
from A_Table_With_Many_Columns
Replace user_defined with whatever you want, like order number or anything else user acceptable

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