Thursday, October 31, 2019

Edit and Update Operations in Asp.net mvc using Ado.net Approach


In Index.cshtml page you can add Edit and delete buttons

So, Copy the below code in Index.cshtml page

<table>

<tr>

<th>Action</th>

</tr>

<tr>

<td>

                @Html.ActionLink("Edit", "Edit", new { id = item.EmpId })||

  @Html.ActionLink("Delete", "Delete", new { id = item.EmpId })

</td>

</tr>

</table>



Edit Operations:



Store Procedure for edit operation

USE [MvcApplicaion]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

Create PROCEDURE sp_GetEmployeeById_Warriors

@EmpId int

AS

BEGIN

                    SELECT * From EmployeeModels where EmpId=@EmpId;

END



Now we go for the EmployeeContext.cs page and copy the below code



   public EmployeeModel GetEmployeeDetailsById(int? id)

        {

            SqlCommand cmd = new SqlCommand("sp_GetEmployeeById_Warriors", con);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@EmpId", id);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            EmployeeModel emp = new EmployeeModel();

            foreach (DataRow dr in dt.Rows)

            {

                emp.EmpId = Convert.ToInt32(dr[0]);

                emp.EmpName = Convert.ToString(dr[1]);

                emp.EmpSalary = Convert.ToInt32(dr[2]);

            }

            return emp;

        }



Now we go for the EmployeeController.cs page copy the below code.



   [HttpGet]

        public ActionResult Edit(int? id)

        {

            EmployeeModel obj = db.GetEmployeeDetailsById(id);

            return View(obj);

        }



Add view For Edit Method.


Now we go for Edit.cshtml page and copy the below code



@model Asp.net_Approach.Models.EmployeeModel

@{

    ViewBag.Title = "Edit";

}



<h2>Edit</h2>

@using (Html.BeginForm())

{

    @Html.Hidden("EmpId"", Model.EmpId)

    @Html.Label("EmpName")

    @Html.TextBox("EmpName", Model.EmpName)

    <br />

    @Html.Label("EmpSalary")

    @Html.TextBox("EmpSalary", Model.EmpSalary)

    <br />

    <input type="submit" value="save" class="btn btn-success" />

}



Update Operations:



Store Procedure



USE [MvcApplicaion]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

create PROCEDURE sp_UpdateEmployeeDetails_Warriors

@EmpName Varchar(50),

@EmpSalary int,

@EmpId int

AS

BEGIN

                    update EmployeeModels

                    set

                    EmpName = @EmpName,

                    EmpSalary = @EmpSalary

                    where EmpId = @EmpId;

END



Now we go for the EmployeeContext.cs page and copy the below code



        public int UpdateEmployeeDetailsById(EmployeeModel obj)

        {

            SqlCommand cmd = new SqlCommand("sp_UpdateEmployeeDetails_Warriors", con);

            cmd.CommandType = CommandType.StoredProcedure;

            con.Open();

            cmd.Parameters.AddWithValue("@EmpId", obj.EmpId);

            cmd.Parameters.AddWithValue("@EmpName", obj.EmpName);

            cmd.Parameters.AddWithValue("@EmpSalary", obj.EmpSalary);

            object i = cmd.ExecuteNonQuery();

            int result = Convert.ToInt32(i);

            con.Close();

            return result;

        }



Now we go EmployeeController.cs page and copy the below code:



[HttpPost]

        public ActionResult Edit(EmployeeModel e)

        {

            int i = db.UpdateEmployeeDetailsById(e);

            if (i > 0)

            {

                return RedirectToAction("index");

            }

            else

            {

                return View();

            }

        }



ExecuteScalar() : only returns the value from the first column of the first row of your query.(its only return the id of which record you store that particular Empid will return).


ExecuteNonQuery() :  does not return data at all: only the number of rows affected by an insert, update, or delete.

ExecuteReader() : returns an object that can iterate over the entire result set.

No comments:

Post a Comment