Ese 1
Ese 1
Q.1) Write a program to handle the Mouse Click Event using C# and explain the concept
of event handling ?
—-> Exception handling is a crucial aspect of error management in C# programming. It enables
developers to anticipate and gracefully handle unexpected situations that may arise during program
execution. Core Exception Classes: 1) System.Exception: The root class of all exceptions in C# . 2)
System.ApplicationException 3)
System.SystemException.Specific Exception Classes: 1) ArgumentException: Thrown when an invalid
argument is passed to a method or constructor. 2) FormatException. 3) IndexOutOfRangeException. 4)
NullReferenceException. 5)
FileNotFoundException. 6) IOException. 7) OutOfMemoryException. Handling Exceptions with try-catch
Blocks: The primary mechanism for handling exceptions in C# is the try-catch block. The try block
encloses the code that may throw an exception, while the catch block specifies the code to execute when
an exception occurs. Syntax :
try
{ // Code that may throw an exception} catch (ExceptionType1 exceptionVariable1) { // Handle
exceptionType1} catch (ExceptionType2 exceptionVariable2)
{ // Handle exceptionType2}
finally
{ // Code that always executes, regardless of exceptions}
Example : try
{
int[] numbers = new int[5];
numbers[5] = 10; // Attempting to access an out-of-bounds index
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Index out of range: " + ex.Message);
}
Q.2) Write a program to handle the Mouse Click Event using C# and explain the concept of
event handling. —--->
using System; using System.Windows.Forms;
public class MouseClickExample : Form
{
public MouseClickExample()
{
this.Text = "Mouse Click Event Example";
this.Size = new Size(300, 200);
this.MouseClick += new
MouseEventHandler(this.Form_MouseClick);
}private void Form_MouseClick(object sender, MouseEventArgs e){ Console.WriteLine("Mouse clicked at
coordinates: " + e.X + ", " + e.Y);}
static void Main(){Application.Run(new MouseClickExample());}}
Event handling is a fundamental concept in programming that enables applications to respond to user
interactions or system events. In C#events are represented by classes that inherit from the
System.EventArgs class. When an event occurs, the corresponding event handler method is invoked,
allowing the program to take appropriate action.
Q.3)Write a C# code to create a class Laptop which inherits the base class LaptopBase.
LaptopBase has several constant values that are used in the main method.
—---> using System;
class LaptopBase
{
const string BRAND_DELL = "Dell"; const string BRAND_SONY = "Sony"; const int
PRICE_DELL = 1500; const int PRICE_SONY = 2000; const string PROCESSOR_DELL = "i5";
const string PROCESSOR_SONY = "i9"; const int RAM_DELL = 4; const int RAM_SONY = 8;
const int HDD_DELL = 1;
const int HDD_SONY = 1;
public void PrintSpecifications()
{
Console.WriteLine("*******************************
*****"); }
}
class Laptop : LaptopBase
{
private string brand; private int price; private string processor; private int ram;
private int hdd;
Multicast Delegates: A multicast delegate, also known as a combinable delegate, is a special type of delegate
that can hold references to multiple methods. When a multicast delegate is invoked, it calls all the methods
it references in the order they were added. This enables you to chain together multiple methods and execute
them in sequence.
Q.5) Describe the structure of a C# Program with proper code structure. Explain the jump
statements available in C#?
—--> 1. Namespaces: C# programs often belong to namespaces, which are hierarchical structures for
organizing code. Typically, the namespace declaration is placed at the beginning of the program. 2. Using
Directives: Using directives specify which namespaces and classes are accessible within the program.3.
Class Declarations: Classes are the fundamental building blocks of object-oriented programming.4.
Method Declarations: Methods are blocks of code that define specific actions or functionalities within a
class. 5. Main Method: The Main method is the entry point of a C# application. 6.Statements: Statements
are the basic building blocks of C# code. They represent instructions that the program executes . 7.
Control Flow Statements: Control flow statements determine the order in which statements are executed.
Jump Statements in C#:
1) break: Terminates the current loop or switch statement and transfers control to the following
statement.2) continue: Skips the remaining iterations of the current loop and starts the next iteration.
3) return: Exits the current method and returns a value (if applicable) to the caller. 4)goto: Jumps to a
specific labeled statement within the program.
5) throw: Raises an exception, which is a special type of error that interrupts the normal program flow.
Q.6) Write a program to add two numbers. Only condition is if the sum of numbers is odd
it fires an event that prints a message using delegates. —--> using System;
U-2
Q.1) Write steps to establish ADO.NET SQL Server Connection and create a table of
Student(id ,name ,email , join_date) write code in Program.cs file.
—-> Step 1: Install SQL Server and SQL Server Management Studio (SSMS) Step 2: Create a SQL Server
Database Step 3: Establish an ADO.NET SQL Server
Connection. Step 4: Create the Student Table
Program.cs :
The LINQ architecture consists of three primary layers:1. Language Extension: The language extension
layer sits at the top of the architecture and provides the query syntax that developers use to formulate
queries. 2. Query Provider : The query provider layer acts as an intermediary between the query
expressions and the data source. It translates the query expressions into a format that the underlying
data source can understand.mory collections.3. Data Source: The data source layer represents the actual
data being queried. It encompasses various types of data storage, including databases, file systems, and
network resources.
This layered architecture provides several advantages for LINQ: 1.) Unified Query Syntax
2.) Expressive Query Language 3.) Improved
Performance 4.) Enhanced Developer
Productivity.
Q.4) Consider the employee database and solve the following queries using SQL. All the
primary keys are underlined and highlighted. Employee (emp_name, street, city) Works
(emp_name, company_name, salary) Company (company_name, city) Manages
(emp_name, manager_name) (a) Find the names of all the employees who work for Royal
Bank Corporation. (b) Find the names, street addresses, and cities of residence of all
employees who work for Royal Bank Corporation and earn more than 300000 per annum. (c)
Find the names of all employees in this database who live in the same city as the companies
for which they work. (d) Find the names of all the employees who earn more than every
employee of First Bank Corporation.
—-> a)
SELECT emp_name
FROM Employee
INNER JOIN Works ON Employee.emp_name = Works.emp_name
WHERE Works.company_name = 'Royal Bank
Corporation'; b)
SELECT e.emp_name, e.street, e.city
FROM Employee e
INNER JOIN Works w ON e.emp_name =
w.emp_name
WHERE w.company_name = 'Royal Bank
Corporation'
AND w.salary > 300000;
c)
SELECT e.emp_name
FROM Employee e
INNER JOIN Works w ON e.emp_name =
w.emp_name
INNER JOIN Company c ON w.company_name
= c.company_name WHERE e.city = c.city; d)
SELECT e.emp_name
FROM Employee e
WHERE e.salary > ( SELECT MIN(salary)
FROM Works w2
WHERE w2.company_name = 'First Bank
Corporation'
);
Q.5) Create a stored Procedure named "SelectAllCustomers" that selects all records from
the "Customers" table.
Explain how LINQ is better than Stored
Procedures.
—--->
CREATE PROCEDURE SelectAllCustomers
AS
BEGIN SELECT *
FROM Customers; END;
Supports a wide
range of query Limited to basic
Power operations operations query
Can be
dynamically
generated at
Flexibility runtime Static
Integration
with
oth
er Better integrated
technologi with other .NET
es technologies Limited integration
U.3
Q.1) Write a JavaScript program to sort the elements of an array in ascending order.
—---> function sortAscending(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) { const temp = array[j]; array[j] = array[j + 1]; array[j + 1] =
temp; }}}
return array;}
const numbers = [5, 2, 4, 1, 3];
Const sortedNumbers=
sortAscending(numbers);
console.log(sortedNumbers); // Output: [1, 2, 3, 4, 5]
Q.2) If anyone wants to perform the operations in Disconnected mode, Which one should we
use: DataReader or DataSet? Explain why?
—-> For performing operations in disconnected mode, the preferred choice is the DataSet object.
DataReader: 1) A forward-only, read-only data stream that retrieves data from a database. 2) Does not
provide any caching or data manipulation capabilities. 3) Requires an active connection to the database to
function. 4) Not suitable for disconnected data access. DataSet: 1) An in-memory representation of a
database that holds data in tables and relationships. 2) Provides caching and data manipulation capabilities,
including filtering, sorting, and grouping. 3) Does not require an active connection to the database to function.
4) Ideal for disconnected data access and scenarios where persistent storage of data is needed.
Q.3) Explain the working of the following controls : (i) Button (ii) Checkbox (iii) Label (iv)
Textbox.
—-->
(i) Button : A button is a control that allows the user to trigger an action when clicked. It is typically used
to submit forms, navigate between pages, or perform other tasks. When a button is clicked, it fires an
event that can be handled by JavaScript or other code. (ii) Checkbox : A checkbox is a control that allows
the user to select or deselect an option. It is typically used to represent a binary choice, such as yes or
no, true or false, or on or off. When a checkbox is checked or unchecked, it fires an event that can be
handled by
JavaScript or other code. (iii) Label : A label is a control that is used to display text. It is typically used to
identify other controls, such as input fields or buttons.
(iv) Textbox: A textbox is a control that allows the user to enter text. It is typically used to collect user input,
such as names, addresses, or email addresses. When a textbox is edited, it fires an event that can be
handled by
JavaScript or other code.
Q.4) Write HTML code to display the following table with a background color cyan in the
center of the web page:
—-->
<!DOCTYPE html>
<html>
<head>
<title>Exam Schedule</title>
<style> table { background-color: cyan; margin: 0 auto; width: 80%; border-collapse:
collapse;} th, td {
padding: 8px; text-align: left;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<table>
<caption>Exam Schedule</caption>
<tr>
<th>Exam Date</th>
<th>Time of Exam</th>
<th>Course Code</th>
<th>Course Begin</th>
<th>Course End</th>
<th>Name</th>
</tr>
<tr>
<td>05/07/2022</td>
<td>11.00 a.m. - 12.00 p.m.</td>
<td>CS-255</td>
<td>DM 2.30 p.m.</td>
<td>3.30 p.m.</td>
<td>CS-256 COA</td>
</tr>
<tr>
<td>06/07/2022</td>
<td>11.00 a.m. - 12.00 p.m.</td>
<td>CS-257</td>
<td>SE 2.30 p.m.</td> <td>3.30 p.m.</td>
<td>CS-258 DAA</td>
</tr>
<tr>
<td>07/07/2022</td>
<td>11.00 a.m. - 12.00 p.m.</td>
<td>HUT-255</td>
<td>OB 2.30 p.m.</td>
<td>3.30 p.m.</td>
<td>ITT-42 PP</td>
</tr>
</table>
</body>
</html>
Q.5) Elaborate the concept of ADO.NET? What are the objects of ADO.NET?
—-> ADO.NET (ActiveX Data Objects for .NET) is a set of classes in the .NET Framework that facilitate
data access from various sources, including relational databases, XML documents, and other data
sources. It provides a consistent programming model for accessing and manipulating data.
Core components of ADO.NET :
1) Connection: Represents the connection to the data source, establishing a communication channel
between the application and the data store. 2) Command: Represents a command that can be executed
against the data source. 3) DataReader: Provides a read-only stream of data. 4) DataAdapter: Bridge the
gap between datasets and data sources . 5) DataSet: Represents a disconnected data cache. 6)
DataTable: Represents a two-dimensional tabular data structure, similar to a database table. 7) DataView:
Provides a filtered and sorted view of an underlying DataTable.
Define how to
handle HTTP
Responsi Manage object creation
requests
bility and lifetime
Q.2) How the edit functionality would work in ASP.NET MVC application.
—-> 1.The user clicks on the "Edit" link for a particular student on the index page. 2. This sends an HTTP
GET request to the Edit action method of the StudentController class. 3. The Edit action method retrieves
the student data from the database based on the ID parameter that is passed in the request. 4. The Edit
action method then renders the Edit view, passing in the student data as a model. 5.The Edit view displays
a form with the student data pre-populated. 6. The user makes any necessary changes to the student data
and submits the form. 7. This sends an HTTP POST request to the Edit action method, with the updated
student data in the request body. 8. The Edit action method validates the updated student data and saves it
to the database. 9. The Edit action method then redirects the user back to the index page.
Q.3) Draw and explain MVC application life cycle?
—--> The ASP.NET MVC application lifecycle is the sequence of events that happen every time an HTTP
request is handled by the application. It can be divided into the following stages:1.)Request: The client
sends an HTTP request to the server. The request includes the URL of the resource that the client wants
to access.The framework then parses the request and extracts the relevant information, such as the URL,
the HTTP method, and the request headers.2) Routing: The ASP.NET MVC framework routes the request
to the appropriate controller action. The routing process uses the URL and the route table to match the
request to a controller action. 3)Action execution: The controller action executes and returns a result. The
controller action is a method on a controller class. It is responsible for handling the request and returning
a result.4)Result execution: The ASP.NET MVC framework executes the result
and sends the response to the client. The result can be a view, a redirect, or a JSON object.
Q.4) How do you implement Forms authentication in MVC? What are the benefits of Forms
authentication?
—--> Steps :
1. Configure the web.config file:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" /> </authentication>
2. Create login and logout pages: You need to create a login page where users can enter their credentials
and a logout page to end their session. 3. Implement user authentication in your controller: In your
controller, you need to implement the action method that handles user authentication. 4. Use the
[Authorize] attribute to protect controller actions: You can use the [Authorize] attribute to protect
controller actions from unauthorized access.
1. Controller Name: The controller name identifies the specific controller class that should handle the request.
2.Action Method Name: The action method name specifies the specific method within the controller that
should be invoked to process the request.
3.Parameters: Parameters provide additional information to the action method, allowing it to handle specific
data or requests.
Q.6) Where Do We Use Middleware Components in the ASP.NET Core
Application?
U-5
Q.1) Describe different types of Dependency injection in MVC.
Benefits of using data annotation validator attributes: 1.Ease of use. 2.Declarative syntax .
3. Automatic validation error messages.
Common Data Annotation Validator Attributes: 1.Required: This attribute specifies that a property is
required and cannot be null or empty. 2.StringLength: This attribute specifies the maximum and minimum
length of a string property. 3.Range: This attribute specifies the range of values that a numeric property can
have. 4.RegularExpression: This attribute specifies a regular expression that a property value must match.
5.EmailAddress. 6.Url.
7.Phone: This attribute specifies that a property value must be a valid phone number.
Q.3) Clarify what is the best practice with Web API error management.
—-> Here are some best practices for Web API error management:
1. Use Proper HTTP Status Codes:
2. Provide Error Details:
3. Use a Consistent Error Format:
4. Log Errors:
5. Use Exception Filters:
6. Implement Error Monitoring:
7. Provide User-Friendly Error Messages:
8. Offer Error Documentation:
9. Use Error Handling Middleware:
10. Consider Error Retries for Transient Errors:
—--->It is a feature that allows you to validate data against a remote server without having to submit the
entire form. This is useful for situations where the data cannot be validated on the client.
To use remote validation, you will need to create a controller action that can handle the validation request.
Q.5) How can you incorporate both authentication and authorization mechanisms into a
Web API, and could you provide examples to illustrate this?
—----> it is crucial for securing access to sensitive data and enforcing user permissions. Authentication
verifies the identity of the user, while authorization determines what actions the user is allowed to
perform.
Consider an API endpoint that retrieves a user's profile information. To protect this endpoint, you can
implement both authentication and authorization: 1 .Authentication: Use token-based authentication to
verify the identity of the user making the request. This ensures that only authenticated users can
access the endpoint. 2. Authorization: Use claims-based authorization to check if the user has the
required claim, such as "accessProfile," to retrieve the profile information. This ensures that only
authorized users can access the sensitive data.
Types of Exception Filters : 1.Global Exception Filters: These filters apply to all controllers and actions
in the application. They are typically used to handle common errors, such as 404 Not Found or 500
Internal Server Error. 2. Controller-specific Exception Filters: These filters are applied to specific
controllers or actions, allowing for more granular error handling. They are useful for handling errors
specific to a particular controller or action.
U-6
Q.1) Can a container autonomously initiate a restart, and what are the reasons for such
behavior? —--->Yes, a container can autonomously initiate a restart. This is called a restart policy.
Reasons : 1. The container has crashed: If the container crashes, it will be restarted automatically. 2.The
container has been updated: If the container has been updated, it will need to be restarted in order for the
new image to be used.3.The container has been unhealthy: If the container is unhealthy, it will be restarted
automatically.
—--->Docker is a platform for building, running, and managing containers. Containers are lightweight,
portable, and self-sufficient environments that contain an application and all of its dependencies. This
makes them easy to deploy and manage across different environments.
Operating
system Host Guest
Resource
isolation Shared Isolated
Performance High Medium