Mvcsample register Form
http://www.c-sharpcorner.com/UploadFile/4d9083/creating-insert-update-and-delete-application-in-mvc-4-using/
Data Source=VIT-11-PC\SQLEXPRESS;Initial Catalog=Zee_Shop;Persist Security Info=True;User ID=sa;Password=***********
http://dotnetcodesg.com/Article/UploadFile/2/287/MVC%204%20-%20A%20Registration%20Form%20By%20Using%20DropDown%20List.aspx
CREATE PROCEDURE [dbo].[Reg_insertupdate_sp]
@Userid varchar(50)=null,
@UserName varchar(50)=null,
@Password varchar(50)=null,
@Address varchar(50)=null,
@Pincode varchar(100)=null,
@Gender varchar(50)=null,
@Hobbies varchar(100)=null,
@Country varchar(50)=null,
@Description varchar(500)=null,
@emailid varchar(50)=null,
@Phone varchar(50)=null,
@Query int
AS
Begin
if(@Query=1)
begin
insert into Ins_register(Userid,Username,Password,Address,Pincode,Gender,Hobbies,Country,Description,Emailid,Phone)values(@Userid,@UserName,@Password,@Address,@Pincode,@Gender,@Hobbies,@Country,@Description,@emailid,@Phone)
if(@@ROWCOUNT>0)
begin
select 'Insert'
end
end
if(@Query=2)
begin
update Ins_register set Username=@UserName,Password=@Password,Address=@Address,Pincode=@Pincode,Gender=@Gender,Hobbies=@Hobbies,Country=@Country,Description=@Description,Emailid=@emailid,Phone=@Phone where Userid=@Userid
select 'Update'
end
if(@Query=3)
begin
delete from Ins_register where Userid=@Userid
select 'Delete'
end
if(@Query=4)
begin
select * from Ins_register
end
end
=================
CREATE TABLE [dbo].[Ins_register] (
[Userid] VARCHAR (50) NULL,
[Username] VARCHAR (50) NULL,
[Password] VARCHAR (50) NULL,
[Address] VARCHAR (100) NULL,
[Pincode] VARCHAR (50) NULL,
[Gender] VARCHAR (50) NULL,
[Hobbies] VARCHAR (100) NULL,
[Country] VARCHAR (50) NULL,
[Description] VARCHAR (500) NULL,
[Emailid] VARCHAR (50) NULL,
[Phone] VARCHAR (50) NULL
);
============================ Models=======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MvcRegister.Models
{
public class Registerdate
{
[Required(ErrorMessage = "Please enter User id")]
public string Userid { set; get; }
[StringLength(12, MinimumLength = 3, ErrorMessage = "Mobile Name must be between 3 and 12 characters!")]
[Display(Name = "Enter User ID")]
[StringLength(12, MinimumLength = 3, ErrorMessage = "Mobile Name must be between 3 and 12 characters!")]
[Required(ErrorMessage="Please Enter username")]
public string UserName { set; get; }
[Required(ErrorMessage="Please Enter Password")]
public string Password{set;get;}
[Required(ErrorMessage="Please enter Address"]
public string Address{set;get;}
[Required(ErrorMessage="Please Enter Pincode")]
[MaxLength(6,ErrorMessage="Exceeding Limit")]
public string Pincode{set;get;}
[Required(ErrorMessage="plese select the male/female")]
public string Gender{set;get;}
[Required(ErrorMessage="Please the hobbies")]
public string Hobbies{set;get;}
[Required(ErrorMessage="please select country")]
public string Country{set;get;}
[Required(ErrorMessage="Please the description")]
public string description{set;get;}
[Required(ErrorMessage="Enter Email ID")]
[EmailAddress(ErrorMessage="invalid email address")]
public string Email{set;get;}
[Required(ErrorMessage="Enter Mobile number")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Phone number")]
public string Phone{set;get;}
}
}
control
=================dataaccess layer===========
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using MVCForm.Models;
namespace MVCForm.DataAcceessLayer
{
public class DBdata
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
public string InsertData(RegisterData Rd)
{
SqlCommand cmd = new SqlCommand("Reg_insertupdate_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Userid", Rd.Userid);
cmd.Parameters.AddWithValue("@UserName", Rd.UserName);
cmd.Parameters.AddWithValue("@Password", Rd.Password);
cmd.Parameters.AddWithValue("@Address", Rd.Address);
cmd.Parameters.AddWithValue("@Pincode", Rd.Pincode);
cmd.Parameters.AddWithValue("@Gender", Rd.Gender);
cmd.Parameters.AddWithValue("@Hobbies", Rd.Hobbies);
cmd.Parameters.AddWithValue("@Country", Rd.Country);
cmd.Parameters.AddWithValue("@Description", Rd.description);
cmd.Parameters.AddWithValue("@emailid", Rd.Email);
cmd.Parameters.AddWithValue("@Phone", Rd.Phone);
cmd.Parameters.AddWithValue("@Query", 1);
con.Open();
string result = cmd.ExecuteNonQuery().ToString();
con.Close();
return result;
}
public string Updatedata(RegisterData Rd)
{
SqlCommand cmd = new SqlCommand("Reg_insertupdate_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Userid", Rd.Userid);
cmd.Parameters.AddWithValue("@UserName", Rd.UserName);
cmd.Parameters.AddWithValue("@Password", Rd.Password);
cmd.Parameters.AddWithValue("@Address", Rd.Address);
cmd.Parameters.AddWithValue("@Pincode", Rd.Pincode);
cmd.Parameters.AddWithValue("@Gender", Rd.Gender);
cmd.Parameters.AddWithValue("@Hobbies", Rd.Hobbies);
cmd.Parameters.AddWithValue("@Country", Rd.Country);
cmd.Parameters.AddWithValue("@Description", Rd.description);
cmd.Parameters.AddWithValue("@emailid", Rd.Email);
cmd.Parameters.AddWithValue("@Phone", Rd.Phone);
cmd.Parameters.AddWithValue("@Query", 2);
con.Open();
string result = cmd.ExecuteNonQuery().ToString();
con.Close();
return result;
}
public string DeleteData(RegisterData Rd)
{
SqlCommand cmd = new SqlCommand("Reg_insertupdate_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Userid", Rd.Userid);
cmd.Parameters.AddWithValue("@Query", 3);
con.Open();
string result = cmd.ExecuteNonQuery().ToString();
con.Close();
return result;
}
public DataSet SelectData(RegisterData Rd)
{
SqlCommand cmd = new SqlCommand("Reg_insertupdate_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Query", 4);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
========================insert view=======================
@model MVCForm.Models.RegisterData
@{
ViewBag.Title = "Insertdata";
}
<h2>Insertdata</h2><table>
<tr>
<td>
@* @Html.ActionLink("Show All Mobile List", "ShowAllMobileDetails")*@
</td>
</tr>
</table>
@*@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
ViewData["result"] = null;
<script type="text/javascript" language="javascript">
alert("Data saved Successfully");
</script>
}
}*@
@using (Html.BeginForm())
{
<table>
<tr>
<td>Enter UserId</td>
<td>@Html.TextBoxFor(a=>a.Userid)</td>
<td>@Html.ValidationMessageFor(a=>a.Userid)</td>
</tr>
<tr>
<td>Enter UserName</td>
<td>@Html.TextBoxFor(a=>a.UserName)</td>
</tr>
<tr>
<td>Enter Password</td>
<td>@Html.TextBoxFor(a=>a.Password)</td>
</tr>
<tr>
<td>Enter Address</td>
<td>@Html.TextBoxFor(a=>a.Address)</td>
</tr>
<tr>
<td>Enter Pincode</td>
<td>@Html.TextBoxFor(a=>a.Pincode)</td>
</tr>
<tr>
<td>Select Gender</td>
<td>@Html.RadioButtonFor(a=>a.Gender,"Male",new{id="Male"})
Male
@Html.RadioButtonFor(a=>a.Gender,"Female",new{id="Female"})FeMale
</td>
</tr>
<tr>
<td>Select Hobbies</td>
<td>@Html.CheckBox("playing",new{id="Playing"})Playing
@Html.CheckBox("Dancing",new{id="danc"})Dancing
@Html.CheckBox("Singing",new{id="sing"})Singing
</td>
</tr>
<tr>
<td>
@Html.DropDownListFor(a => a.Country, new SelectList(
new List<Object>{
new { value = 0 , text = "Red" },
new { value = 1 , text = "Blue" },
new { value = 2 , text = "Green"}
},
"value",
"text",
2))
</td>
<tr>
<td>
Enter Description
</td>
<td><@Html.TextAreaFor(a=>a.description)</td>
</tr>
<tr>
<td>
Enter Email id
</td>
<td>@Html.TextBoxFor(a=>a.Email)</td>
<td>@Html.ValidationMessageFor(a=>a.Email)</td>
</tr>
<tr>
<td>Enter Phone</td>
<td>@Html.TextBoxFor(a=>a.Phone)</td>
<td>@Html.ValidationMessageFor(a=>a.Phone)</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit1" type="submit" value="submit" />
</td>
</tr>
</table>
}
=================== controller===================
namespace MVCForm
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "RegisterStore", action = "InsertData", id = UrlParameter.Optional }
);
}
}
}
=========================actioncontrollers===========================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCForm.Models;
using System.Data;
namespace MVCForm.Controllers
{
public class RegisterStoreController : Controller
{
//
// GET: /RegisterStore/
public ActionResult Insertdata()
{
return View();
}
[HttpPost]
public ActionResult Insertdata(RegisterData Rd)
{
if (ModelState.IsValid)
{
DataAcceessLayer.DBdata objdb = new DataAcceessLayer.DBdata();
string result = objdb.InsertData(Rd);
ViewData["result"] = result;
ModelState.Clear();
return RedirectToAction("ShowViewDetails");
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
public ActionResult ShowViewDetails(RegisterData Rd)
{
DataAcceessLayer.DBdata objdb1 = new DataAcceessLayer.DBdata();
Rd.ShowViewDetails = objdb1.SelectData();
return View(Rd);
}
public ActionResult ShowViewFullDetails(RegisterData Rd,string id)
{
DataAcceessLayer.DBdata objdb1 = new DataAcceessLayer.DBdata();
Rd.Userid = id;
Rd.ShowViewDetails = objdb1.SelectFullData(Rd);
return View(Rd);
}
public ActionResult DeleteRecord(RegisterData rd,string id)
{
DataAcceessLayer.DBdata obj = new DataAcceessLayer.DBdata();
rd.Userid = id;
string res = obj.DeleteData(rd).ToString();
return RedirectToAction("ShowViewDetails");
}
public ActionResult EditRecord(RegisterData rd, string id)
{
DataAcceessLayer.DBdata obj = new DataAcceessLayer.DBdata();
rd.Userid = id;
DataSet ds= obj.SelectFullData(rd);
rd.Userid = ds.Tables[0].Rows[0]["UserId"].ToString();
rd.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
rd.Password = ds.Tables[0].Rows[0]["Password"].ToString();
rd.Address = ds.Tables[0].Rows[0]["Address"].ToString();
rd.Pincode = ds.Tables[0].Rows[0]["Pincode"].ToString();
rd.Gender = ds.Tables[0].Rows[0]["Gender"].ToString();
rd.Hobbies = ds.Tables[0].Rows[0]["Hobbies"].ToString();
rd.Country = ds.Tables[0].Rows[0]["Country"].ToString();
rd.description = ds.Tables[0].Rows[0]["Description"].ToString();
rd.Email = ds.Tables[0].Rows[0]["emailid"].ToString();
rd.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
return View(rd);
}
[HttpPost]
public ActionResult EditRecord(RegisterData Rd)
{
DataAcceessLayer.DBdata objdb = new DataAcceessLayer.DBdata();
string result = objdb.Updatedata(Rd);
ViewData["result"] = result;
ModelState.Clear();
return RedirectToAction("ShowViewDetails");
}
}
}
========================= ShowViewDetails Page==================
@model MVCForm.Models.RegisterData
@using MVCForm.Models
@{
ViewBag.Title = "ShowViewDetails";
}
<h2>ShowViewDetails</h2>
<table>
<tr>
<td>
@Html.ActionLink("ADD NEW Reocrd", "insertData")
</td>
</tr>
</table>
@{
<table >
<tr>
<th>UserId </th><th>UserName </th><th>Country </th><th>Email </th><th>Edit </th><th>Delete </th><th>View Details </th>
</tr>
</table>
for (var i = 0; i < Model.ShowViewDetails.Tables[0].Rows.Count; i++)
{
var Userid = Model.ShowViewDetails.Tables[0].Rows[i]["Userid"].ToString();
var UserName = Model.ShowViewDetails.Tables[0].Rows[i]["UserName"].ToString();
var Country = Model.ShowViewDetails.Tables[0].Rows[i]["Country"].ToString();
var Email = Model.ShowViewDetails.Tables[0].Rows[i]["emailid"].ToString();
<table >
<tr>
<td>@Userid </td><td>@UserName</td><td>@Country</td><td>@Email</td>
<td>
@Html.ActionLink("Edit","EditRecord",new{id=Model.ShowViewDetails.Tables[0].Rows[i]["Userid"].ToString()})
</td>
<td>
@Html.ActionLink("Delete", "DeleteRecord", new { id = Model.ShowViewDetails.Tables[0].Rows[i]["Userid"].ToString() }, new { onclick="return confirm('Are you sure want delete the record')"})
</td>
<td>@Html.ActionLink("ViewDEatils", "ShowViewFullDetails", new { id = Model.ShowViewDetails.Tables[0].Rows[i]["Userid"].ToString() })</td>
</tr>
</table>
}
}
============================= ShowViewFullDetails page================
@model MVCForm.Models.RegisterData
@{
ViewBag.Title = "ShowViewFullDetails";
}
<table>
<tr>
<td>
@Html.ActionLink("ADD NEW Reocrd", "insertData")
</td>
</tr>
</table>
@{
<table>
<tr>
<th>UserId </th><th>UserName </th><th>Password </th><th>Address </th>
<th>Pincode </th><th>Gender </th><th>Hobbies </th>
<th>Country </th>
<th>Description </th><th>Email </th><th> Phone </th>
</tr>
</table>
for (var i = 0; i < Model.ShowViewDetails.Tables[0].Rows.Count; i++)
{
var Userid = Model.ShowViewDetails.Tables[0].Rows[i]["Userid"].ToString();
var UserName = Model.ShowViewDetails.Tables[0].Rows[i]["UserName"].ToString();
var Password = Model.ShowViewDetails.Tables[0].Rows[i]["Password"].ToString();
var Address = Model.ShowViewDetails.Tables[0].Rows[i]["Address"].ToString();
var Pincode = Model.ShowViewDetails.Tables[0].Rows[i]["Pincode"].ToString();
var Gender = Model.ShowViewDetails.Tables[0].Rows[i]["Gender"].ToString();
var Hobbies = Model.ShowViewDetails.Tables[0].Rows[i]["Hobbies"].ToString();
var Country = Model.ShowViewDetails.Tables[0].Rows[i]["Country"].ToString();
var Description = Model.ShowViewDetails.Tables[0].Rows[i]["Description"].ToString();
var Email = Model.ShowViewDetails.Tables[0].Rows[i]["emailid"].ToString();
var Phone = Model.ShowViewDetails.Tables[0].Rows[i]["Phone"].ToString();
<table >
<tr>
<td>@Userid </td><td>@UserName</td><td>@Password</td><td>@Address</td><td>@Pincode</td>
<td>@Gender</td> <td>@Hobbies</td><td>@Country</td>
<td>@Description</td><td>@Email</td><td>@Phone</td>
</tr>
</table>
}
}
===================== Delete ===========================
@model MVCForm.Models.RegisterData
@{
ViewBag.Title = "DeleteRecord";
}
<h2>DeleteRecord</h2>
<table>
<td>
@Html.ActionLink("Add New Reord","Insertdata")
@Html.ActionLink("ShowDetails", "ShowViewdetails");
</td>
</table>
@using (Html.BeginForm())
{
<table>
<tr>
<td>
userid:@Html.DisplayFor(a => a.Userid)
</td>
<td>
UserName:@Html.DisplayFor(a => a.UserName)
</td>
<td>
Country:@Html.DisplayFor(a => a.Country)
</td>
<td>
Email:@Html.DisplayFor(a => a.Email)
</td>
<td>
<input type="submit" value="Delete" onclick=" return confirm('Are you sure')"/>
</td>
</tr>
</table>
}
================= Update=================
@model MVCForm.Models.RegisterData
@{
ViewBag.Title = "EditRecord";
}
<h2>EditRecord</h2>
<table>
<tr>
<td>
<@Html.ActionLink("Show All Details","ShowViewDetails")
</td>
</tr>
</table>
@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
ViewData["result"] = null;
<script type="text/javascript" language="javascript">
alert("Data Updated Successfully");
</script>
}
}
@using (Html.BeginForm())
{
<table>
<tr>
<td>Enter UserId</td>
<td>@Html.TextBoxFor(a=>a.Userid)</td>
</tr>
<tr>
<td>Enter UserName</td>
<td>@Html.TextBoxFor(a=>a.UserName)</td>
</tr>
<tr>
<td>Enter Password</td>
<td>@Html.TextBoxFor(a=>a.Password)</td>
</tr>
<tr>
<td>Enter Address</td>
<td>@Html.TextBoxFor(a=>a.Address)</td>
</tr>
<tr>
<td>Enter Pincode</td>
<td>@Html.TextBoxFor(a=>a.Pincode)</td>
</tr>
<tr>
<td>Select Gender</td>
<td>@Html.RadioButtonFor(a=>a.Gender,"Male",new{id="Male"})
Male
@Html.RadioButtonFor(a=>a.Gender,"Female",new{id="Female"})FeMale
</td>
</tr>
<tr>
<td>Select Hobbies</td>
<td>@Html.RadioButtonFor(a=>a.Hobbies,"English",new{id="english"})
English
@Html.RadioButtonFor(a=>a.Hobbies,"Non English",new{id="Non english"})
NON English
</td>
</tr>
<tr>
<td>Select Country</td>
<td>
@Html.DropDownListFor(a => a.Country, new SelectList(
new List<Object>{
new { value = "India" , text = "Red" },
new { value = "USA", text = "Blue" },
new { value = "UK" , text = "Green"}
},
"value",
"text",
2))
</td></tr>
<tr>
<td>
Enter Description
</td>
<td>@Html.TextAreaFor(a=>a.description)</td>
</tr>
<tr>
<td>
Enter Email id
</td>
<td>@Html.TextBoxFor(a=>a.Email)</td>
</tr>
<tr>
<td>Enter Phone</td>
<td>@Html.TextBoxFor(a=>a.Phone)</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit2" type="submit" value="Update" />
</td>
</tr>
</table>
}
===============================