While working and manipulating the gridview we will work on the different sort of operations that needs to be performed in the gridview.
For doing this we are going to perform the update operation using the model and the action named employee.
public enum getstatus
{
success, failure
}
public class EmployeeModel
{
public int EmpId { get; set; }
[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
[Required(ErrorMessage = "PhoneNo is required")]
public string PhoneNo { get; set; }
public string Address { get; set; }
public getstatus Status { get; set; }
public string msg { get; set; }
}
Then after this we will call the action that is made for updating the details of the user.
[HttpPost]
public ActionResult UpdateUser(EmployeeModel emp)
{
try
{
// TODO: Add update logic here
EmpRepObj = new EmployeeRepository();
EmpRepObj.UpdateUser(emp);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Then at last we will create the method that will update the details of the user in the repository.
public EmployeeModel UpdateUser(EmployeeModel EmpMod)
{
try
{
int result = -1;
using (SqlConnection sqlConnection = new SqlConnection(strConnString))
{
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter("@empId", EmpMod.EmpId));
parameterList.Add(new SqlParameter("@empFirstName", EmpMod.FirstName));
parameterList.Add(new SqlParameter("@empMiddleName", EmpMod.MiddleName));
parameterList.Add(new SqlParameter("@empLastName", EmpMod.LastName));
parameterList.Add(new SqlParameter("@empPhoneNo", EmpMod.PhoneNo));
parameterList.Add(new SqlParameter("@empAddress", EmpMod.Address));
if (String.Equals(sqlConnection.State, ConnectionState.Closed))
sqlConnection.Open();
result = Convert.ToInt32(SqlHelper.ExecuteScalar(strConnString, CommandType.StoredProcedure, "uspUpdateUser", parameterList.ToArray()));
EmpMod.Status = (result > 0 ? getstatus.success : getstatus.failure);
}
}
catch (SqlException ex)
{
EmpMod.msg = ex.Message;
}
return EmpMod;
}
0 Comment(s)