Dot Net Lokendra
Dot Net Lokendra
Dot Net Lokendra
using System;
class Class1{
public virtual void Display(){
Console.WriteLine("This is the display method of Class 1");
}
}
class Class2: Class1{
public override void Display(){
Console.WriteLine("This is the display method of Class 2");
}
}
class Program
{
static void Main()
{
Class2 obj2 = new Class2();
obj2.Display();
Console.ReadKey();
}
}
Output:
1
LAB 2.
Write a program to handle exception when User put character in price field.
using System;
class Program
{
static void Main(string[] args)
{
int price = 0;
try
{
Console.Write("Enter the price: ");
price = Convert.ToInt32(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine("Invalid input format. Please enter a valid price. ");
Console.WriteLine($"Error Message:{e.Message}");
}
finally{
Console.WriteLine($"The value for price is {price} "); }
}
}
Output:
2
LAB 3
Write a program to display student list filter by department Id using Lambda
expression. Student has attributes(Id, DepartmentId, Name and Address) and
take any number of students.
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public int Id { get; set; }
public int DepartmentId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student { Id = 102, DepartmentId = 301, Name = "Aaron", Address = "Thamel" },
new Student { Id = 32, DepartmentId = 702, Name = "Steven", Address = "Lainchaur"
},
new Student { Id = 35, DepartmentId = 121, Name = "Maxwell", Address = "Lazimpat"
},
new Student { Id = 44, DepartmentId = 193, Name = "David", Address = "Sorhakhutte"
}
};
3
Output:
LAB 4
Write a program to validate the Login form when user submit empty value
using JQuery.
<!DOCTYPE html>
<html>
<head>
<title>Login Form Validation using JQuery</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$("#login-form").submit(function(event) {
var username = $("#username").val();
var password = $("#password").val();
if (username === "" ) {
event.preventDefault();
$("#username_msg").text("Username cannot be empty"); }
if(password === ""){
event.preventDefault();
$("#password_msg").text("Password cannot be empty"); }
});
});
</script>
4
</head>
<body>
<fieldset style="text-align: center; ">
<legend>Login Form</legend>
<form id="login-form" action="#" method="post">
<input type="text" id="username" placeholder = "Enter Username">
<p id = "username_msg" style="color: red;"> </p>
<input type="password" id="password" placeholder="Enter Password">
</html>
Output:
5
LAB 5
Write a program to get the list of products in json format using ASP.NET Web
API.
using Microsoft.AspNetCore.Mvc;
namespace WebApplication2.Controllers
{
[ApiController]
[Route("[controller]")]
public class ProductsController : ControllerBase
{
private static List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "Product 1", Price = 10.99M },
new Product { Id = 2, Name = "Product 2", Price = 20.49M },
};
[HttpGet(Name = "GetProduts")]
public IActionResult GetProducts()
{
return Ok(products); //be default, "Ok" method returns in JSON format.
}
}
}
6
Output:
LAB 6
Write a program to program to validate Player Information when click on
save button using MVC pattern.
[Range(10, 20)]
public string Name { get; set; }
[Required]
public int Age { get; set; }
}
}
Step 3: Add following code in privacy.cshtml which will be our form page
@model PlayerValidation.Models.Player
<h1>Player Validation</h1>
7
<p>Form for Player</p>
@using (Html.BeginForm())
{
Output:
8
LAB 7
using System;
using System.Collections.Generic;
// Authorization service
class AuthorizationService
{
private Dictionary<string, UserRole> userRoles = new Dictionary<string, UserRole>();
class Program
{
static void Main(string[] args)
{
AuthorizationService authorizationService = new AuthorizationService();
9
// Create users
User adminUser = new User { Username = "admin", Role = UserRole.Admin };
User modUser = new User { Username = "moderator", Role = UserRole.Moderator };
User regUser = new User { Username = "user123", Role = UserRole.User };
Output:
10
LAB 8
Write a program to store User login information for 5 days using Cookie
Solution:
Certainly! Here's an example of how you can create a simple ASP.NET Core MVC application to
store user login information for 5 days using cookies:
using Microsoft.AspNetCore.Authentication.Cookies;
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
});
_signInManager = signInManager;
[HttpGet]
11
return View();
[HttpPost]
if (ModelState.IsValid)
if (result.Succeeded)
return View(model);
await _signInManager.SignOutAsync();
[Authorize]
12
}
@if (User.Identity.IsAuthenticated)
<p>Welcome, @User.Identity.Name!</p>
LAB 9
Write a crud operation to display, insert, update and delete Student
information using ASP.NET CORE MVC.
Solution
Creating a complete CRUD (Create, Read, Update, Delete) application in ASP.NET Core MVC
involves multiple steps and requires creating controllers, views, models, and setting up database
access. CRUD operations for displaying, inserting, updating, and deleting Student information in
an ASP.NET Core MVC application.
13
public string Name { get; set; }
using Microsoft.EntityFrameworkCore;
: base(options)
Generate controllers and views using scaffolding. Open the terminal and navigate to your project
folder:
app.UseEndpoints(endpoints =>
endpoints.MapControllerRoute(
14
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
In the terminal, run the following commands to perform migrations and create the database:
15