0

Hi I want To Make one Login Page For Members And Admin Also.. Here I have done the Following Code as:

In Model:

   public class Login
        {
             public int ID { get; set; }
             public string UserName { get; set; }
             public string Password { get; set; }
        }

controller

[HttpPost]
public ActionResult Login(Login login, MemberDetailsWrapper memberlogin)
{
    WrapperDB db = new WrapperDB();
    if (login.UserName.Equals("admin") &&login.Password.Equals("admin"))     //This will check for admin
    {
           returnRedirectToAction("Index");
    }
    else if
    (
        (login.UserName.Equals(memberlogin.objMemberBasicInformation.MemberFirstName.Equals(db.MemberBasicInformationDBS.Where(a =>a.MemberFirstName.Equals(a.MemberFirstName))))
        && (login.Password.Equals(memberlogin.objMemberBasicInformation.Qualification.Equals(db.MemberBasicInformationDBS.Where(a=>a.Qualification.Equals(a.Qualification))))))
    )
//This is For Member Login When Member Enter His FistName as a userName and Qualification as Password then Both credential match with the data present in db and return the respected person information
    {
           return RedirectToAction("Index","MemberDetails");
    }               
    ModelState.AddModelError("", "UserName And Password is Incorrect!!");
    return View();
}

In view :

<%@PageTitle=""Language="C#"Inherits="System.Web.Mvc.ViewPage<SHGManagementProject.Models.Login>"%>

<h2>Login</h2>
<%using (Html.BeginForm("Login", "Home", FormMethod.Post))
  { %>
<%:Html.ValidationSummary(true) %>
<fieldset>
<legend>Log in Form</legend>
<div>
UserName
<%:Html.TextBoxFor(m=>m.UserName) %>
</div>
<br/>
<div>
        Password
<%:Html.PasswordFor(m=>m.Password) %>
</div>
<br/>

<div>
<inputid="SubmitLogin"type="submit"value="Login"/>
<%--<%:Html.ActionLink("Are you Member? click Here!!","Login","Account") %>--%>
</div>
</fieldset>
<%} %>

On execution When i Enter the Username and Pass as admin then it will perform successfully.. but when i enter the another username and password then it throws Error NullReferenceException was Unhandled by User code

please tell me the solution and help me to solve this

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
SBSB
  • 23
  • 8

1 Answers1

0

Without knowing where your code is throwing the error, I'm assuming the issue is down to memberLogin parameter?

[HttpPost]
public ActionResult Login(Login login, MemberDetailsWrapper memberlogin)
{
    WrapperDB db = new WrapperDB();
    if (login.UserName.Equals("admin") &&login.Password.Equals("admin"))     //This will check for admin
    {
           returnRedirectToAction("Index");
    }
    else if (memberlogin != null)
    {
       if   
        (
            (login.UserName.Equals(memberlogin.objMemberBasicInformation.MemberFirstName.Equals(db.MemberBasicInformationDBS.Where(a =>a.MemberFirstName.Equals(a.MemberFirstName))))
            && (login.Password.Equals(memberlogin.objMemberBasicInformation.Qualification.Equals(db.MemberBasicInformationDBS.Where(a=>a.Qualification.Equals(a.Qualification))))))
        )
        {
           return RedirectToAction("Index","MemberDetails");
        }
        else
        {
            ModelState.AddModelError("", "UserName And Password is Incorrect!!");
            return View();
        }
    }

    ModelState.AddModelError("", "UserName And Password is Incorrect!!");
    return View();
}

I've added a check against memberlogin to check if it's null, double check the parenthesis though, as I've just typed this by hand.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82