How to update record from database in asp.net/mvc
How to update record from database table in asp.net/mvc?
Step 1 : Make controller method
public ActionResult UpdateCategory(int ID) // this id will pass from display page where we will call this method
{
try
{
BLL_Category obj = new BLL_Category(); // Bll class object creation
MdlCategory Category = obj.BLL_GetCategory(ID); // Bll method call this method will return model object that we will save into mdlcategory object
return View(Category); // before returning the view page you have to make get call and pass id and bind the return model object into return view
}
catch (Exception)
{
throw;
}
}
on the base of this method you have to create the view page by selecting the following option
After creating the view page you have to make the following changes in view page template
@model Pick_n_Buy.Models.MdlCategory
@{
ViewBag.Title = “UpdateCategory”;
Layout = “~/Views/Shared/_LayoutPage1.cshtml”;
}
<h2 class=”text-secondary text-center”>Update Category</h2>
@using (Html.BeginForm(“UpdateCategory”, “Admin”, FormMethod.Post, new { enctype = “multipart/form-data” })) @*// method name, controller name , method post, and encrype type *@
{
@Html.AntiForgeryToken()
<div class=”form-horizontal”>
@Html.ValidationSummary(true, “”, new { @class = “text-danger” })
@Html.HiddenFor(model => model.ID)
<div class=”form-group”>
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = “control-label col-md-2” })
<div class=”col-md-10″>
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.Name, “”, new { @class = “text-danger” })
</div>
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = “control-label col-md-2” })
<div class=”col-md-10″>
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.Description, “”, new { @class = “text-danger” })
</div>
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.Thumbnail, htmlAttributes: new { @class = “control-label col-md-2” })
<div class=”col-md-10″>
<input type=”file” name=”file” /> remove previous html code and place this file type html tag
@Html.ValidationMessageFor(model => model.Thumbnail, “”, new { @class = “text-danger” })
</div>
</div>
<div class=”form-group”>
<div class=”col-md-offset-2 col-md-10″>
<input type=”submit” value=”Update” class=”btn btn-secondary” />
</div>
</div>
</div>
}
Step 3: Defined another method in controller with the same name with HTTPPOST, where model and file will pass as a parameter
[HttpPost]
public ActionResult UpdateCategory(MdlCategory mdl, HttpPostedFileBase file)
{
try
{
var allowedextention = new[] { “.jpg”, “.Jpg”, “.jpeg”, “.png” }; // defined image extension
if (file != null)
{
var ext = Path.GetExtension(file.FileName); // get file extension
if (allowedextention.Contains(ext))
{
var filename = Path.GetFileName(file.FileName);
var path = “~/Images/Category_Image/” + filename+ext; // make full path
file.SaveAs(Server.MapPath(path)); //save image on given path
mdl.Thumbnail = path.Replace(“~/”, “/../”);
}
}
if (ModelState.IsValid) //validate if user is login or not
{
BLL_Category obj = new BLL_Category();
obj.BLL_UpdateCategory(mdl); //BLL method call
return RedirectToAction(“Category”, “Admin”); // after successful call redirect to this controller method
}
return View();
}
catch (Exception)
{
throw;
}
}
Step 3: Defined Bll method
public void BLL_UpdateCategory(MdlCategory mdl)
{
DAL_Category obj = new DAL_Category();
obj.Dal_Update_Category(mdl);
}
Step 4: Defined DAL method
public void Dal_Update_Category(MdlCategory mdl)
{
try
{
SqlParameter[] parm = new SqlParameter[4];
parm[0] = new SqlParameter(“@name”, SqlDbType.NVarChar)
{
Value = mdl.Name
};
parm[1] = new SqlParameter(“@Description”, SqlDbType.NVarChar)
{
Value = mdl.Description
};
parm[2] = new SqlParameter(“@Thumbnail”, SqlDbType.NVarChar)
{
Value = mdl.Thumbnail
};
parm[3] = new SqlParameter(“@ID“, SqlDbType.Int)
{
Value = mdl.ID
};
SqlHelper.SaveData(this._ConnString, CommandType.StoredProcedure, “SPW_Update_Category”, parm);
}
catch (Exception)
{
throw;
}
}
Step 5: Defined SQLhelper class method
public static int SaveData(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandparameters)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = commandType;
cmd.CommandText = commandText;
for (int i = 0; i < commandparameters.Length; i++)
{
cmd.Parameters.Add(commandparameters[i]);
}
cmd.Connection = con;
return cmd.ExecuteNonQuery();
}}
Step 6: Stored procedure
Create procedure [dbo].[SPW_Update_Category]
(
@ID int,
@name nvarchar(50),
@Thumbnail nvarchar(50),
@Description nvarchar(50)
)
as
begin
update Tbl_Category set Name=@name, Thumbnail=@Thumbnail, Description=@Description where ID=@ID
end