PRACTICAL NO – 1
Write the program for the following
Practical 1(A):
Aim - Create an application to print on screen the output of adding, subtracting,
multiplying and dividing two numbers entered by the user in C#
Code –
using System;
using System.Web.UI;
namespace pract_1a
{
public partial class pract_1a : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Optionally handle page load logic here
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
// Get the input numbers
int n1 = Convert.ToInt32(TextBox1.Text);
int n2 = Convert.ToInt32(TextBox2.Text);
// Perform operations
double addition = n1 + n2;
double subtraction = n1 - n2;
double multiplication = n1 * n2;
double division = n2 != 0 ? (double)n1 / n2 : double.NaN;
// Display results
Label3.Text = $"Addition: {addition}<br />" +
$"Subtraction: {subtraction}<br />" +
$"Multiplication: {multiplication}<br />" +
$"Division: {(double.IsNaN(division) ? "Cannot divide by zero" :
division.ToString())}";
}
catch (FormatException)
{
Label3.Text = "Please enter valid numbers.";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
// Reset the TextBoxes and Label
TextBox1.Text = "";
TextBox2.Text = "";
Label3.Text = "";
}
}
}
OUTPUT –
Practical 1(B):
Aim – Create an application to print Floyd’s triangle till n rows in c#
Code- using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace pract_1b
{
public partial class pract_1b : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
try
{
// Get the number of rows from the user input
int rows = Convert.ToInt32(txtRows.Text);
// Initialize the starting number
int number = 1;
// StringBuilder to accumulate the output
StringBuilder sb = new StringBuilder();
// Generate Floyd's Triangle
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
sb.Append(number + " ");
number++;
}
sb.Append("<br />"); // New line for each row
}
// Display the result
lblResult.Text = sb.ToString();
}
catch (FormatException)
{
lblResult.Text = "Please enter a valid number.";
}
}
}
}
OUTPUT –
Practical 1(C):
Aim- Create an application to demonstrate the following operations – 1) Generate
Fibonacci series 2) Test for Prime numbers
Code-
using System;
using System.Text;
using System.Web.UI;
namespace pract_1c
{
public partial class pract_1c : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Optionally handle page load logic here
}
protected void btnGenerateFibonacci_Click(object sender, EventArgs e)
{
try
{
int terms = Convert.ToInt32(txtFibonacci.Text);
StringBuilder result = new StringBuilder();
int a = 0, b = 1;
result.Append(a + " ");
for (int i = 1; i < terms; i++)
{
result.Append(b + " ");
int temp = a + b;
a = b;
b = temp;
}
FibonacciResult.Text = "Fibonacci Series: " + result.ToString();
}
catch (FormatException)
{
FibonacciResult.Text = "Please enter a valid number.";
}
}
protected void btnTestPrime_Click(object sender, EventArgs e)
{
try
{
int number = Convert.ToInt32(txtPrime.Text);
if (IsPrime(number))
{
PrimeResult.Text = $"{number} is a prime number.";
}
else
{
PrimeResult.Text = $"{number} is not a prime number.";
}
}
catch (FormatException)
{
PrimeResult.Text = "Please enter a valid number.";
}
}
private bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
for (int i = 3; i <= Math.Sqrt(number); i += 2)
{
if (number % i == 0) return false;
}
return true;
}
}
}
OUTPUT-
PRACTICAL NO – 2
Write the program for the following
Practical 2(A):
Aim – Create a simple application to demonstrate use of boxing and unboxing
Code-
using System;
using System.Web.UI;
namespace pract_2a
{
public partial class pract_2a :
System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
// Optionally handle page load logic
here
}
protected void
btnDemonstrate_Click(object sender,
EventArgs e)
{
try
{
// Retrieve the integer input from the TextBox
int originalValue = Convert.ToInt32(txtInput.Text);
// Boxing: Convert the value type to object type
object boxedValue = originalValue;
// Modify the boxed value indirectly
// Note: Direct modification is not possible as boxedValue is an object reference
// and we cannot modify the value inside the object directly
int modifiedValue = originalValue + 10; // New value to simulate modification
// Unboxing: Convert the object type back to value type
int unboxedValue = (int)boxedValue;
// Display results
lblBoxing.Text = $"Original Value: {originalValue}<br />" +
$"Boxed Value (as object): {boxedValue}<br />" +
$"Modified Value (not affecting boxedValue): {modifiedValue}<br />";
lblUnboxing.Text = $"Unboxed Value: {unboxedValue} (This is the value
retrieved from boxedValue)";
}
catch (FormatException)
{
lblBoxing.Text = "Please enter a valid integer.";
lblUnboxing.Text = "";
}
}
}
}
Output –
Practical 2(B):
Aim- Create a simple program to perform addition and subtraction using
delegates
Code –
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Practical_2b
{
public partial class Practical_2b : System.Web.UI.Page
{
// Define delegate type for operations
public delegate double Operation(double x, double y);
protected void Page_Load(object sender, EventArgs e)
{
// Optionally handle page load logic here
}
// Method to perform addition
private double Add(double x, double y)
{
return x + y;
}
// Method to perform subtraction
private double Subtract(double x, double y)
{
return x - y;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
PerformOperation(Add, AdditionResult);
}
protected void btnSubtract_Click(object sender, EventArgs e)
{
PerformOperation(Subtract, SubtractionResult);
}
protected void btnReset_Click(object sender, EventArgs e)
{
// Clear TextBoxes and result labels
txtFirstNumber.Text = "";
txtSecondNumber.Text = "";
AdditionResult.Text = "";
SubtractionResult.Text = "";
}
private void PerformOperation(Operation operation, Label resultLabel)
{
try
{
// Retrieve input numbers from TextBoxes
double num1 = Convert.ToDouble(txtFirstNumber.Text);
double num2 = Convert.ToDouble(txtSecondNumber.Text);
// Perform the operation
double result = operation(num1, num2);
// Display the result in the appropriate label
if (resultLabel != null)
{
resultLabel.Text = $"Result: {result}";
}
}
catch (FormatException)
{
// Display error message if input is not valid
if (resultLabel == AdditionResult)
{
AdditionResult.Text = "Please enter valid numbers.";
}
else if (resultLabel == SubtractionResult)
{
SubtractionResult.Text = "Please enter valid numbers.";
}
}
}
}
}
Output-
Practical 2(C):
Aim- Create a simple application to demonstrate use of concept of interfaces.
Code-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace pract_2c
{
// Define the interface
public interface INotification
{
void Send(string message, Label resultLabel);
}
// Implement Email Notification
public class EmailNotification : INotification
{
public void Send(string message, Label resultLabel)
{
// For demonstration, just update the label
resultLabel.Text = $"Email sent with message: {message}";
}
}
// Implement SMS Notification
public class SMSNotification : INotification
{
public void Send(string message, Label resultLabel)
{
// For demonstration, just update the label
resultLabel.Text = $"SMS sent with message: {message}";
}
}
public partial class pract_2c : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Optionally handle page load logic here
}
protected void btnSend_Click(object sender, EventArgs e)
{
// Determine which type of notification to send
INotification notification = null;
if (rbEmail.Checked)
{
notification = new EmailNotification();
}
else if (rbSMS.Checked)
{
notification = new SMSNotification();
}
// Send the notification
if (notification != null)
{
string message = txtMessage.Text;
notification.Send(message, Result);
}
else
{
Result.Text
}
}
protected void
{
// Clear
Result.Text
}
}
}
Output –
PRACTICAL NO – 3
Write the program for the following
Practical 3(A):
Aim – Create a simple web page with various server controls to demonstrate
setting and use of their properties.
Code-
using System;
using System.Web.UI;
public partial class pract_3a_alternate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Page load logic can be placed here if needed
}
protected void Button1_Click(object sender, EventArgs e)
{
// Get the user's name from the TextBox
string name = TextBox1.Text;
// Determine the selected fruit
string favoriteFruit = "";
if (RadioButton1.Checked) favoriteFruit = "Apple";
else if (RadioButton2.Checked) favoriteFruit = "Banana";
else if (RadioButton3.Checked) favoriteFruit = "Orange";
// Prepare the result message
string resultMessage = $"Hello, {name}! Your favorite fruit is {favoriteFruit}.";
// Apply text formatting based on CheckBoxes
ResultLabel.Font.Bold = CheckBox1.Checked;
ResultLabel.Font.Italic = CheckBox2.Checked;
ResultLabel.Font.Underline = CheckBox3.Checked;
// Apply selected font size from DropDownList
ResultLabel.Font.Size = int.Parse(DropDownList1.SelectedValue);
// Apply selected text color
switch (DropDownList2.SelectedValue)
{
case "BabyPink":
ResultLabel.ForeColor = System.Drawing.Color.FromArgb(255, 182, 193);
break;
case "BabyLavender":
ResultLabel.ForeColor = System.Drawing.Color.FromArgb(230, 230, 250);
break;
case "Grey":
ResultLabel.ForeColor = System.Drawing.Color.Gray;
break;
default:
ResultLabel.ForeColor = System.Drawing.Color.Black;
break;
}
// Display the result
ResultLabel.Text = resultMessage;
}
}
OUTPUT-
Aim – Create a simple application to display your vacation using Calendar
Control.
Code-
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Pract_3b_calender
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// No actions required on Page Load for now
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.Day == 5 && e.Day.Date.Month == 9)
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
Label lbl = new Label();
lbl.Text = "<br>Teachers Day!";
e.Cell.Controls.Add(lbl);
}
if (e.Day.Date.Day == 28 && e.Day.Date.Month == 9)
{
Calendar1.SelectedDate = new DateTime(2024, 9, 7);
Calendar1.SelectedDates.SelectRange(Calendar1.SelectedDate,
Calendar1.SelectedDate.AddDays(10));
Label lbl1 = new Label();
lbl1.Text = "<br>Ganpati Festival!";
e.Cell.Controls.Add(lbl1);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;
Calendar1.TitleFormat = TitleFormat.Month;
Label1.Text = "Your Selected Date: " + Calendar1.SelectedDate.Date.ToString();
Label2.Text = "Today's Date: " + Calendar1.TodaysDate.ToShortDateString();
Label3.Text = "Ganpati Vacation Starts: 7-09-2024";
TimeSpan d = new DateTime(2024, 9, 07) - DateTime.Now;
Label4.Text = "Days Remaining for Ganpati Vacation: " + d.Days.ToString();
TimeSpan d1 = new DateTime(2024, 12, 31) - DateTime.Now;
Label5.Text = "Days Remaining for New Year: " + d1.Days.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Calendar1.SelectedDates.Clear();
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = "Selected Date: " + Calendar1.SelectedDate.ToShortDateString();
}
}
}
OUTPUT-
Practical 3(C):
Aim- Demonstrate the use of Tree View Operations on web form.
Code- using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Pract_3c
{
public partial class WebForm2 : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// No need to bind data on initial load
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
// Clear existing nodes
TreeView1.Nodes.Clear();
// Load XML data from file
var xmlDocument = new System.Xml.XmlDocument();
string filePath = Server.MapPath("~/stddetails.xml");
xmlDocument.Load(filePath);
// Iterate through each student node
foreach (System.Xml.XmlNode student in xmlDocument.SelectNodes("//student"))
{
TreeNode studentNode = new TreeNode("Student");
// Add child nodes for student details
studentNode.ChildNodes.Add(new TreeNode("ID: " +
student["sid"].InnerText));
studentNode.ChildNodes.Add(new TreeNode("Name: " +
student["sname"].InnerText));
studentNode.ChildNodes.Add(new TreeNode("Class: " +
student["sclass"].InnerText));
// Add the student node to the TreeView
TreeView1.Nodes.Add(studentNode);
}
}
catch (Exception ex)
{
// Display error message
ErrorMessageLabel.Text = "An error occurred: " + ex.Message;
ErrorMessageLabel.Visible = true;
}
}
}
}
OUTPUT-
PRACTICAL NO – 4
Write the program for the following
Practical 4(A):
Aim – Create a Registration Form to demonstrate use of various Validation controls.
Code- using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace VALIDATION_FORM
{
public partial class VALIDATION_FORM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Submitted");
}
}
}
OUTPUT-