ASP.net - How to put a recordset in cache - c#

I have around 100 rows in database ( which can increase in future ) for different labels used in page. These rows are required on all pages. I want to do it in a way that I get these rows once from db and put some where in cache or something similar to that. So that i don't have to query database on every page refresh. And how can this cache be used by all site visitors?

You can use Cache Variables something like this :
Cache["COMMON_DATA"] = lstData (This can be any collection like LIST<Country>)
You can also use SqlCacheDependency in asp.net that will auto reset your cache if more rows are added in the the database
else you can update the Cache again manually after inserting data in the table

Caching in ASP.net and Application State would be a couple of ideas for you.

Related

how to recognize newly added rows directly to datagridview - c#

I have bound a datatable to a datagridview and allowed the user to delete update, and insert new records directly to this datagridview
Now I want to know how do I recognize newly inserted row/rows ?
I want to get new row/rows and delete update, or insert them
into SQL server database.
How can I do it ?
Thanks
You can look into the DataTable.GetChanges method https://msdn.microsoft.com/en-us/library/thc1eetk
or better look into the DataAdapter.Update method https://learn.microsoft.com/en-us/dotnet/api/...
You can do it in 2 different ways - depend on your use case:
1st: Server side
Use this concept if you want to track the changes with multiple session per user, submit all changes to server side and track the changes datetime and save status.
2nd: Client side
Use this concept if you want to track the changes on client side regardless of the multi-session per user, basically, add 1 extra column to track the changes history -better to keep it hidden-

Improving nested objects filtering speed

Here's a problem I experience (simplified example):
Let's say I have several tables:
One customer can have mamy products and a product can have multiple features.
On my asp.net front-end I have a grid with customer info:
something like this:
Name Address
John 222 1st st
Mark 111 2nd st
What I need is an ability to filter customers by feature. So, I have a dropdown list of available features that are connected to a customer.
What I currently do:
1. I return DataTable of Customers from stored procedure. I store it in viewstate
2. I return DataTable of features connected to customers from stored procedure. I store it in viewstate
3. On filter selected, I run stored procedure again with new feature_id filter where I do joins again to only show customers that have selected feature.
My problem: It is very slow.
I think that possible solutions would be:
1. On page load return ALL data in one viewstate variable. So basically three lists of nested objects. This will make my page load slow.
2. Perform async loazing in some smart way. How?
Any better solutions?
Edit:
this is a simplified example, so I also need to filter customer by property that is connected through 6 tables to table Customer.
The way I deal with these scenarios is by passing in Xml to SQL and then running a join against that. So Xml would look something like:
<Features><Feat Id="2" /><Feat Id="5" /><feat Id="8" /></Features>
Then you can pass that Xml into SQL (depending on what version of SQL there are different ways), but in the newer version's its a lot easier than it used to be:
http://www.codeproject.com/Articles/20847/Passing-Arrays-in-SQL-Parameters-using-XML-Data-Ty
Also, don't put any of that in ViewState; there's really no reason for that.
Storing an entire list of customers in ViewState is going to be hideously slow; storing all information for all customers in ViewState is going to be worse, unless your entire customer base is very very small, like about 30 records.
For a start, why are you loading all the customers into ViewState? If you have any significant number of customers, load the data a page at a time. That will at least reduce the amount of data flowing over the wire and might speed up your stored procedure as well.
In your position, I would focus on optimizing the data retrieval first (including minimizing the amount you return), and then worry about faster ways to store and display it. If you're up against unusual constraints that prevent this (very slow database; no profiling tools; not allowed to change stored procedures) than please let us know.
Solution 1: Include whatever criteria you need to filter on in your query, only return and render the requested records. No need to use viewstate.
Solution 2: Retrieve some reasonable page limit of customers, filter on the browser with javascript. Allow easy navigation to the next page.

How would I audit a MVC3 .NET application?

