In this article we will learn how to perform Crude operation Select Insert, Update And Delete With ASP.NET MVC
Before start creating application let’s have a look on database part first. We are going to use user_master table for performing CRUD Operation. Here is user_master table view
Step 1 : Create the following database with sql server and ive table name user_master
Step 2 : Now create mvc web application and add class DAL.cs ,In this dal.cs class we have defined modal attribute for data access layer,this modal class can be used for passing modal data from controller to view and view to controller.
public class DAL
{
public int id { get; set; }
public string name { get; set; }
public string hoby { get; set; }
public string gender { get; set; }SqlConnection con = new SqlConnection(@”Data Source=(LocalDB)\v11.0;AttachDbFilename=
\MVC_Crude\App_Data\Database1.mdf;Integrated Security=True“);public void insert_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand(“insert into user_master values
(‘” + da.name + “‘,'” + da.hoby + “‘,'” + da.gender + “‘)”, con);
cmd.ExecuteNonQuery();
con.Close();
}
public List<DAL> Get_data()
{
List<DAL> D = new List<DAL>();
con.Open();
SqlCommand cmd = new SqlCommand(“select * from user_master“, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DAL da = new DAL();
da.id = Convert.ToInt32(dr[“Id“].ToString());
da.name = dr[“name“].ToString();
da.hoby = dr[“hoby“].ToString();
da.gender = dr[“gender“].ToString();
D.Add(da);
}
con.Close();
return D;
}
public List<DAL> edit_data(int id)
{
List<DAL> D = new List<DAL>();
con.Open();
SqlCommand cmd = new SqlCommand(“select * from user_master where Id='” + id + “‘”, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DAL da = new DAL();
da.id = Convert.ToInt32(dr[“Id“].ToString());
da.name = dr[“name“].ToString();
da.hoby = dr[“hoby“].ToString();
da.gender = dr[“gender”].ToString();
D.Add(da);
}
con.Close();
return D;
}public void update_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand(“update user_master set name='” + da.name + “‘,
hoby='” + da.hoby + “‘,gender='” + da.gender + “‘ where Id='” + da.id + “‘”, con);
cmd.ExecuteNonQuery();
con.Close();}
public void delete_data(DAL da)
{con.Open();
SqlCommand cmd = new SqlCommand(“delete from user_master where Id='” + da.id + “‘”, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
Step 4 : Now Create controller ABCcontroller, First request comes from browser and appropriate controller can be initialize with invoking appropriate action method of controller,In this controller we have define action method of insert,update, delete with accordance of view-result as below.
public class ABCController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Insert()
{
return View();
}
[HttpPost]
public ActionResult Insert(DAL da)
{
da.insert_data(da);
ViewBag.Message = “Data Inserted Succesfully………“;
return View();
}
[HttpPost]
public ActionResult Select()
{
return View();
}
[HttpGet]
public ActionResult Select(DAL da)
{
// da.Get_data(da);
return View(da.Get_data());
}
[HttpGet]
public ActionResult Edit(int id)
{
DAL da = new DAL();
return View(da.edit_data(id).FirstOrDefault());
}
[HttpPost]
public ActionResult Edit(DAL da)
{
da.update_data(da);
return View(da);
}
[HttpGet]
public ActionResult Delete(int id)
{
DAL da = new DAL();
return View(da.edit_data(id).FirstOrDefault());
}
[HttpPost]
public ActionResult Delete(DAL da)
{
da.delete_data(da);
return View();
}
Step 5 : Now Create view for above action method : Insert.cshtml, Select.cshtml, Edit.cshtml,
MVC_Crude.Models.DAL
@{
ViewBag.Title = “Insert“;
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.ValidationMessageFor(model => model.name)
new SelectListItem(){Text=”Amreli“,Value=”Amreli“} ,
new SelectListItem(){Text=”Suart“,Value=”Surat“} },”–select–“)
@Html.ValidationMessageFor(model => model.hoby)
@Html.RadioButtonFor(model => model.gender,”Female“,false)Female
@Html.ValidationMessageFor(model => model.gender)
@section Scripts {
@Scripts.Render(“~/bundles/jqueryval“)
}
Step 6 : Run Mvc web project and Perform insert,update delete crude operation
Crude operation Screenshot :
SEE MORE
- Auto Refresh Partial View in ASP.NET MVC
- What is ASP.NET Core
- Difference between TempData keep() And Peek() in Asp.Net MVC
- Difference between viewbag,viewdata and tempdata in asp.net mvc
- ASP.NET MVC With AngularJS
- Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC
- MVC CRUD Operations Using Entity Framework
- Search Functionality in ASP.NET MVC
- How to create a User Registration page using asp.net mvc
- Store Multiple Checkbox state from cookie using Jquery
- Cascading Dropdownlist using Ajax in Asp.Net Mvc with city state country
- Insert, Update, Delete In GridView Using ASP.Net C#
- Binding Dropdownlist With Database In Asp.Net MVC
- Search and Filter data in Gridview using Asp.net MVC
- Select Insert, Update And Delete With ASP.NET MVC
- Display Data in GridView Using Asp.net MVC
- Validation in ASP.NET MVC Razor view
- CRUD Operation Using 3-Tier Architecture In ASP.NET
- How to get Connection String from Web.Config in Asp.Net C#
- Login page using 3-Tier Architecture in ASP.Net
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database
27 Comments