public PartialViewResult allEmployees() { //List<Employee> emps = new List<Employee>(); //Employee emp1 = new Employee() { EmployeeId = 001, EmployeeGender = "male", EmployeeName = "Ram", DepartmentId = 1 }; //Employee emp2 = new Employee() { EmployeeId = 002, EmployeeGender = "female", EmployeeName = "Sita", DepartmentId = 2 }; //emps.Add(emp1); //emps.Add(emp2); //Employee emp = new Employee(); //if (emps.Count() != 0) //{ // var list = emps.Where(x => x.EmployeeId == 002).FirstOrDefault(); // emp = list; //} Employee empp = new Employee(); var empslist = db.GetEmployees(); foreach (Employee e in empslist) { empp = e; break; } return PartialView("_employees", empp); }
public ActionResult Create(Employee emp, string Command) { if (ModelState.IsValid) { db.SaveData(emp); TempData["Msg"] = "Data has been saved succeessfully"; return RedirectToAction("Index2"); } return View(emp); }
public IList<Employee> GetEmployees() { string strConfig = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; List<Employee> emplist = new List<Employee>(); using (SqlConnection conn = new SqlConnection(strConfig)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "SELECT [EmpId],[Name],[Gender],[DeptId] FROM [Sample].[dbo].[tblEmployee]"; cmd.CommandType = CommandType.Text; conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (rdr.Read()) { Employee emp = new Employee(); emp.EmployeeId = Convert.ToInt32(rdr["EmpId"]); emp.EmployeeName = Convert.ToString(rdr["Name"]); emp.EmployeeGender = Convert.ToString(rdr["Gender"]); emp.DepartmentId = Convert.ToInt32(rdr["DeptId"]); emplist.Add(emp); } conn.Close(); rdr = null; cmd = null; } return emplist; }
public void SaveData(Employee emp) { // define INSERT query with parameters string query = "INSERT INTO [Sample].[dbo].[tblEmployee] (Name,Gender,DeptId) " + "VALUES ('"+emp.EmployeeName+"','"+emp.EmployeeGender+"','"+emp.DepartmentId+"') "; // create connection and command string strConfig = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection cn = new SqlConnection(strConfig)) using(SqlCommand cmd = new SqlCommand(query, cn)) { // define parameters and their values cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar, 50).Value = emp.EmployeeName; cmd.Parameters.Add("@EmployeeGender", SqlDbType.VarChar, 50).Value = emp.EmployeeGender; cmd.Parameters.Add("@DepartmentId", SqlDbType.VarChar, 50).Value = emp.DepartmentId; // open connection, execute INSERT, close connection cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); } }
public void UpdateData(Employee emp) { // define INSERT query with parameters string query = "UPDATE [Sample].[dbo].[tblEmployee] SET Name='" + emp.EmployeeName + "',Gender='" + emp.EmployeeGender + "',DeptId='" + emp.DepartmentId + "' where EmpId='"+emp.EmployeeId+"'"; // create connection and command string strConfig = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection cn = new SqlConnection(strConfig)) using (SqlCommand cmd = new SqlCommand(query, cn)) { // open connection, execute Delete, close connection cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); } }
public ActionResult Edit(Employee emp) { if (ModelState.IsValid) { db.UpdateData(emp); TempData["Msg"] = "Data has been updated succeessfully"; return RedirectToAction("Index2"); } return View(emp); }