DOTNET_Labs_All

CSIT .NET Lab Tasks — All Labs


Lab Task 1: Bank Account Management System (Basic)

Objective: Create a basic bank account system with the ability to perform deposits, withdrawals, and maintain a transaction history. The task will help you understand object-oriented programming concepts such as classes, properties, and methods in C#.

Instructions

1. Create a BankAccount Class:

2. Constructor:

3. Methods:

4. Properties:

5. Test the Class:


Lab Task 2: Bank Account Management System with Inheritance and Polymorphism

Objective: Extend the concepts from Lab Task 1 to enhance the system by adding multiple types of bank accounts (e.g., Savings, Checking) using Inheritance and Polymorphism. This will give you a deeper understanding of object-oriented programming (OOP) concepts in C#, such as class inheritance, method overriding, and polymorphic behavior.

Instructions

1. Extend the BankAccount Class:

2. Derived Classes:

3. Test the Bank Accounts:


Lab Task 3: Understanding Single-cast and Multi-cast Delegates

Questions

1. Define a Delegate:

2. Create Two Methods:

3. Single-cast Delegate:

4. Multi-cast Delegate:

5. Remove a Method:


Lab Task 4: Using Lambda Expressions with Action, Func, and Predicate

Objective: Practice creating and using lambda expressions with Action, Func, and Predicate delegates in C#.

Tasks

  1. Define an Action<List<string>> delegate using a lambda expression that prints each element of a given list on a new line.

  2. Define a Func<int, bool> delegate using a lambda expression that determines whether a given integer is even.

  3. Define a Predicate<int> delegate using a lambda expression that checks whether a given integer is a multiple of 5.


Lab Task 5: Working with LINQ in C# using Arrays and Collections

Objective: To practice LINQ operations using both query syntax and method syntax on arrays and generic collections such as List<T>, with a custom class Employee.

Employee Class

public class Employee
{
    public string Name { get; set; }
    public double Salary { get; set; }
    public string Department { get; set; }
    public bool IsResigned { get; set; }
    public int Experience { get; set; }
}

Given a list/array of employees, perform the following operations using both query syntax and method syntax:

Tasks

  1. Filter active employees — Retrieve only the employees who are currently working (i.e., IsResigned == false).

  2. Sort by salary descending — Return the list of employees sorted in descending order of salary.

  3. Get distinct departments — Extract a list of unique department names from the list.

  4. Check if any employee has zero experience — Return true if any employee has Experience == 0.

  5. Project only Name and Salary — Create a new list of objects that contains only the Name and Salary of each employee.


Lab Task 6: C# File I/O – Note Management System

Objective: Design and implement a C# console application named NoteManagerApp that allows the user to manage personal text-based notes. The application should store all notes inside a specific directory on the D: drive and provide options to create, read, list, copy, move, and delete notes.

Methods

  1. SetupNoteDirectory() — Create a folder named D:\NotesApp if it does not already exist.

  2. CreateNote(string title, string content) — Create a text file named <title>.txt inside NotesApp. If it exists, prompt for overwrite.

  3. ReadNote(string title) — Display the full contents of the note using File.ReadAllText.

  4. ListAllNotes() — List all note titles (i.e., file names without extensions) inside NotesApp.

  5. CopyNote(string originalTitle, string copyTitle) — Copy the contents of one note to a new file using File.Copy.

  6. MoveNote(string oldTitle, string newTitle) — Rename or move a note using File.Move.

  7. DeleteNote(string title) — Delete a note file using File.Delete. Confirm with the user before deletion.

  8. ReadNoteLineByLine(string title) — Display each line of the note with line numbers using File.ReadLines.


Objective: Learn how to set up an ASP.NET Core MVC application and understand the project structure.

Lab Instructions


Objective: Understand how to create controllers in ASP.NET Core MVC and work with different types of ActionResult return types to handle different HTTP responses such as views, redirects, JSON data, and file downloads.

Tasks

