0% found this document useful (0 votes)
6 views20 pages

Net LAB Manual

nfn efjf je

Uploaded by

royroboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Net LAB Manual

nfn efjf je

Uploaded by

royroboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

SHIVALIKCOLLEGEOFENGINEERING

RecognisedunderUGCSection(2f)oftheUGCAct1956

Lab Manual

EXPERIMENT-1

Aim: Introduction to C# and .NET Framework

Objective: Create a simple "Hello World" console application.

Steps:

1. Open Visual Studio or your preferred IDE.

2. Create a new Console Application project.

3. Replace the auto-generated code with the following:

Code:

using System;

class Program {

static void Main() {

Console.WriteLine("Hello, World!");

4.Run the program by pressing F5.

Output:

Hello, World!

Page 1 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT-2

Aim: Basic C# Programming Constructs

Objective: Create a console application to perform arithmetic


operations

Code:

using System;

class ArithmeticOperations

static void Main()

Console.WriteLine("Enter two numbers:");

double num1 = Convert.ToDouble(Console.ReadLine());

double num2 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Choose an operation: +, -, *, /");

char operation = Console.ReadKey().KeyChar;

Console.WriteLine();

double result = 0;

switch (operation)

case '+': result = num1 + num2; break;

case '-': result = num1 - num2; break;

case '*': result = num1 * num2; break;

Page 2 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

case '/': result = (num2 != 0) ? num1 / num2 :


double.NaN; break;

default: Console.WriteLine("Invalid operation!"); return;

Console.WriteLine($"Result: {result}");

Output:
Enter two numbers:
5
3
Choose an operation: +
Result: 8

Page 3 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT-3

Aim: Functions and Arrays in C#

Objective: Create a console application to calculate the factorial of a


number using functions.

Code: using System;


class FactorialCalculator
{
static long Factorial(int num)
{
if (num <= 1) return 1;
return num * Factorial(num - 1);
}
static void Main()
{
Console.WriteLine("Enter a number:");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Factorial of {number} is {Factorial(number)}");
}
}

Output:
Enter a number:
5
Factorial of 5 is 120

Page 4 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT-4

Aim: Object-Oriented Programming – Classes and Objects

Objective-: Define a Student class with properties and methods.Create


objects of the Student class and invoke methods.

Code:

using System;

class Student

public string Name { get; set; }

public int Age { get; set; }

public void DisplayDetails()

Console.WriteLine($"Name: {Name}, Age: {Age}");

class Program

static void Main()

Student student = new Student { Name = "John Doe", Age = 20 };

Page 5 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

student.DisplayDetails();

Output:

Name: John Doe, Age: 20

Page 6 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT-5

Aim: OOP – Inheritance and Polymorphism

Objective: Demonstrate method overriding and use of the virtual and override
keywords.

Code-:

using System;

class Animal

public virtual void Speak()

Console.WriteLine("Animal speaks");

class Dog : Animal

public override void Speak()

Console.WriteLine("Dog barks");

class Program

Page 7 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

static void Main()

Animal animal = new Animal();

animal.Speak();

Dog dog = new Dog();

dog.Speak();

Animal polymorphicAnimal = new Dog();

polymorphicAnimal.Speak();

Output:
Animal speaks
Dog barks
Dog barks

Page 8 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT- 6

Aim: Encapsulation and Access Modifiers

Objective: Create a BankAccount class with private fields and public properties.

Code-:

using System;

class BankAccount

private double balance;

public double Balance

get { return balance; }

set

if (value >= 0)

balance = value;

else

Console.WriteLine("Invalid balance");

public void Deposit(double amount)

Page 9 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

if (amount > 0) balance += amount;

else Console.WriteLine("Invalid deposit amount");

public void Withdraw(double amount)

if (amount > 0 && amount <= balance)

balance -= amount;

else

Console.WriteLine("Invalid withdrawal amount");

class Program

static void Main()

BankAccount account = new BankAccount();

account.Deposit(100);

account.Withdraw(50);

Console.WriteLine($"Balance: {account.Balance}");

Output:

Balance: 50

Page 10 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT- 7

Aim: Exception Handling and File I/O

Objective: Implement try-catch blocks to handle runtime errors.

Code: using System;


using System.IO;

class ExceptionHandlingDemo
{
static void Main()
{
string filePath = "example.txt";

try
{
// Attempt to read the file
string content = File.ReadAllText(filePath);
Console.WriteLine("File Content:\n" + content);
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"Error: The file '{filePath}' was not found.");
Console.WriteLine("Details: " + ex.Message);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine($"Error: You don't have permission to access the file
'{filePath}'.");
Console.WriteLine("Details: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An unexpected error occurred: " + ex.Message);
}
finally
{
Console.WriteLine("File reading operation is completed.");
}
}
}

Page 11 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

Output:

Case 1: File exists:


File Content:
Hello, this is a sample text file.
File reading operation is completed.

Case 2: File does not exist:


Error: The file 'example.txt' was not found.
Details: Could not find file 'example.txt'.
File reading operation is completed.

Case 3: Unauthorized access:


Error: You don't have permission to access the file 'example.txt'.
Details: Access to the path 'example.txt' is denied.
File reading operation is completed.

Page 12 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT- 8

Aim: Windows Forms Application

Objective: Create a basic calculator application using Windows Forms.

Code: using System;


using System.Windows.Forms;

public class CalculatorForm : Form


{
private TextBox txtNumber1, txtNumber2;
private Label lblResult;
private Button btnAdd, btnSubtract, btnMultiply, btnDivide;

public CalculatorForm()
{
// TextBox for Number 1
txtNumber1 = new TextBox { Location = new System.Drawing.Point(20, 20),
Width = 100 };
Controls.Add(txtNumber1);

// TextBox for Number 2


txtNumber2 = new TextBox { Location = new System.Drawing.Point(20, 60),
Width = 100 };
Controls.Add(txtNumber2);

// Buttons
btnAdd = new Button { Text = "Add", Location = new System.Drawing.Point(150,
20) };
btnAdd.Click += (sender, e) => PerformOperation("Add");
Controls.Add(btnAdd);

btnSubtract = new Button { Text = "Subtract", Location = new


System.Drawing.Point(150, 60) };
btnSubtract.Click += (sender, e) => PerformOperation("Subtract");
Controls.Add(btnSubtract);

btnMultiply = new Button { Text = "Multiply", Location = new


System.Drawing.Point(150, 100) };
btnMultiply.Click += (sender, e) => PerformOperation("Multiply");
Controls.Add(btnMultiply);

btnDivide = new Button { Text = "Divide", Location = new


System.Drawing.Point(150, 140) };
btnDivide.Click += (sender, e) => PerformOperation("Divide");
Controls.Add(btnDivide);
Page 13 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

// Label for Result


lblResult = new Label { Location = new System.Drawing.Point(20, 100), Width =
200 };
Controls.Add(lblResult);
}

private void PerformOperation(string operation)


{
try
{
double num1 = Convert.ToDouble(txtNumber1.Text);
double num2 = Convert.ToDouble(txtNumber2.Text);
double result = 0;

switch (operation)
{
case "Add":
result = num1 + num2;
break;
case "Subtract":
result = num1 - num2;
break;
case "Multiply":
result = num1 * num2;
break;
case "Divide":
if (num2 == 0) throw new DivideByZeroException("Cannot divide by
zero.");
result = num1 / num2;
break;
}

lblResult.Text = $"Result: {result}";


}
catch (FormatException)
{
MessageBox.Show("Please enter valid numbers.", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (DivideByZeroException ex)
{
MessageBox.Show(ex.Message, "Math Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch (Exception ex)
{

Page 14 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

MessageBox.Show($"An unexpected error occurred: {ex.Message}", "Error",


MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

[STAThread]
public static void Main()
{
Application.EnableVisualStyles();

Application.Run(new CalculatorForm());
}
}

Output:
1. Addition:
o Input: Number 1 = 10, Number 2 = 20
o Output: Result: 30
2. Division by Zero:
o Input: Number 1 = 10, Number 2 = 0
o Output: MessageBox: "Cannot divide by zero."
3. Invalid Input:
o Input: Number 1 = abc, Number 2 = 5
o Output: MessageBox: "Please enter valid numbers."
4. Other Operations:
o Subtraction: Result: -10
o Multiplication: Result: 200
o Division: Result: 2

Page 15 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT:9

Aim: Working with Databases in .NET

Objective: Create a Windows Forms application to connect to an


SQL database.

Code-: using System;

using System.Data.SqlClient;

using System.Windows.Forms;

public partial class MainForm : Form

public MainForm()

InitializeComponent();

private void btnFetchData_Click(object sender, EventArgs e)

string connectionString = "Data Source=localhost;Initial


Catalog=School;Integrated Security=True";

using (SqlConnection conn = new


SqlConnection(connectionString))

conn.Open();

Page 16 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

SqlCommand cmd = new SqlCommand("SELECT Name, Age


FROM Students", conn);

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

string studentName = reader["Name"].ToString();

string studentAge = reader["Age"].ToString();

MessageBox.Show($"Name: {studentName}, Age:


{studentAge}");

Output:

Popup 1:

Name: John Doe, Age: 20

Popup 2:

Name: Jane Smith, Age: 22

Popup 3:

Name: Alex Johnson, Age: 19

Page 17 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

EXPERIMENT-10

Aim: Application Deployment and Debugging

Objective: Deploy a Windows Forms application.

Code-: Steps and Expected Output

1. Deployment Steps:

o Publish the application using the Visual Studio Publish


Wizard:

 Go to Build → Publish.

 Select a target location (folder, IIS, or Azure).

 Configure the installation settings (framework


dependency, etc.).

 Generate a setup file (e.g., .exe or .msi).

o Transfer the published files to the target machine or


environment.

2. Installation:

o Locate the generated setup.exe or .msi file in the


deployment folder.

o Run the installer on the target system.

o Follow the installation prompts (e.g., selecting a directory


for installation).

Output during installation:

o An installation wizard appears, prompting the user for


inputs like installation path.

o A completion dialog confirms successful installation:

Page 18 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

Installation Complete!

Your application has been installed successfully.

3. Execution:

o Navigate to the installed application (e.g., from the Start


Menu or desktop shortcut).

o Launch the application.

Output after execution:

o The deployed application opens and runs as designed.

o Example: If the deployed application is a calculator, its


user interface appears, and the user can perform
calculations.

Expected Behavior:

o The application UI matches the design from the


development environment.

o The application's functionality (e.g., database connectivity,


UI responsiveness) is verified to be working correctly.

Output Example

 For a basic calculator application:

o The calculator UI is displayed.

o Users can perform calculations like addition, subtraction,


multiplication, and division.

Page 19 of 20
SHIVALIKCOLLEGEOFENGINEERING
RecognisedunderUGCSection(2f)oftheUGCAct1956

Sample Execution Output:

Welcome to the Basic Calculator!

 Result of 5 + 3 = 8

 For a database-connected application:

o Users can connect to the database and view data, like in


Experiment 8.

Sample Execution Output:

Student Records:

1. John Doe, Age: 20

2. Jane Smith, Age: 22

Page 20 of 20

You might also like