ASP.NET - How to show a pop up message when a record is added in the SQL SERVER database

Asked By dinesh on 10-Apr-13 08:58 AM
Hi friends,

       I have an web application in asp.net with MVC 4.0. My requirement is when ever my web application is opened a pop up should be showed with the newly added record in the table of Sql server database.
How to do this.

With Regards,
Dinesh
Erik Little replied to dinesh on 13-Apr-13 01:26 PM
I do not know the fields you're querying but I can get you started.

--This example shows how to use the web api with the mvc controller. This might not be what you need but it is a little easier to understand and use, just my personal opinion.

-SQL Query

--ID should be a incremental primary key

create proc proc_GetRecords
 
@LastRecordID INT
 
AS
 
SELECT * FROM MyTable mt WHERE mt.ID > @LastRecordID



//when the window opens call your MVC controller
window.onload=function(){

//if you did not pass a lastrecordID check the localstorage of the browser
function GetRecords(lastrecordID) {

    lastrecordID = lastrecordID || window.localStorage.getItem('lastrecordID');
   
    $('div').dialog({
    autoOpen: true,
    height: 'auto',
    width: 'auto',
    //position: [$(e.target).position().left,
    //   $(e.target).position().top + 40],
    open: function (event, ui) {
    $.ajax({
      type: "GET",
      cache: false,
                      //the web api controller your working with
      url:'api/lastestrecords?id='+lastrecordID,
      async: false,
      success: function (data) {
 
   lastrecordID = data.records[data.records.length-1].ID
        
      if (!lastrecordID || lastrecordID <1) {
        //no new record then return with out doing anything
      return;
      };
  //store the last lastrecordID in localstorage
  window.localStorage.setItem('lastrecordID', lastrecordID); //do something with the records data
      }
    });
    }});
 
  }
}


--web api controller

public class LastestRecords : ApiController
  {
    public HttpResponseMessage Get( int id)
    {
 
      //var lastestRecords=  //call sql query context.proc_GetRecords(id);
return Request.CreateResponse(HttpStatusCode.Created, new { records = lastestRecords }); ;
    }
}