user7677010
user7677010

Reputation:

Session variable returning null

I have a login page and I tried storing some data in my sessions but when I run the project it gives me this error as you see in screenshots:enter image description here

I tried to debug my code as you see in screenshot and its show me Both No_ and E_mail is null even though I checked my database and those columns. No_ and E_mail both have data: enter image description here


The Code in Controller:

[HttpPost]
public ActionResult Login(string Mail)
{     
    using (DataContext db = new DataContext())
    {
        ED_data_A_S_Contact con = new ED_data_A_S_Contact();
        ED_data_A_S_Contact_Business_Relation cb = new ED_data_A_S_Contact_Business_Relation();

        var user = from cbr in db.Contact_Business_Relation
                   join c in db.Contact
                   on cbr.Contact_No_ equals c.Company_No_ into f
                   from c in f.DefaultIfEmpty()
                   //where c.E_Mail == Mail
                   select new
                   {
                       Mail = c.E_Mail
                   };

        if (user != null)
        {
            Session["No_"] = cb.No_.ToString();
            Session["Email"] = con.E_Mail.ToString();
            FormsAuthentication.SetAuthCookie(Mail, false);

            return RedirectToAction("Index");
        }    
        else
        {
            ModelState.AddModelError("", "Kunde nr er ikke gyldig");
        }
    }

    return View();
}

public ActionResult LoggedIn()
{   
    if (Session["No_"] != null)
    {   
         return View();   
    }
    else
    {
        return RedirectToAction("Login");
    }
}

Contact_Business_Relation :

public string No_ { get; set; }

Contact:

public string E_Mail { get; set; }

The Code in view:

@model ED_data_A_S_Contact

    <div class="login-wrap">
        <div class="login-html">
            <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">Log ind</label>

            <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab"></label>

                <form class="login-form" method="POST" enctype="multipart/form-data" action="/Account/Login/">
                    @*@Html.AntiForgeryToken()*@
                    <div class="sign-in-htm">
                        <div class="group">
                            <label for="No_" class="label">Kunde ID</label>
                            <input style="color:black;" id="Email" name="Mail" value="" required="" type="email" placeholder="Kunde ID ..." class="input">
                            <button style="float: right;margin-top: -36px; background: transparent; border: none;margin-right: 11px;"  type="button" id="eye">
                                <img src="https://cdn0.iconfinder.com/data/icons/feather/96/eye-16.png" alt="eye" />
                            </button>

                            @*@Html.ValidationMessageFor(u => u.CustomerID)*@
                        </div>


                        <div class="group">
                            <input type="submit" class="button" value="Log på">
                        </div>
                        <span style="color:#dadada">@*@Html.ValidationSummary(true)*@</span>
                        <div class="hr"></div>

                        <div class="foot-lnk">

                            <a target="_blank" href="#">Problem med login?</a>
                        </div>
                    </div>

</form>

        </div>
    </div>

Can anyone direct me in the right direction? thx

Upvotes: 2

Views: 963

Answers (1)

Stephen Wilson
Stephen Wilson

Reputation: 1514

ED_data_A_S_Contact_Business_Relation cb = new ED_data_A_S_Contact_Business_Relation();

You don't actually seem to set the properties of this object anywhere before accessing them. All you do is new the object. All the properties have their default values.

Perhaps you should be accessing user.Mail? In which case you should also set the other properties also.

Try this:

// Populate the user variable so we can use it below to set session variables
var query = from cbr in db.Contact_Business_Relation
            join c in db.Contact
            on cbr.Contact_No_ equals c.Company_No_ into f
            from c in f.DefaultIfEmpty()
            where c.E_Mail == Mail
            select new
            {
                Mail = c.E_Mail,
                No = c.No // Or whatever the relevant property is in c or cbr
            };

var user = query.FirstOrDefault();

            if (user != null)
            {
                 Session["No_"] = user.No.ToString();
                 Session["Email"] = user.Mail.ToString();
                 FormsAuthentication.SetAuthCookie(user.Mail, false);

                 return RedirectToAction("Index");
            }

Upvotes: 1

Related Questions