Introduction
This article explains how to retrieve values from a database using a Stored Procedure and bind the data to a Table using a MVC.
Step1:
Create the table as the fallowing
CREATE TABLE [dbo].[tbl_category] (
[Cat_id] INT IDENTITY (1, 1) NOT NULL,
[Category] NVARCHAR (MAX) NULL,
);
Step2:
Create the store procedure as the fallowing
Create proc Sp_ShowCategory
as
begin
Select * from tbl_category
end
Step3:
Create database_Access class as fallowing
public DataSet ShowCategory()
{
SqlCommand com = new SqlCommand("Sp_ShowCategory", con);
com.CommandType = CommandType.StoredProcedure;
DataSet ds = dboperation.getrecord(com);
return ds;
}
public void updateCategory(string sr,string cat)
{
SqlCommand com = new SqlCommand("Sp_updateCategory", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@sr", sr);
com.Parameters.AddWithValue("@category", cat);
dboperation.ddl(com, con);
}
public void DeleteCategory(string sr)
{
SqlCommand com = new SqlCommand("Sp_deleteCategory", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@sr", sr);
dboperation.ddl(com, con);
}
Step4:
In Controller write fallowing code
Call database access class in controller
database_Access_layer.db dblayer = new database_Access_layer.db();
Write fallowing code view method in controller
public ActionResult Show_Category()
{
DataSet ds = dblayer.ShowCategory();
ViewBag.Category = ds.Tables[0];
return View();
}
public ActionResult Show_Category()
{
DataSet ds = dblayer.ShowCategory();
ViewBag.Category = ds.Tables[0];
return View();
}
public ActionResult Edit_Categoty()
{
DataSet ds = dblayer.ShowCategorybyid(Convert.ToInt32(Request.QueryString["sr"]));
ViewBag.Category = ds.Tables[0];
return View();
}
[HttpPost]
public ActionResult Edit_Categoty(FormCollection fc)
{
dblayer.updateCategory(Convert.ToString(Request.QueryString["sr"]),fc["category"]);
TempData["msg"] = "Category updated Successfully";
return RedirectToAction("Show_Category");
}
public ActionResult Delete_Categoty(string sr)
{
dblayer.DeleteCategory(sr);
return RedirectToAction("Show_Category");
}
Step5: Show_Category.cshtml
Step6: Edit_category.cshtml
OutPut:
Thanks
No comments:
Post a Comment