Sequence contains no matching element

Posted by Rajeshmoorthy under ASP.NET MVC on 12/15/2015 | Points: 10 | Views : 1126 | Status : [Member] | Replies : 4
I am using FirstorDefault in my linq query. I still get the above error.

My model
 public partial class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }

[Required]
[StringLength(20)]
public string UserName { get; set; }

[Required]
[StringLength(50)]
public string UserPassword { get; set; }

public DateTime? LastModified { get; set; }

public bool? Inactive { get; set; }

[StringLength(50)]
public string Firstname { get; set; }

[StringLength(50)]
public string Lastname { get; set; }

[StringLength(30)]
public string Title { get; set; }

[StringLength(3)]
public string Initial { get; set; }

[StringLength(100)]
public string EMail { get; set; }

public DateTime? UserLastLogin { get; set; }

public int? FailedLoginAttempts { get; set; }


[StringLength(1)]
public string PasswordChangeatLogin { get; set; }

public bool? IsAdmin { get; set; }
}
}


My code in the action
public ActionResult Login(Models.User user)
{
// var errors = ModelState
//.Where(x => x.Value.Errors.Count > 0)
//.Select(x => new { x.Key, x.Value.Errors })
//.ToArray();
if (ModelState.IsValid)
{
if (IsValid(user.UserName, user.UserPassword))
{
FormsAuthentication.SetAuthCookie(user.UserName, true);
return RedirectToAction("Default", "Home");
}
else
{
ModelState.AddModelError("", "Login data is incorrect!");
}
}
return View(user);
}

private bool IsValid(string username, string password)
{

bool IsValid = false;

using (var db = new PowerERP.Models.PowerModel())
{
var user = db.Users.FirstOrDefault(u => u.UserName == username);
if (user != null)
{
var encryptedPassword = CryptorEngine.Encrypt(password.Trim(), true);
if (user.UserPassword == encryptedPassword)
{
IsValid = true;
}
}


}
return IsValid;

}


Can somebody help?? PLEASE stuggling for the last 1 day. Let me know if your require any more info

Thanks
Rajesh Moorthy



Responses

Posted by: Professionaluser on: 12/15/2015 [Member] [MVP] Bronze | Points: 25
Posted by: Rajnilari2015 on: 12/15/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Instead of

db.Users.FirstOrDefault(u => u.UserName == username);

try

db.Users.Where(u => u.UserName == username).FirstOrDefault();


--
Thanks & Regards,
RNA Team

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajeshmoorthy on: 12/22/2015 [Member] Starter | Points: 25

Up
0
Down
Dear RajNilari

I tried your sample but got this error!
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

I had parked this for sometime since i got fed up breaking my head.!!

regards
Rajesh

Thanks
Rajesh Moorthy

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajeshmoorthy on: 12/22/2015 [Member] Starter | Points: 25

Up
0
Down
I think I found the problem
I am isolated the models one by one and found one of the models to have some issues. Though I am not able to figure the problem.
I shall now start adding the models one by one and check which of these is causing the issue. Thanks to all for giving in their inputs. I will come back to post the issue once i figure it out!!!

regards
Rajesh

Thanks
Rajesh Moorthy

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response