Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Insert in MVC Gridview

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 40
    Comment on it

    While binding records or details with the gridview you need to perform several operations into it.

     

    One of these operation is to insert the details for the model which we have made the action and the view.

     

    For doing that the primary step is to create the model for it.

     

     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; }
        }

     

     

    After this we will create the action which will be of POST type for sending and saving detail.
     

     [HttpPost]
            public ActionResult RegisterEmployeeDetails(EmployeeModel EmpModel)
            {
                EmpModObj = new EmployeeModel();
                try
                {
                    if (ModelState.IsValid)
                    {
                        EmpRepObj = new EmployeeRepository();
                        EmpRepObj.SaveDetails(EmpModel);
                    }
                    else
                    {
                        return View("RegisterEmployee");
                    }
                }
                catch (Exception ex)
                {
                    EmpModObj.msg = ex.Message;
                }
                return View(EmpModel);
            }
    

     

     

    After this we will create the method in the repository for saving details of the employee.

     

     public EmployeeModel SaveDetails(EmployeeModel EmployeeModel)
            {
                try
                {
                    using (SqlConnection sqlConnection = new SqlConnection(strConnString))
                    {
                        List<SqlParameter> parameterList = new List<SqlParameter>();
                        parameterList.Add(new SqlParameter("@empFirstName", EmployeeModel.FirstName));
                        parameterList.Add(new SqlParameter("@empMiddleName", EmployeeModel.MiddleName));
                        parameterList.Add(new SqlParameter("@empLastName", EmployeeModel.LastName));
                        parameterList.Add(new SqlParameter("@empPhoneNo", EmployeeModel.PhoneNo));
                        parameterList.Add(new SqlParameter("@empAddress", EmployeeModel.Address));
                        if (String.Equals(sqlConnection.State, ConnectionState.Closed))
                            sqlConnection.Open();
                        EmployeeModel.EmpId = Convert.ToInt32(SqlHelper.ExecuteScalar(strConnString, CommandType.StoredProcedure, "uspRegisterEmployee", parameterList.ToArray()));
                        EmployeeModel.Status = (EmployeeModel.EmpId > 0 ? getstatus.success : getstatus.failure);
                    }
                }
                catch (SqlException ex)
                {
                    EmployeeModel.msg = ex.Message;
                }
                return EmployeeModel;
            }

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: