Hellow Friends,
Now I am going
to describe how to create your Simple Register
form, Login form and How to make CRUD Operation which is very interesting
concept in asp.net using C# with mvc4
platform .It is very intresting concept in mvc.So Please Follow the steps.
Step-1
Step-2
Create a store procedure which name is sp_insertpro:
create Procedure sp_insertpro
(
@userid int,
@name varchar(50)=null,
@emailid varchar(50)=null,
@address varchar(50)=null
)
AS
Begin
set nocount on
begin
Insert Into userinfo(Name,emailid,password,Address)
values(@name,@emailid,@password, @address)
end
End
Step-3
Add your
connection string in web.config file:
<connectionStrings>
<add name="ConnString" connectionString="Data Source=xyz;Initial Catalog=rabi;Integrated
Security=True;Pooling=False" />
</connectionStrings>
Open your
visualstudeo2010 go to file menu then create new project and select web option in visual c# from Installed
template in left side of your screen and provide name mvcapplication click on ok button.
Step-5
Select Internet Application template from select a
template,select Razor from view engine and click on create a unit test project
check box than click on ok .
Step-6
Now go to
your ,select model folder and right
click here àAddàclass and provide name user then click on Add button:
Shown in
figure below:
Step-7
Write the
code for model operation for register:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace mvcapplication.Models
{
public class user
{
[Key] //Primary Key
public int userid { get; set; }
[Required(ErrorMessage = "Name
is mandatory")]
[Display(Name = "Name")]
public string Name { get; set; }
[Required(ErrorMessage = "emailid
is mandatory")]
[Display(Name = "emailid")]
public string emailid
{ get; set; }
[Required(ErrorMessage = "password
is mandatory")]
[Display(Name = "password")]
public string
password { get; set;
}
[Required(ErrorMessage = "Address
is mandatory")]
[Display(Name = "Address")]
public string Address
{ get; set; }
public bool Insert(user u)
{
string ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
SqlConnection conn = new
SqlConnection(ConnString);
conn.Open();
SqlCommand cmd = new
SqlCommand("sp_insertpro",
conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@name",
System.Data.SqlDbType.VarChar, 50).Value =
u.Name;
cmd.Parameters.Add("@emailid", System.Data.SqlDbType.VarChar, 50).Value = u.emailid;
cmd.Parameters.Add("@password",
System.Data.SqlDbType.VarChar, 50).Value =
u.password;
cmd.Parameters.Add("@address",
System.Data.SqlDbType.VarChar, 50).Value =
u.Address;
Object obj = cmd.ExecuteNonQuery();
return true;
}
}
}
Step-8
Create anather
model and provide name Login like above procedure and write this code for login
application:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace mvcapplication.Models
{
public class Login
{
[Required(ErrorMessage = "emailid
is mandatory")]
[Display(Name = "emailid")]
public string emailid
{ get; set; }
[Required(ErrorMessage = "password
is mandatory")]
[Display(Name = "password")]
public string
password { get; set;
}
public bool login(string emailid, string
password)
{
string ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
SqlConnection conn = new
SqlConnection(ConnString);
conn.Open();
SqlCommand cmd = new
SqlCommand("Select
* from userinfo where emailid='" + emailid + "' and password ='" + password + "'", conn);
Object obj = cmd.ExecuteScalar();
if (obj == null)
{
return false;
}
else
{
return true;
}
}
}
}
Step-9
Now go to
your solution explore ,select controller folder and right click here àAddà controller and select empty mvc
controller from scaffolding option in
Template ,provide name HomeController click
on ok button.
Like this:
Step-10:
Write the
business logic code in homecontroller for controller operation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvcapplication.Models;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace mvcapplication.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
//List<user> uList =context.u.ToList();
ViewData["Message"] = "";
return View();
}
[HttpPost]
public ActionResult
Index(user u)
{
ViewData["Message"] = "";
return View();
}
[HttpGet]
public ActionResult
Regdview()
{
//List<user> uList =context.u.ToList();
ViewData["Message"] = "";
return View();
}
[HttpPost]
public ActionResult
Regdview(user u)
{
if (ModelState.IsValid)
{
u.Insert(u);
ViewData["Message"] = "Your Record inserted sucessfully..";
return View();
}
else
{
ViewData["Message"] = "";
return View();
}
}
[HttpGet]
public ActionResult
login()
{
ViewData["Message"] = "";
return View();
}
[HttpPost]
public ActionResult
login(Login u)
{
if (ModelState.IsValid)
{
if (u.login(u.emailid, u.password))
{
return RedirectToAction("Index", "CRUD");
}
else
{
ViewBag.Message = "Invalid Username or
password";
return View();
}
}
else
{
return View();
}
}
}
}
Step-11
Goto your
home controller double click on Index ->right click->addview like this.
Shown in
figure below.
Then click on strongly typed view check box select
->select user (mvcapplication.Models) from model class drop down list..
Step-12
Write this code in side Indexview
@model mvcapplication.Models.user
@{
Layout =
null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width"
/>
<title>Welcome to our web site......</title>
</head>
<body>
<h2>welcome to our home page...</h2>
<div>
<input id="btnsignin"
type="button"
value="Sign
In" onclick="@("window.location.href='" +
@Url.Action("login", "Home") + "'");"
/>
<input id="btnsignup"
type="button"
value="Sign
Up" onclick="@("window.location.href='" +
@Url.Action("Regdview", "Home") + "'");"
/>
</div>
</body>
</html>
Step-13
Double click over Regdview(user u) and create view above procedure like this:
Write this code:
@model mvcapplication.Models.user
@{
Layout =
null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width"
/>
<title>Regdview</title>
</head>
<body>
<div>
@* @using (Html.BeginForm("Index", "Home",
FormMethod.Post, new { id = "FormName" }))*@
@using
(Html.BeginForm("Regdview", "Home", FormMethod.Post,
new { id = "FormName"
}))
{
<div>
<input id="btnlogin"
type="button"
value="login"
onclick="@("window.location.href='" +
@Url.Action("login", "Home") + "'");"
/>
</div>
<fieldset style="width: 400px;">
<legend>Student Registration Form</legend>
<div>
@ViewData["Message"]
</div>
<div>
@Html.LabelFor(m
=> m.name)
</div>
<div>
@Html.TextBoxFor(m
=> m.name, new { id = "txtName" })
@Html.ValidationMessageFor(m
=> m.name)
</div>
<div>
@Html.LabelFor(m
=> m.emailid)
</div>
<div>
@Html.TextBoxFor(m
=> m.emailid, new { id = "txtemailid" })
@Html.ValidationMessageFor(m
=> m.emailid)
</div>
<div>
@Html.LabelFor(m
=> m.password)
</div>
<div>
@Html.TextBoxFor(m
=> m.password, new { id = "txtpassword" })
@Html.ValidationMessageFor(m
=> m.password)
</div>
<div>
@Html.LabelFor(m
=> m.Address)
</div>
<div>
@Html.TextBoxFor(m
=> m.Address, new { id = "txtAddress" })
@Html.ValidationMessageFor(m
=> m.Address)
</div>
<div>
<input type="submit" title="Submit" id="btnSubmit" />
@*<input type="button"
title="Reset" id="btnReset" value="Reset"/>*@
</div>
</fieldset>
}
</div>
</body>
</html>
Step-14
Double click over login(Login
u) and create view above procedure like this:
Write code in side login view :
@model mvcapplication.Models.user
@{
Layout =
null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width"
/>
<title>login</title>
</head>
<body>
<div >
@using
(Html.BeginForm("login", "Home", FormMethod.Post))
{
<fieldset style="max-width:500px;">
<legend>Student Login Form</legend>
<div>
<div>
emailid
</div>
<div>
@Html.TextBoxFor(m => m.emailid)
@Html.ValidationMessageFor(m =>
m.emailid)
</div>
</div>
<div>
<div>
password
</div>
<div>
@Html.PasswordFor(m => m.password)
@Html.ValidationMessageFor(m =>
m.password)
@ViewBag.Message
</div>
</div>
<div>
<input
type="submit"
title="Submit"
/>
</div>
</fieldset>
}
</div>
</body>
</html>
Step-15
Go to your solution explore right click here
select Add->new item.
Shown in figure below
Step-16
Select Data from installed template of left side
then click on ADO.NET Entity Data Model and provide name EDM.edmx. and click on Add button.
Shown in figure below
Step-17
Select generate from database and click on next
button.
Shown in figure below.
Step-18
Select database connection and provide name
suppose rabicrud_entity and click on next button.
Shown in figure
below
Step-19
Select table (userifo)
and provide model name rabimodel and click on finish
button:
Shown in figure
below
Step-20
After click on finish
EDM.edmx page create like this,shown in figure below.
Step-21
Now you should build your application in
Build->Build Solution process.
After building
your application go to your Solution Explore create a anather controller and
provide name CRUDController for CRUD operation and select MVCController
with read/write actions and views,using Entity Framework
option in Template field dropdownlist,then select userinfo (mvcapplication) in
model class drop downlist and select rabicrud_entity
(mvcapplication)
in data context drop downlist which provided at step-17 and
click on add button.shown in figure below.
Now it automatic create code as well as page
inside your application.
I have explained Register,login and CRUD operations of asp.net in c# using store
procedure with mvc4 application.
So after read please Comments and
suggestions.
Thanks & Regard
Rabi
No comments:
Post a Comment