If you want to know how to get the session value in a static private method in MVC.
I suggest you could try to use System.Web.HttpContext.Current.Session["UserName"] instead of Session["UserName"].
More details, you could refer to below test demo.
Home controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
Session["UserName"] = "Test";
return View();
}
public ActionResult About()
{
ViewBag.Message = Re();
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
private static string Re()
{
string s = (string)System.Web.HttpContext.Current.Session["UserName"];
string name = "SELECT Technician, RequestedBy, Status FROM HelpDesk WHERE RequestedBy = " + s;
return name;
}
}
}
About view result:
Best Regards,
Brando
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
29 Points
127 Posts
Session variable in select clause
Oct 07, 2019 01:10 AM|Baze72|LINK
I have a session variable set in a controller (public ActionResult Index()):
Is it possible in the same controller to use this variable in a select statement in a private static? I have tried:
"SELECT Technician, RequestedBy, Status FROM HelpDesk WHERE RequestedBy = " & Session["UserName"] = user.Name
But that does not work.
Thanks
Star
9831 Points
3120 Posts
Re: Session variable in select clause
Oct 07, 2019 05:31 AM|Brando ZWZ|LINK
Hi Baze72,
If you want to know how to get the session value in a static private method in MVC.
I suggest you could try to use
System.Web.HttpContext.Current.Session["UserName"]
instead ofSession["UserName"]
.More details, you could refer to below test demo.
Home controller :
About view result:
Best Regards,
Brando