For working in gridview you can use either the jquery or the predefined template for displaying the data in the grid.
First i will create the model class that is used to display the details in the gridview.
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 corresponding action that will call the repository method that fetch data from the database.
public ActionResult Index()
{
EmpRepObj = new EmployeeRepository();
List<EmployeeModel> EmpMod = new List<EmployeeModel>();
EmpMod = EmpRepObj.GetDetails().ToList();
return View(EmpMod);
}
Then we will create the repository in which the code is being defined for the select operation.
public List<EmployeeModel> GetDetails()
{
List<EmployeeModel> EmployeeLst = new List<EmployeeModel>();
try
{
SqlDataReader reader = null;
using (SqlConnection sqlConnection = new SqlConnection(strConnString))
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
reader = SqlHelper.ExecuteReader(sqlConnection, CommandType.StoredProcedure, "uspGetUsers");
if (reader.HasRows)
{
while (reader.Read())
{
EmployeeLst.Add(new EmployeeModel
{
EmpId = reader["EmpId"] != null ? !string.IsNullOrEmpty(Convert.ToString(reader["EmpId"])) ? Convert.ToInt32(Convert.ToString(reader["EmpId"]).Trim()) : 0 : 0,
FirstName = reader["EmpFirstName"] != null ? Convert.ToString(reader["EmpFirstName"]) : String.Empty,
MiddleName = reader["EmpMiddleName"] != null ? Convert.ToString(reader["EmpMiddleName"]) : String.Empty,
LastName = reader["EmpLastName"] != null ? Convert.ToString(reader["EmpLastName"]) : String.Empty,
PhoneNo = reader["EmpPhoneNo"] != null ? Convert.ToString(reader["EmpPhoneNo"]) : String.Empty,
Address = reader["EmpAddress"] != null ? Convert.ToString(reader["EmpAddress"]) : String.Empty,
Status = getstatus.success
});
}
}
}
}
catch (Exception ex)
{
EmployeeLst.Add(new EmployeeModel
{
msg = ex.Message,
Status = getstatus.failure
});
}
return EmployeeLst;
}
0 Comment(s)