简体   繁体   中英

how to display value of column from another table

How do I display the value of column Department from tblDepartment to tblEmployee in a webform table? I have this code but it only displays the DeptID .

This is the Page_load code

        sConn = new SqlConnection(sStr);
        daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
        daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
        dsEmp = new DataSet();
        dsDep = new DataSet();

        daEmp.Fill(dsEmp, "tblEmployee");
        daDep.Fill(dsDep, "tblDepartment");

        dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };

        DataTable dt = new DataTable();

        dgvEmployee.DataSource = dsEmp.Tables["tblEmployee"];
        dgvEmployee.DataBind();

These are the tables

在此处输入图片说明

When you need to display information from two or more different tables you can join them based on relationship( primary key and foreign key ) columns:

You need to Replace this Query:

SELECT * FROM tblEmployee

With this:

SELECT employee.*,department.Department 
FROM tblEmployee employee 
    INNER JOIN tblDepartment department ON employee.DeptID=department.DeptID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM