Home > Net >  Automatically load newly inserted rows (SQL Server database) in ASP.NET webforms front-end?
Automatically load newly inserted rows (SQL Server database) in ASP.NET webforms front-end?

Time:12-05

I have a SQL Server database with one table and I am doing operations using Entity Framework 6 in C#.

I have a jQuery in my frontend which calls a method in the C# backend and loads the data from the database using Entity Framework.

Now I want to invoke this method automatically as soon my table in database gets any new rows / data.

I don't want to use SignalR, wants to keep it simple as possible.

I am still figuring out the best possible and easiest way to do this. If I add a trigger on my table in SQL Server, then how it will invoke my jQuery method or C# method?

CodePudding user response:

One option to automatically load newly inserted rows from a SQL database on your front-end webforms is to use a timer control. You can set the timer control to periodically check the database for new rows, and if any are found, it can load them onto your webform.

To implement this, you would first need to add a Timer control to your webform. Then, in the code behind file for your webform (e.g. in a .aspx.cs file), you can add an event handler for the Timer control's Tick event. This event will be raised periodically according to the interval that you set for the Timer control.

In the event handler for the Tick event, you can use Entity Framework to query the database for any new rows. If any are found, you can use jQuery to update the webform with the new data.

You may also need to add a trigger to your SQL database table that updates a timestamp column whenever a new row is inserted. This will allow you to only query for rows that have been inserted since the last time the Timer control's Tick event was raised.

Overall, using a Timer control to periodically check the database for new rows is a relatively simple approach that does not require using SignalR. However, keep in mind that this approach may not be suitable for high-traffic or real-time applications, as it may not be able to keep up with the rate of incoming data. In those cases, you may want to consider using a different approach such as SignalR.

CodePudding user response:

If I add a trigger on my table in SQL Server, then how it will invoke my jQuery method or C# method?

It can't, at least not directly, without implementing something poorly like a CLR procedure that then invokes your application.

You can look into Query Notifications to potentially accomplish your goals in a more vanilla way, but be warned, you'll probably be the only person in existence actively using this feature:

Built upon the Service Broker infrastructure, query notifications allow applications to be notified when data has changed. This feature is particularly useful for applications that provide a cache of information from a database, such as a Web application, and need to be notified when the source data is changed.

  • Related