TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
MVC CRUD Example with Visual Studio and SQL Server
Kumar Bhimsen
Dec 22
2015
Code
1.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
MVCDemo.zip
CrudController .cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
CurdMvc.Models;
using
System.Data;
using
System.Configuration;
using
System.Data.SqlClient;
namespace
CurdMvc.Controllers
{
public
class
CurdController: Controller
{
//
// GET: /Curd/
SqlConnection con =
new
SqlConnection(
"Data Source=BITS-PC;Initial Catalog=TestDB;Integrated Security=True"
);
public
ActionResult Index()
{
List < CurdModel > lstRecord =
new
List < CurdModel > ();
SqlDataReader dr =
null
;
SqlCommand command =
new
SqlCommand(
"GetAllRecordSP"
, con);
command.CommandType = CommandType.StoredProcedure;
con.Open();
dr = command.ExecuteReader();
while
(dr.Read())
{
CurdModel mdl =
new
CurdModel();
mdl.id = Convert.ToInt32(dr[
"Id"
]);
mdl.email = dr[
"Email"
].ToString();
mdl.name = dr[
"Name"
].ToString();
lstRecord.Add(mdl);
}
con.Close();
return
View(lstRecord);
}
[HttpGet]
public
ActionResult Add(
int
? id)
{
CurdModel mdl =
new
CurdModel();
if
(id !=
null
)
{
SqlCommand cmd =
new
SqlCommand(
"GetRecordByIdSP"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
"@Id"
, SqlDbType.Int).Value = id;
SqlDataReader dr =
null
;
con.Open();
dr = cmd.ExecuteReader();
DataTable dt =
new
DataTable();
dt.Load(dr);
mdl.id = Convert.ToInt32(dt.Rows[0][0].ToString());
mdl.name = dt.Rows[0][1].ToString();
mdl.email = dt.Rows[0][2].ToString();
con.Close();
return
View(mdl);
}
return
View();
}
[HttpPost]
public
ActionResult add(CurdModel model)
{
if
(model.id > 0)
{
SqlCommand command =
new
SqlCommand(
"UpdateRecordByIdSP"
, con);
command.CommandType = CommandType.StoredProcedure;
// add parameters
command.Parameters.Add(
"@Name"
, SqlDbType.VarChar).Value = model.name;
command.Parameters.Add(
"@Email"
, SqlDbType.VarChar).Value = model.email;
command.Parameters.Add(
"@Id"
, SqlDbType.Int).Value = model.id;
con.Open();
int
iRetVal = command.ExecuteNonQuery();
}
else
{
SqlCommand command =
new
SqlCommand(
"AddNewRecordSP"
, con);
command.CommandType = CommandType.StoredProcedure;
// add parameters
command.Parameters.Add(
"@Name"
, SqlDbType.VarChar).Value = model.name;
command.Parameters.Add(
"@Email"
, SqlDbType.VarChar).Value = model.email;
command.Parameters.Add(
"@Id"
, SqlDbType.Int).Direction = ParameterDirection.Output;
con.Open();
int
iRetVal = command.ExecuteNonQuery();
con.Close();
}
return
RedirectToAction(
"Index"
,
"curd"
);
}
public
ActionResult Delete(
int
id)
{
SqlCommand command =
new
SqlCommand(
"DeleteRecordByIdSP"
, con);
command.CommandType = CommandType.StoredProcedure;
// add parameters
command.Parameters.Add(
"@Id"
, SqlDbType.Int).Value = id;
con.Open();
command.ExecuteNonQuery();
con.Close();
return
RedirectToAction(
"Index"
,
"curd"
);
}
public
ActionResult Details(
int
id)
{
CurdModel mdl =
new
CurdModel();
SqlCommand cmd =
new
SqlCommand(
"GetRecordByIdSP"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
"@Id"
, SqlDbType.Int).Value = id;
SqlDataReader dr =
null
;
con.Open();
dr = cmd.ExecuteReader();
DataTable dt =
new
DataTable();
dt.Load(dr);
mdl.id = Convert.ToInt32(dt.Rows[0][0].ToString());
mdl.name = dt.Rows[0][1].ToString();
mdl.email = dt.Rows[0][2].ToString();
con.Close();
return
View(mdl);
}
}
}
MVC
SQL Server
Visual Studio