Update specific column of List using Entity Framework in ASP.Net MVC

Last Reply one month ago By dharmendr

Posted one month ago

After joinning the joined column values are adding like 

EMPID   EMPNAME  AGE DEPNAME

1          SANVI      23    SWA

2          SAI          23    AWS

But for me its adding like 

EMPID   EMPNAME AGE DEPNAME

1           SANVI      23    0

2           SAI         23     0

0           0             0       SWA

0            0            0       AWS

Its adding into the new rows.

Can You please solve me in this issue.

When I try to put Update in place of Add in below code its not working . 

        public ActionResult AddEmp()
        {
            EmployeeEntities3 entities = new EmployeeEntities3();
            List<Emp> employees = entities.Emps.ToList();
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "Select [Emp].EMPID,[Emp].EMPNAME,[Emp].AGE,[Department].DEPNAME from Emp INNER JOIN Department ON [Emp].[DEPID]=[Department].[DEPID]";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        if (sdr.HasRows)
                        {
                            while (sdr.Read())
                            {
                                employees.Add(new Emp
                                {
                                    // DEPID = System.Convert.ToInt32(sdr["DEPID"]),
                                    DEPNAME = sdr["DEPNAME"].ToString()
                                });

                            }
                        }
                    }
                    con.Close();
                }
            }

            if (employees.Count == 0)
            {
                employees.Add(new Emp());
            }

            List<Department> departments = GetDepartmentList();
            List<SelectListItem> items = new List<SelectListItem>();

            foreach (Department d in departments)
            {
                items.Add(new SelectListItem
                {
                    Text = d.DEPNAME,
                    Value = d.DEPID.ToString()
                });
            }

            var tuple = new Tuple<List<SelectListItem>, List<Emp>>(items, employees);

            return View(tuple);
        }

 

                        if (sdr.HasRows)
                        {
                            while (sdr.Read())
                            {
                                employees.Add(new Emp
                                {
                                    // DEPID = System.Convert.ToInt32(sdr["DEPID"]),
                                    DEPNAME = sdr["DEPNAME"].ToString()
                                });
                            }
You are viewing reply posted by: dharmendr one month ago.
Posted one month ago Modified on one month ago

HiPrathyusha,

Use below code.

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult AddEmp()
    {
        EmployeeEntities entities = new EmployeeEntities();
        List<Emp> employees = entities.Emps.ToList();

        List<Department> departments = GetDepartmentList();
        foreach (Emp emp in employees)
        {
            //Updating the department based on department id.
            emp.DEPID = departments.Where(x => x.DEPID == Convert.ToInt32(emp.DEPID)).FirstOrDefault().DEPNAME;
        }

        if (employees.Count == 0)
        {
            employees.Add(new Emp());
        }
        List<SelectListItem> items = new List<SelectListItem>();

        foreach (Department d in departments)
        {
            items.Add(new SelectListItem
            {
                Text = d.DEPNAME,
                Value = d.DEPID.ToString()
            });
        }

        var tuple = new Tuple<List<SelectListItem>, List<Emp>>(items, employees);

        return View(tuple);
    }

    private static List<Department> GetDepartmentList()
    {
        List<Department> dep = new List<Department>();
        dep.Add(new Department { DEPID = 1, DEPNAME = "SWA" });
        dep.Add(new Department { DEPID = 2, DEPNAME = "AWS" });

        return dep;
    }

    public class Department
    {
        public int DEPID { get; set; }
        public string DEPNAME { get; set; }
    }
}