In this article of ASP.NET MVC we will learn about login functionality using ADO.NET so let's start.
Firstly create Model Class,in my case i create class namely called Employee and hit the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignIn_SignUp_Using_AdoDotNet.Models
{
public class Employee
{
public int id { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
}
}
Then create a controller and hit the code below:
using SignIn_SignUp_Using_AdoDotNet.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using System.Web.DynamicData;
using SignIn_SignUp_Using_AdoDotNet.Models;
namespace SignIn_SignUp_Using_AdoDotNet.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Employee emp)
{
string strCon = "Data Source=DESKTOP-UTTQQQ1;Initial Catalog=Login;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand cmd = new SqlCommand("select * from dboLogin where Username = @username and Password = @password",con);
cmd.Parameters.AddWithValue("@username", emp.UserName);
cmd.Parameters.AddWithValue("@password", emp.UserPassword);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Session["Username"] = emp.UserName.ToString();
return RedirectToAction("Dashboard");
}
else
{
ViewBag.msg = "Error";
return RedirectToAction("Index");
}
}
public ActionResult Dashboard()
{
return View();
}
}
}
Finally i have two views ( Login and Dashboard) :
Login.cshtml:
@model SignIn_SignUp_Using_AdoDotNet.Models.Employee
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserPassword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserPassword, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Dashboard.cshtml :
@{
ViewBag.Title = "Dashboard";
}
<h2>Dashboard</h2>
@if (Session["Username"]!=null)
{
<h3>@Session["Username"].ToString()</h3>
}
else
{
@ViewBag.msg
}
Comments
Post a Comment