I'm requiring a way to audit an MVC3 EF application capturing the following values:
Timestamp
Field Name
Old Value
New Value
I think I've wrongfully done the binding manually, and as a result all the row is updated after an edit (so trigger will assume everything is being updated)... therefore rather avoid DB triggers as it'll need a re-write of all the binding.
I would imagine, if I can capture the old values (somehow), and then compare to the new values, I can populate an audit table with the above fields.
Any advice on this would be much appreciated.
Depending on the version of SQL you using you could look in to Change Data Capture
You can subscribe to the saving changes event of the entity. Here is an example... Change History in MVC and EF

how to show in asp.net grid total rows and actual rows without slowing the performance

we have a asp.net1.1 3 tiers project in production .
right now for fetching data to asp.net grid, we have a constant call maxRows, and we don't fetch more than that (for performance reason).
we have a new request from the customer, "show me the num of total rows in the asp.net grid and the num of actual rows that have been fetch for each grid in the project."
between the presentation layer and the business layer we use .net remoting, and we are passing dataset between the tiers.
the database is ms sql server 2005.
we plan to upgrade this project to .net4.0
what is the solution for this request for .net1.1 and for .net4.0 ?
does asp.net grid have a built in feature to handle paging so we don't get outOfMemory exception for fetching a large amount of records ?
I will suggest you to use CustomPaging.
It will show both the total number of rows in the dataset and you can fetch only the current page of grid and show it to the user:
Refer to the following documentation:
http://msdn.microsoft.com/en-us/library/aa713421%28v=vs.71%29.aspx
https://web.archive.org/web/20211020140032/https://www.4guysfromrolla.com/articles/031506-1.aspx
two labels per grid.
num_of_rows_in_grid_label.Text = gridname.Rows.Count.ToString();
actual_num_of_rows_label.Text = datasourcename.Rows.Count.ToString();
(here datasourcename is a name of dataset, datatable or whatever u have there)
this is just a sample code to demonstrate logic, dunno what ur actual code looks like.

Pagination improvements in ASP.NET 2.0 Grids

I currently have a datagrid bound to a table with tens of thousands of records. I display the datagrid using the existing asp.net 2.0. I show ten records at a time.
My problem is whenever I try to access the next page, I gets all the records again from the database and then displays the ones that are required. This is slowing down the app. Is there feature within .net 2.0 which will help me optimize this issue? I cant use third party controls or ajax.
UPDATE
I cant use SQL to get 10 records at a time as well. Without changing any existing business logic or data retrieval, I want to do this.
You can do paging in your SQL and/or stored procedure.
Basically, you pass the sproc the number of items on the page and what page you are on. For example, page 3 and 20 records per page.
If you can use SQL Server 2005 or higher, you can use some newer keywords that make the query easier.
Here is a simple version of such a query:
SELECT ClientName, RowNumber
FROM (SELECT ClientName, ROW_NUMBER() OVER (ORDER BY ClientName) AS RowNumber
FROM Clients) AS cl
WHERE RowNumber BETWEEN 12 AND 30
You would make the values above of "12" and "30" be input parameters.
This way, you are only returning the rows you are going to display.
You can stick your datasource in the session.
then check if your DataSource is null from the session and then go get it.
This way you only get your data one time and if you need to get fresh data (say because you updated something) just empty the session variable that holds your data source.
EDIT:
here's a half attempt at an example, (by this i mean I'm not going into how to change the page because I assume you already know how to do that)
protected void PageChanging(object sender, GridViewPageEventArgs e)
{
if(Session["YourSessionVariableForData"] == null)
Session["YourSessionVariableForData"] = YourDataCall();
YourGridView.PageIndex = e.NewPageIndex;
YourGridView.DataSource = ((YourDataType)Session["YourSessionVariableForData"]);
YourGridView.DataBind();
}
Do the Paging in a Stored Procedure. Look into using the ROW_NUMBER() function for this. Create a class that will call the stored procedure and wire it to the grid using an ObjectDataSource.

Categories

Resources