Create a new controller named ProductController.

  1. In the Index action of the ProductController, return a View that displays a list of products.

  2. Add a new action GetProductInfo in ProductController that returns product information in JSON format.

  3. Add a new action RedirectToHome in ProductController that returns a RedirectResult to the home page (i.e., Home/Index).

  4. Add a new action DownloadProductFile in ProductController that returns a file for download. You can use a simple text file or image as the content. [File]

  5. Add a new action DisplayMessage in ProductController that returns a simple string message using ContentResult.

  6. Add a new action ReturnStatusCode in ProductController that returns a custom HTTP status code (e.g., 204 No Content). [StatusCodeResult]


Objective: Understand how to use Razor syntax, apply Tag Helpers in ASP.NET Core MVC, and create Custom Tag Helpers to enhance the functionality and maintainability of views.

Task 1: Razor Syntax, ViewBag, and ViewData

Controller:

View (Index.cshtml):

Task 2: Tag Helper and Custom Tag Helper

  1. Built-in Tag Helpers:

    • Create a simple form in Index.cshtml that uses asp-for tag helpers for input fields (Name, Email).
    • Use the asp-validation-for tag helper to display validation errors for these fields.
    • Add a submit button using asp-button tag helper.
  2. Custom Tag Helper:

    • Create a custom Tag Helper DisplayDateTagHelper that takes a date parameter and formats it as "MM/dd/yyyy".
    • In the view, use the custom tag helper to display the current date using <display-date date="DateTime.Now"></display-date>.

Objective: To understand and implement Model Binding and Data Validation in ASP.NET Core MVC, including how to bind form data to models and apply validation using data annotations.

Task 1: Model Binding

  1. Create a Model:

    • Create a Person model with the following properties:
      • Name (string)
      • Email (string)
      • Age (int)
  2. Create a Controller (ModelBindingController):

    • Add an Index action to render a form for creating a Person object.
    • Add a Create action to handle the form submission and bind the form data to a Person model using Model Binding.
  3. Create a View (Index.cshtml):

    • Create a form that uses asp-for tag helpers to bind input fields for Name, Email, and Age to the Person model.
    • Use the asp-action attribute to submit the form to the Create action.

Task 2: Data Validation with Annotations

  1. Add Data Annotations to the Person Model:

    • Name: [Required], [MaxLength(50)].
    • Email: [Required], [EmailAddress].
    • Age: [Required], [Range(18, 100)].
  2. Update the Controller (Create action):

    • In the Create action, check if the model is valid using ModelState.IsValid. If it is not valid, return to the form with validation errors.
  3. Display Validation Errors in the View:

    • Use asp-validation-for tag helpers in the view to display validation messages for Name, Email, and Age.
  4. Submit the Form:

    • Test the form by submitting invalid data (e.g., empty fields, invalid email, or age out of range).
    • Ensure the validation messages are displayed when invalid data is submitted.

Objective: Use SqlConnection, SqlCommand, and SqlDataReader to retrieve and insert data into the Employee table.

Tasks

  1. Retrieve Data (Read):

    • Use SqlCommand.ExecuteReader() to select all employees where IsResigned = 0.
    • Display the Name and Salary of each employee in the console.
  2. Insert Data (Write):

    • Use SqlCommand.ExecuteNonQuery() to insert a new employee into the Employee table.
    • Use parameterized queries to pass values: Name, Salary, Department, IsResigned, and Experience.

Lab Task 12: Working with Entity Framework using DbContext

Objective: Use Entity Framework (Code First or Database First) to perform CRUD operations on the Employee table using DbContext.

Tasks

1. Setup DbContext and Entity Class

Create an Employee entity class with properties:

Create an AppDbContext class inheriting from DbContext. Add a DbSet<Employee> property named Employees.

2. Retrieve Data (Read)

3. Insert Data (Create)

4. Update Data (Update)

5. Delete Data (Delete)


Lab Task 13: Working with Session, TempData, and Cookies

Objective: Understand and implement state management in ASP.NET Core using Session, TempData, and Cookies.

Tasks

1. Session

2. TempData

3. Cookies


Lab Task 14: Authentication and Authorization (with HttpContext)

Objective: Implement manual authentication (using cookies + HttpContext) and role-based authorization without ASP.NET Core Identity.

Tasks

1. Authentication (Login/Logout with Cookies)

2. Authorization (Role-based)

3. Custom Policy Example

4. Demonstration Flow