Create a Web Application in ASP.Net to display all the EmpName and DeptId of the employees from the database using SQL source control and bind it to a GridView. Database fields are (DeptId, DeptName, EmpName, Salary).

Answer:

Table Name: Employees

FieldData Type
DeptIdint
DeptNamevarchar
EmpNamevarchar
Salarydecimal

Sample SQL Query to Create the Table:

CREATE TABLE Employees (
    DeptId INT,
    DeptName VARCHAR(100),
    EmpName VARCHAR(100),
    Salary DECIMAL(10, 2)
);

Sample Data:

INSERT INTO Employees (DeptId, DeptName, EmpName, Salary) VALUES (1, 'HR', 'John Doe', 50000),
       (2, 'IT', 'Jane Smith', 60000),
       (3, 'Finance', 'Robert Brown', 70000);

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EmployeeApp._Default" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Employee List</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>Employee List</h2>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" 
                DataKeyNames="DeptId" Width="50%" CssClass="gridview-style">
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Web.UI;

namespace EmployeeApp
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Bind the GridView when the page loads
            if (!IsPostBack)
            {
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
            }
        }
    }
}
Scroll to Top