Lambd expression showing error in mvc5

Posted by Klbaiju under ASP.NET MVC on 7/6/2018 | Points: 10 | Views : 410 | Status : [Member] | Replies : 2
I have created a table Login with fields uname and pword.

After creating the table I have added this table by database first method using entity frame work

this is my view

@model MVCLoginExample.login

@{
ViewBag.Title = "StrongtypeLogin";
}

<h2>StrongtypeLogin</h2>
@using (Html.BeginForm("StrongtypeLogin", "login", FormMethod.Post))
{
<table>
<tr>
<td>Enter UserName: </td>
<td>@Html.TextBoxFor(m => m.uname)</td>
</tr>
<tr>
<td>Enter Password: </td>
<td>@Html.TextBoxFor(m => m.pword)</td>
</tr>

<tr>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
}

this is my code in controller

public class LoginController : Controller
{
//
// GET: /Login/

ExerciseEntities db = new ExerciseEntities();
public ActionResult Index()
{
return View();
}
public ActionResult Welcome()
{

return View();
}

public ActionResult StrongtypeLogin()
{

return View();

}
[HttpPost]
public ActionResult StrongtypeLogin(login objlogin)
{

var muser = db.logins.FirstOrDefault(m => m.uname == objlogin.uname && m.pword == objlogin.pword);
if (muser != null)
{

Session["uname"] = objlogin.uname;
return RedirectToAction("welcome");

}

return View();
}
}

this is not working muser showing as null.

when I changed the lambda expression with linq it is working fine

following is linq

var muser =from p in db.logins where p.uname==objlogin.uname && p.pword==objlogin.pword
select p;

var muser = db.logins.FirstOrDefault(m => m.uname == objlogin.uname && m.pword == objlogin.pword);

this code showing null.

I also want to know how to add a try catch in this query

how to solve this

Regards

Baiju




Responses

Posted by: Sheonarayan on: 7/8/2018 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Please use CODE block to format your code. Many a times even if a member wants to reply to your queries he/she may not feel comfortable replying as they feel difficulty in understanding the code as text. Keeping the code inside block help them understand easily and quickly.

var muser = db.logins.FirstOrDefault(m => m.uname == objlogin.uname && m.pword == objlogin.pword);

correct
var muser = db.logins.Where(m => m.uname == objlogin.uname && m.pword == objlogin.pword).FirstOrDefault();


You can also try m.uname.Equals method instead of using '=='.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Posted by: Allemahesh on: 7/11/2018 [Member] [MVP] Silver | Points: 25

Up
0
Down
Just try....

var muser = db.logins.ToList().FirstOrDefault(m => m.uname == objlogin.uname && m.pword == objlogin.pword);


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

Login to post response