Hi,
we are developing ASP.Net MVC application and update some data if there is change in database.
I have to implement a functionality where if some changes occur in the Database Table then only the UI needs to be refreshed (automatically) without any user intervention.
It also needs that i cannot implement a Ajax timer where after certain time the UI refreshes if some changes are made or not.
It also needs that i cannot implement a Ajax timer where after certain time the UI refreshes if some changes are made or not.
Please help me on this.
Thanks in Advance.
Rishabh KatariaPosted May 16, 2022, 5:23 AM
use the SignalR in mvc to update the data on client side without refresh the page
1.First Step make the MVC project.
2.Install the package Microsoft.AspNet.SignalR and Microsoft.Owin.
3.Go to Models Folder Add Your Model Class Like Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalR.Models
{
public class Employee
{
public int empId { get; set; }
public string empName { get; set; }
public int Salary { get; set; }
public string DeptName { get; set; }
public string Designation { get; set; }
}
}
4.Add MyHub.cs in Models Folder like MyHub.cs
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalR.Models
{
[HubName("myHub")]
public class MyHub : Hub
{
[HubMethodName("sendMessages")]
public static void SendMessages()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext();
context.Clients.All.updateMessages();
}
}
}
5.Make the Repository class in Models Folder like Repository.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace SignalR.Models GetAllMessages()();
{
public class Repository
{
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
public List
{
var messages = new List
using (var cmd = new SqlCommand(@"SELECT [empId],
[empName],[Salary],[DeptName],[Designation] FROM [dbo].[Employee]", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
var dependency = new SqlDependency(cmd);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
messages.Add(item: new Employee
{
empId = int.Parse(ds.Tables[0].Rows[i][0].ToString()),
empName = ds.Tables[0].Rows[i][1].ToString(),
Salary = Convert.ToInt32(ds.Tables[0].Rows[i][2]),
DeptName = ds.Tables[0].Rows[i][3].ToString(),
Designation = ds.Tables[0].Rows[i][4].ToString(),
});
}
}
return messages;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e) //this will be called when any changes occur in db table.
{
if (e.Type == SqlNotificationType.Change)
{
MyHub.SendMessages();
}
}
}
}
6.Add Your ConnectionString in WebConfig File
7.Add this code into Your Home Controller
using SignalR.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SignalR.Controllers
messages = new List();
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult GetMessages()
{
List
Repository r = new Repository();
messages = r.GetAllMessages();
return Json(messages, JsonRequestBehavior.AllowGet);
}
public ActionResult Create()
{
return View();
}
}
}
8.Add these code of line in your View
@{
ViewBag.Title = "Home Page";
}
@section Scripts{
}
9. Add these line into your Global.asax file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace SignalR
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
}
protected void Application_End()
{
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
}
}
}
10.Add Owin Class into your Project like Startup.cs
using Microsoft.Owin;
using Owin;
using System;
using System.Threading.Tasks;
[assembly: OwinStartup(typeof(SignalR.Startup))]
namespace SignalR
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
IF owin class option is not shown then go to Add->New Items-> search Owin in search bar and add Startup.cs class in your project
Nilesh SawardekarPosted Aug 21, 2019, 11:53 AM
naveen ChakravarthulaPosted Aug 21, 2019, 11:37 AM
Nilesh Sawardekar,
Nilesh SawardekarPosted Aug 21, 2019, 11:26 AM