Checking the database for new rows at regular intervals of time - c#

I'm creating a web application in which I need to Check the Database in regular Intervals for any new rows Updated in the table. If any Rows have been updated and return the Rows to the user.
When I refresh the Page I update the rows in my Website. But I need to Know If any new Rows have been added to the Table without refreshing the Page
How can I achieve this ??
Can you suggest me a Method (Probably which doesn't slow down the web site).
Can I achieve this using web services??

The usual way to do this is using javascript to hit a page on your website with setInterval posting back to a page to ask about any changes, web sockets can do this real time but require modern browsers.
Here is a similar question that's been answered well, though it's for Asp.net MVC:
Creating an AJAX alert for a user when there's a server-side change
If I'm being forced to use vanilla ASP.Net I do usually put those kind of requests into a webservice.

Related

Refresh table when new record is inserted in ASP.NET Core MVC

I'm creating an app with ASP.NET Core MVC, Razor, EF Core, SQL Server as the database.
I have 3 views: List, Create, and Edit.
List - showing the data from the table (as a list)
Create - for creating a new record
Edit - to edit a selected record
These pages will be accessed by different users.
The case is: how can I update the list without refreshing the List page? So when there's a new record, the data table will be updated with a new record, even when the record is edited.
I am trying the code with this sample --> https://github.com/dyatchenko/ServiceBrokerListener
But it seems that the page didn't updated.
My code:
public IActionResult Index()
{
var connectionString = this.Configuration.GetConnectionString("SQLConnection");
var listener = new SqlDependencyEx(connectionString, "ProductStatistic", "TrialPurpose", listenerType: SqlDependencyEx.NotificationTypes.Insert);
listener.TableChanged += (o, e) => Console.WriteLine("Your table was changed!");
listener.Start();
var model = _db.TrialPurposeDataViews.FromSqlRaw("exec spTrialPurposeDataView").ToList();
listener.Stop();
return View(model);
}
Did I make a mistake on the code?
Need advice and help, really appreciated.
Thank you.
How can I update the list without refreshing the List page?
Do you mean that you want to see record changes in near-real-time in your web page, without requiring an entire postback / page refresh?
You need to use client side programming for that, and the only client side language that works in browsers is Javascript. MS are pushing something new called "Blazor" for client side programming, but it's not really "native" as far as I can tell.
The functionality you're after is delivered using something called "AJAX" which is basically "Using javascript to make your web page behave like a thick client application"
There are now many javascript plugins, libraries, frameworks and layers that do what you want. I recommend you buy a third party tool rather than write your own. I've seen Telerik used successfully in the past.
It's not very well explained in various ASP.Net MVC articles that there is no way to do all that "standard" stuff if just using ASP.NET MVC.
If you do take the AJAX / Telerik route, keep in mind that you still need to build API's in your controller. These basically return data, often in JSON format, which your client side javascript uses to update the page in-place rather than having to post back.
An option that I tried and gave up on was the free jqGrid libray. Here's an example of how incredibly complicated it is to get seemingly simple things like grid updates running.
Real-time data in a grid - better method

instant insert/edit

I've seen multiple websites on the web where they allow you to manipulate data. (i.e. Insert records, delete records, edit records).
The cool thing I've noticed is that when you click remove button, the item removes instantly from the grid. and then it asynchronously goes to a database and removes record from there without bothering a user.
For example in a regular asp.net application if you're using gridview you need to wait until database operation is complete, because gridview is a databound control. If you use update panel you still see item in grid until DataBind event fires again.
Question: is there any js/.net library that will allow me to add such a functionality to the project out of the box?
Maybe you want to use WebMethod on server side + Ajax calls on client side (using jQuery for example).
You can you Client Side Grid Like JQGrid. http://www.trirand.com/blog/jqgrid/jqgrid.html. For loading and editing data you can use Web method or web Services. You can also use ASP.net MVC .
Yes you can do.You have web method that read your data from your source such as database.
You can call this method using Ajax and you can bind ajax response to gridview
Here is example Link

Is caching a good idea in this scenario and if so what kind

I am using asp.net with sql server 2008 r2
I am building a website where users can run reports based off information in a database. For one of the reports I have about 6 checkboxlists that dynamically load based off the information in the database which then users can select certain boxes to narrow down the information that will be included in their report.
The data that populates these checkboxlists does not update but about once every couple days. Right now I have the check boxes loading in the initialize control event for each checkboxlist. Each time I select a different tab and then come back to the tab which includes these checkboxlists it seems to reload the checkboxlists each time which is slow and takes about 6-10 seconds.
I am new to caching, is this where it would be helpful to cache the data that loads into the checkboxlists so the site will not need to perform continuous loads?
Yes caching would be helpful in this case.
You can read more about caching application data here: http://msdn.microsoft.com/en-us/library/6hbbsfk6(v=vs.100).aspx
You could cache at your data access layer, then your logic would be:
Get checkbox data
Check if data is stored in cache
if so, return it
if not, hit the database for it, store it the result in cache, then return it.
For specific information about how to add to and read from the cache, see the above link.
Yes
If you're checkboxes load data through Ajax, then you should probably use ASP.NET output caching capabilities.
If you render them on the server - you could store the data in some DataObject (singleton, or static field of some object) so it would be available to all requests that you server serves.
You could wrap this functionality in a singleton object, that when initialized load the data from the DB, and then on each get of the data check whether it should be reloaded (and reloads it if needed)

Using asp.net C# and jquery to filter a collection of data

I'm developing a webpage where users can filter data and then export the selection to pdf or excel. The website is using ASP .NET C#.
To filter the data I have crated a webpage containing a left column with boxes and drop downs enabling users to enter search criteria and then click a search button. This is the typical search/filter functionality that a lot of websites contains. After filtering the data, the selecting is displayed using an asp listview. This listview generates a table. I've implemented Dragtable, enabling users to alter the sequence of the columns in this table, and also the columnManager enabling users to select which columns to display.
So my problem is that the filter/search functionality is resolved back-end using asp .net C#, while the selecting and rearranging of columns is done client-side using jQuery. When users now click the "export"-button, I'd like to collect the filter done by the asp controls in addition to the filter done by jQuery, and create the file. This file should only contain the columns selected using columnManager, in the same order they are in after being altered using dragtable.
What is best practice when using both server side filter and client side filter? I've also considered to drop all of the server-side filtering and use only ajax.post() to send all filters in JSON format to the server.
I also want to enable users to save their current filter for another time.
Update
I've decided to drop the server side and use ajax (getJSON to be more precisely) to get the search result. This way I don't loose the ordering and sorting of columns that is done with jQuery/js, which I would with a post-back (could use cookies'n stuff, of course).
I also keep all the logic in one place, and JSON objects can easily be wrapped/unwrapped server-side. Thanks for your answers, though!
It's not at all bad to have Client side and Server side working at same place.
All the good applications running in the world have these combination together.
However we need to see the approach.
If you don't want any postbacks , I would recommend to go with Jquery.
If you are ok with roundtrips, go ahead with Server Side and Ajax.
Your approach has another problem: What happens if the user orders the columns first and than filters it? The order of the columns is resetted.
However with the jquery ui draggable you are able to fire ajax calls to notify the server side code about the change. You can bind a function to the stop event: http://api.jqueryui.com/draggable/#event-stop
So I would do the following:
Fire ajax call when the user changes the order of the columns, but do not refresh the data
Fire ajax calls when the user adds a filter AND refresh the data
The server side code is always up to date and renders the correct ouput if it has to.

Advanced method for retrieving data from the database

In my site, I'm using update panel in the master page. Half of my web page will retrieve the data from the database in dynamic. As, my update panel is in master page(with ajax loader), it is taking much time for every event. Is there any other advanced method to get the data from the database instead of using update panel.. Or any other idea instead of this?
I think you need to profile to see where the bottleneck is; I expect you have unnecessary code going on in your aspx that isn't really needed for the UpdatePanel.
Personally, I wouldn't use UpdatePanel now; I'd use a simple (but separate) page (or route, if using MVC) that just does the code needed for this work, and use jQuery to load it.
I think you want to load your web page from database table which contains html. If it is so then the best idea would be to create controls for headers/ footers or repeated sections and use caching which would reduce the load time of your web pages.
You could easily cache your controls. You can check here http://msdn.microsoft.com/en-us/library/aa478965.aspx
Happy coding

Categories

Resources