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