Login page in asp.net Mvc4 Web application

Login page in asp.net Mvc4 Web Application

 Introduction :  Here in this tutorial  explain how to make login page in mvc4 web application. here data can be retrieve from the Sql server database.

Step1:  First create mvc4 web application  and add the database as below image



 Step 2 : Now create Model claass and give name DAL.cs here in this claas write the query logic to fetch data from the database.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Mvc.Models
{
    public class DAL
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Chetan_program\Mvc\Mvc\App_Data\Database1.mdf;Integrated Security=True");
        public int id { get; set; }
        [Required]
        public string name { get; set; }
        [Required]
        [DataType(DataType.EmailAddress)]
        public string email { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string pwd { get; set; }

       public DataTable login()
        {
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter("select name,email,pwd from tbl_reg", con);
            adp.Fill(dt);
            return dt;
        }


Step 3 :  Now create Controller and add the following code.

 public class ABCController : Controller
    {
        //
        // GET: /ABC/
      

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(DAL da)
        {
            DataTable dt = new DataTable();
            dt = da.login(da);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["email"].ToString() == da.email && dr["pwd"].ToString() == da.pwd)
                    {
                        Session["username"] = dr["name"].ToString();
                        return RedirectToAction("profile");
                    }
                    else
                    {
                        ViewBag.Message = "Enter uEmail And Password  ?";
                    }
                }
            }
            return View();
        }

Step 4: Now add view  as following view can get data from the controller display to user browser

@model Mvc.Models.DAL

@{
    ViewBag.Title = "Login";
    Layout = "~/Content/index.cshtml";
}

<h2>Login</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>DAL</legend>


        <div class="editor-label">
            @Html.LabelFor(model => model.email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.email)
            @Html.ValidationMessageFor(model => model.email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.pwd)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.pwd)
            @Html.ValidationMessageFor(model => model.pwd)
        </div>

     
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

See Other Tutorial :

*  AngularJS Crude With ASP.NET MVC

*  Convert Rows to columns using 'Pivot' in SQL Server

*  Mvc Registration page With user exist using Ajax method

*  MVC 4 How to Perform Insert Update Delete Edit Select Operation

*  MVC4 Edit,update,Delete,cancel inside gridview using sql database

*  MVC 4 Gridview To Display Data Using SQL Server Database With Simple code 

*  Login page in asp.net Mvc4 Web application

*  Mvc4 How to bind Dropdown List using Sql Database

Gridview find control in asp.net
Previous
Next Post »