Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create a web form with a drop down list containing the department ids. As soon as the user selects a particular Department id the employees of the department should be fetched from the database and listed in a tabular format
Posted
Comments
Peter Leow 8-Mar-14 1:06am    
Is there a question? What have you tried?

1 solution

Hi,
Here is the solution for your question:

Create 2 table in sql server
1-> Table for Department named tblDepartment habing fields a)DeptId int
b)DeptName nvarchar(30)
2->Table for employees named tblEmployes having fields a)id int,
b)EmployeeId nvarchar(30),
c)DeptId int,
D)Employee Name etc add field name as per your requirement.

Note: What ever the value of DeptId in tblDepartment should be same for DeptId in tblEmployes values.

ASPX code:

ASP.NET
<pre><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlDepartment" runat="server"  AutoPostBack="True" 
            onselectedindexchanged="ddlDepartment_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:GridView ID="gvEmployes" runat="server" AutoGenerateColumns="false">
        </asp:GridView>
    </div>
    </form>
</body>
</html>



Code Behind:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDepartmentIds();
        }
    }
    private void BindDepartmentIds()
    {
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tblDepartment",con);
            DataSet dsDepartment = new DataSet();
            da.Fill(dsDepartment);
            ddlDepartment.DataTextField = "DeptId";
            ddlDepartment.SelectedValue = "DeptId";
            ddlDepartment.DataSource = dsDepartment;
            ddlDepartment.DataBind();
        }
        catch { }
        finally { con.Close(); }
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tblEmployes where deptid='"+ddlDepartment.SelectedValue.ToString()+"'",con);
            DataSet dsEmployees = new DataSet();
            da.Fill(dsEmployees);
            gvEmployes.DataSource = dsEmployees;
            gvEmployes.DataBind();
        }
        catch { }
        finally { con.Close(); }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900