Select Insert, Update And Delete With ASP.NET MVC

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

Select Insert, Update And Delete With ASP.NET MVC

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.LabelFor(model => model.name)
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
@Html.LabelFor(model => model.hoby)
@Html.DropDownListFor(model => model.hoby,new SelectListItem[] {
new SelectListItem(){Text=”Amreli“,Value=”Amreli“} ,
new SelectListItem(){Text=”Suart“,Value=”Surat“} },”–select–“)
@Html.ValidationMessageFor(model => model.hoby)
@Html.LabelFor(model => model.gender)
@Html.RadioButtonFor(model => model.gender,”Male“,true)Male
@Html.RadioButtonFor(model => model.gender,”Female“,false)Female
@Html.ValidationMessageFor(model => model.gender)
@ViewBag.Message

@Html.ActionLink(“Back to List“, “Select“)

@section Scripts {
@Scripts.Render(“~/bundles/jqueryval“)
}

Step 6 : Run Mvc web project and Perform insert,update delete crude operation

Select Insert, Update And Delete With ASP.NET MVC

 

Crude operation Screenshot :

Select Insert, Update And Delete With ASP.NET MVC

SEE MORE

25 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *