I was getting data from the database in this controller and then I am storing the username into a session it works when I move to the view page after login.
public ActionResult shoppage(assign s)
{
if (ModelState.IsValid)
{
using (transactionEntities db = new transactionEntities())
{
var obj = db.assigns.Where(a => a.Username.Equals(s.Username) && a.Password.Equals(s.Password)).FirstOrDefault();
if (obj != null)
{
Session["Username"] = s.Username.ToString();
return View();
}
else
{
return RedirectToAction("login");
}
}
But when I try to access it in another action result it gives me a null value exception
public ActionResult insertwatch()
{
if (Session["Username"] != null)
{
name = Session["Username"].ToString();
}
if (Request.Form["submit1"] != null)
{
transactionEntities dt = new transactionEntities();
itemthing it = new itemthing();
it.uname = name.ToString();
it.itemprice = "1200";
it.itemname = "lucia";
dt.itemthings.Add(it);
return RedirectToAction("shoppage");
}
else if (Request.Form["submit2"] != null)
{
transactionEntities dt = new transactionEntities();
itemthing it = new itemthing();
it.uname = name.ToString();
it.itemprice = "1800";
it.itemname = "Benson";
dt.itemthings.Add(it);
return RedirectToAction("shoppage");
}