Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating an MVC application.I am getting a null value in variable when I fetching data from database.

My Model Class Code

C#
public partial class tblUser
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Please Enter Your Email Id")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Please Enter Your Password")]
    [DataType(DataType.Password)]
    [StringLength(18, ErrorMessage = "The password must be atleast 3 characters long", MinimumLength = 3)]
    public string Password { get; set; }

    [Required(ErrorMessage = "Please Enter Your Password")]
    [DataType(DataType.Password)]
    [StringLength(18, ErrorMessage = "The password must be atleast 3 characters long", MinimumLength = 3)]
    public string NewPassword { get; set; }
}



Here is my change password view code

HTML
@using (Html.BeginForm("Changepassword", "Home", FormMethod.Post))
       {

               <table class="center">


                   <tr>
                       <td>Old Password</td>
                       <td>
                           @Html.EditorFor(pass => pass.Password)
                       </td>
                       <td>@Html.ValidationMessageFor(pass => pass.Password)</td>
                   </tr>
                   <tr class="rowspace">
                       <td>New Password</td>

                       <td>
                           @Html.EditorFor(pass => pass.NewPassword)
                       </td>
                       <td>@Html.ValidationMessageFor(pass => pass.NewPassword)</td>
                   </tr>

                   <tr class="rowspace">
                       <td colspan="3" id="button">
                           <input type="submit" value="Change Password" /></td>
                   </tr>
                   <tr class="rowspace"><td colspan="3">@ViewBag.Message</td></tr>
               </table>

       }


Here is my Home controller code.

Problem I am facing - var userDetail is returning null and when i checked and debug my code using breakpoint my login.Email is not fetching email from database and it is returning null.

C#
public ActionResult Changepassword(tblUser login)
       {
           using (UserDetailsEntities db = new UserDetailsEntities())
           {
               var detail = db.tblUsers.Where(log => log.Password == login.Password).FirstOrDefault();
               if (detail != null)
               {
                   var userDetail = db.tblUsers.FirstOrDefault(c => c.Email == login.Email);


                   if (userDetail != null)
                   {
                       userDetail.Password = login.NewPassword;

                       db.SaveChanges();
                       ViewBag.Message = "Record Inserted Successfully!";
                   }
                   else
                   {
                       ViewBag.Message = "Password not Updated!";
                   }

               }
           }

           return View(login);
       }


What I have tried:

I thought my view does not have a HiddenFor for the Email property of the model, so it will be null in my HttpPost method but it didn't work out.
Posted
Updated 1-May-16 5:11am
Comments
Ehsan Sajjad 1-May-16 11:05am    
what is exact value in the login.Email when you debug and what is in the table?
F-ES Sitecore 1-May-16 15:02pm    
login.email is coming from your model, not the database. You should do this in a single call anyway;

var detail = db.tblUsers.Where(log => log.Password == login.Password && log.Email == login.Email).FirstOrDefault();

You shouldn't store your passwords in plain text anyway, do some research into storing passwords as hashed values with salt.

1 solution

Your problem lies on the following line:

C#
var userDetail = db.tblUsers.FirstOrDefault(c => c.Email == login.Email);


i suspect you want it to be :

var userDetail = db.tblUsers.FirstOrDefault(c => c.Email == detail.Email);


because login isntance will have Email null as you are not passing that from view as hidden field so it will be null, you are fetching user on the basis of password match right now, and if any row found you are want to pass the emial returned from that instance to use to fetch user details.

Please note that, your way of getting row on the basis of password is not right, as multiple users can have same password con-incidently, so in that case there will be problems, if user 1 is updating his password and user2 also has same password, the password for which user will get updated? Think about that scenario.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900