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