2

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

3
  • 1
    But you don't populate the cb object fields with anything, you just create it with new, and it seems the default constructor does not do anything with the fields either. You are not loading data for that object from the database Commented Oct 12, 2017 at 13:54
  • You sure session is null? seems like object cb is null. Commented Oct 12, 2017 at 13:55
  • 1
    @Andrei i have always problem with this , would you please give me example :) Commented Oct 12, 2017 at 14:03

1 Answer 1

1
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");
            }
Sign up to request clarification or add additional context in comments.

7 Comments

I've updated my answer with an example based on your code - oh, and stop using all those _ in your variable and class names, not good.
i did excat same thing about 1hr ago and than i changed it , i tried again but its give same error , i got before : System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'Mail' and no extension method 'Mail' accepting a first argument of type 'System.Linq.IQueryable<AnonymousType#1>' could be found (are you missing a using directive or an assembly reference?)
I've updated my answer, hopefully this is what you need.
tnx Stephen its solved the problem,But idont understand it , i can login with any emails for ex i can login with [email protected] or [email protected] which i dont have any this emails in my DB ,beside this i use both sessions in one of my pages to see in browser is that correct info or not , and both session just show me one email and one No , it dosent matter with which emai i logged in , is that problem with my query or ...?
OMG . yeah , dat was it , dats happend when i coding with anger lol , tnx Stephen for your help and your time .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.