A
Lab Record
.NET FRAMEWORK AND PROGRAMMING
(BCSP-701)
Session: - 2023-24
Department of Computer Science and Engineering
Submitted to Submitted by
Mr. Girish Bisht Akash Giri
Assistant professor 200120101012
EXPERIMENT-1
Aim: Write a program to implement SET, GET properties.
PROGRAM
Using System;
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main(string[] args)
{
Person obj1 = new Person();
obj1.Name = "Aakash";
Console.WriteLine(obj.Name);
}
}
OUTPUT:
Aakash
Press enter key to exit..
EXPERIMENT-3
Aim: Write a program to print the Armstrong Number.
PROGRAM
using System;
public class ArmstrongExample
{
public static void Main(string[] args)
{
int n,r,sum=0,temp;
Console.Write("Enter the Number= ");
n = int.Parse(Console.ReadLine());
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
Console.Write("Armstrong Number.");
else
Console.Write("Not Armstrong Number.");
}
}
OUTPUT:
EXPERIMENT-4
Aim: Create a console application to calculate area of circle. Accept radius from user
and print it.Create and console application to build simple calculator, calculator will
have following functions Accept 2 numbers perform Add/Sub/Multi and print result.
Program for area of circle:
using System;
class Circle{
static void Main(string[] args){
Console.Write("Enter Radius: ");
double rad = Convert.ToDouble(Console.ReadLine());
double area = Math.PI * rad * rad;
Console.WriteLine("Area of circle is: " + area);
}
}
OUTPUT:
Program to implement calculator:
class Program
{
static void Main(string[] args){
int num1;
int num2;
string operand;
ConsoleKeyInfo status;
float answer;
while (true)
{
Console.Write("Please enter the first integer: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the second integer: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter an operand (+, -, /, *): ");
operand = Console.ReadLine();
switch (operand)
{
case "-":
answer = num1 - num2;
break;
case "+":
answer = num1 + num2;
break;
case "/":
answer = num1 / num2;
break;
case "*":
answer = num1 * num2;
break;
default:
answer = 0;
break;
}
Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = "
+
answer.ToString());
Console.WriteLine("\n\n Do You Want To Break (Y/y)");
status = Console.ReadKey();
if(status.Key==ConsoleKey.Y)
{
break;
}
Console.Clear();
}
}
}
OUTPUT:
EXPERIMENT-5
Aim: Write a program to Use a Exception (Predefined and User defined).
PROGRAM
using System;
public class InvalidAgeException : Exception{
public InvalidAgeException(String message)
: base(message)
{
}
}
public class TestUserDefinedException{
static void validate(int age){
if (age < 18){
throw new InvalidAgeException("Sorry, Age must be greater than 18");
}
}
public static void Main(string[] args){
try {
Validate(19);
validate(12);
}
catch (InvalidAgeException e) { Console.WriteLine(e); }
}
}
OUTPUT
EXPERIMENT-6
Aim: Write a program to implement the concept of Abstract and Sealed classes.
Implementation of abstract classes:
PROGRAM
using System;
public abstract class Animal
{
public abstract string Sound { get; }
public virtual void Move()
{
Console.WriteLine("Moving...");
}
}
public class Cat : Animal
{
public override string Sound => "Meow";
public override void Move()
{
Console.WriteLine("Walking like a cat...");
}
}
public class Dog : Animal
{
public override string Sound => "Woof";
public override void Move()
{
Console.WriteLine("Running like a dog...");
}
}
class Program
{
static void Main(string[] args)
{
Animal[] animals = new Animal[] { new Cat(), new Dog() };
foreach (Animal animal in animals)
{
Console.WriteLine($"The {animal.GetType().Name} goes {animal.Sound}");
animal.Move();
}
}
}
OUTPUT :
Implementation of Sealed Classes
PROGRAM
using System;
sealed class SealedClass {
public int Add(int a, int b){
return a + b;
}
}
class Program {
static void Main(string[] args){
SealedClass slc = new SealedClass();
int total = slc.Add(6, 4);
Console.WriteLine("Total = " + total.ToString());
}
}
OUTPUT:
EXPERIMENT-7
Aim: Write a program to implement ADO.Net database connectivity.
PROGRAM
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class Program{
static void Main(string[] args)
{
new Program().Connecting();
}
public void Connecting()
{
using (
SqlConnection con = new SqlConnection(“datasource=.;
database=student; integrated security=SSPI”)
)
{
con.Open();
Console.WriteLine("Connection Established Successfully");
}
}
}
}
OUTPUT:
EXPERIMENT-8
Aim: Write a program to implement the concept of Data streams.
PROGRAM
using System;
using System.IO;
public sealed class Program{
public static void Main(){
using (Stream s = new FileStream(@"c:\A\file.txt", FileMode.Open))
{
int obj;
while ((obj = s.ReadByte()) != -1)
{
Console.Write("{0} ", (char)obj);
}
Console.ReadLine();
}
}
}
EXPERIMENT-9
Aim: Write a program to implement the events and delegates.
Implementation of Events:
PROGRAM
using System;
namespace SampleApp {
public delegate string MyDel(string str);
class EventProgram {
event MyDel MyEvent;
public EventProgram() {
this.MyEvent += new MyDel(this.WelcomeUser);
}
public string WelcomeUser(string username) {
return "Welcome " + username;
}
static void Main(string[] args) {
EventProgram obj1 = new EventProgram();
string result = obj1.MyEvent("This is event 1");
Console.WriteLine(result);
}
}
}
OUTPUT:
Implementation of Delegates:
PROGRAM
using System;
class Program{
public delegate void addnum(int a, int b);
public delegate void subnum(int a, int b);
//Add method
public void sum(int a, int b){
Console.WriteLine("(100 + 40) = {0}", a + b);
}
// subtract method
public void subtract(int a, int b){
Console.WriteLine("(100 - 60) = {0}", a - b);
}
public static void Main(String []args)
{
Program obj = new Program();
addnum del_obj1 = new addnum(obj.sum);
subnum del_obj2 = new subnum(obj.subtract);
del_obj1(100, 40);
del_obj2(100, 60);
}
}
OUTPUT:
EXPERIMENT-11
Aim: Write a program to implement Indexers.
PROGRAM
using System;
public class MyCollection{
private int[] data = new int[10];
public int this[int index, bool square]{
get
{
if (square)
return data[index] * data[index];
else
return data[index];
}
set
{
if (square)
data[index] = (int)Math.Sqrt(value);
else
data[index] = value;
}
}
public int this[string name]
{
get
{
switch (name.ToLower())
{
case "first":
return data[0];
case "last":
return data[data.Length - 1];
default:
throw new ArgumentException("Invalid index parameter.");
}
}
}
// Read-only indexer
public int this[int index]
{
get { return data[index]; }
}
}
public class Program
{
public static void Main()
{
MyCollection collection = new MyCollection();
collection[0, false] = 5;
collection[1, false] = 10;
collection[2, false] = 15;
collection[3, false] = 20;
Console.WriteLine(collection[0, false]);
Console.WriteLine(collection[1, true]);
Console.WriteLine(collection["first"]);
Console.WriteLine(collection["last"]);
// Getting values using read-only indexer
Console.WriteLine(collection[2]);
Console.ReadLine();
}
}
OUTPUT:
EXPERIMENT-2
Aim: Write a program to implement string using Array.
PROGRAM
using System;
class Program {
static void Main(string[] args) {
int[] numbers = {1,2,3,4,5,6,7,8};
Console.WriteLine("Element in first index : " + numbers[0]);
Console.WriteLine("Element in second index : " + numbers[1]);
Console.WriteLine("Element in third index : " + numbers[2]);
Console.WriteLine("Element in fourth index : " + numbers[3]);
Console.WriteLine("Element in five index : " + numbers[4]);
Console.ReadLine();
}
}
OUTPUT:
EXPERIMENT-10
Aim: Design the WEB base Database connectivity Form by using ASP. NET.
PROGRAM:
1. Setting up the Project:
Create an ASP.NET Web Application project in Visual Studio.
2. Designing the Database Connectivity Form (Web Form):
<!DOCTYPE html>
<html>
<head>
<title>Database Connectivity Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Database Connectivity Form</h2>
<label for="txtName">Name:</label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /><br />
<label for="txtEmail">Email:</label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
3. Implementing Database Connectivity (Code-behind):
using System;
using System.Configuration;
using System.Data.SqlClient;
namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// You can add code here that executes when the page loads
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Retrieve form data
string name = txtName.Text;
string email = txtEmail.Text;
// Database connection string
string connectionString =
ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
// Insert data into the database
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "INSERT INTO YourTableName (Name, Email) VALUES (@Name, @Email)";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@Email", email);
try
{
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
Response.Write("Data inserted successfully!");
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
}
}
}
Table of Contents
S. No. Experiment Name Date Signature
01 WAP to implement SET, Get Properties?
02 WAP to implement String Using array's?
03 WAP to print the ARMSTRONG Number?
04 Create a console application to calculate area
of circle. Accept radius from user Calculate
circle area and print it Create a console
application to build simple calculator
Calculator will have following functions
Accept 2 numbers Perform Add/Sub/Div/Mult
Print Result.
05 WAP to Use a Exception (Predefined and User
defined).
06 WAP to implement the concept of Abstract and
Sealed Classes.
07 WAP to implement ADO.Net Database
connectivity.
08 WAP to implement the concept of Data
Streams.
09 WAP to implement the Events and
Delegates.
10 Design the WEB base Database connectivity
Form by using ASP. NET.
11 WAP to implement Indexers.