his lab file provides a series of programming assignments designed to help you learn
T
C# and the .NET Framework. These assignments cover a range of topics, from basic
console applications to more advanced Windows Forms applications. By completing
these assignments, you will gain practical experience in writing C# code and
developing robust software applications.
Assignments:
ach assignment includes a problem statement, code, and output. The code is
E
provided as a starting point, and you are expected to modify it to meet the
requirements of the assignment.
1 . Program to display the addition, subtraction, multiplication, and division of
two numbers using console applications.
Code:
using System;
ublic class Calculator
p
{
public static void Main(string[] args)
{
Console.Write("Enter the first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
onsole.Write("Enter the second number: ");
C
double num2 = Convert.ToDouble(Console.ReadLine());
onsole.WriteLine($"Addition: {num1 + num2}");
C
Console.WriteLine($"Subtraction: {num1 - num2}");
Console.WriteLine($"Multiplication: {num1 * num2}");
if (num2 != 0)
{
Console.WriteLine($"Division: {num1 / num2}");
}
else
{
Console.WriteLine("Cannot divide by zero.");
}
Console.ReadLine();
}
}
Output:
nter the first number: 10
E
Enter the second number: 2
Addition: 12
Subtraction: 8
Multiplication: 20
Division: 5
. Program to display the first 10 natural numbers and their sum using a console
2
application.
Code:
using System;
ublic class NaturalNumbers
p
{
public static void Main(string[] args)
{
int sum = 0;
Console.WriteLine("First 10 natural numbers:");
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
sum += i;
}
Console.WriteLine();
Console.WriteLine($"Sum: {sum}");
Console.ReadLine();
}
}
Output:
irst 10 natural numbers:
F
1 2 3 4 5 6 7 8 9 10
Sum: 55
3. Program to display the addition using the Windows application.
Code:
sing System;
u
using System.Windows.Forms;
ublic class AdditionForm : Form
p
{
private TextBox txtNum1;
private TextBox txtNum2;
private Button btnAdd;
private Label lblResult;
ublic AdditionForm()
p
{
// Initialize controls
txtNum1 = new TextBox();
txtNum1.Location = new System.Drawing.Point(50, 50);
txtNum1.Size = new System.Drawing.Size(100, 20);
t xtNum2 = new TextBox();
txtNum2.Location = new System.Drawing.Point(50, 80);
txtNum2.Size = new System.Drawing.Size(100, 20);
tnAdd = new Button();
b
btnAdd.Text = "Add";
btnAdd.Location = new System.Drawing.Point(50, 110);
btnAdd.Click += BtnAdd_Click;
lblResult = new Label();
lblResult.Location = new System.Drawing.Point(50, 140);
lblResult.Size = new System.Drawing.Size(200, 20);
/ / Add controls to the form
Controls.Add(txtNum1);
Controls.Add(txtNum2);
Controls.Add(btnAdd);
Controls.Add(lblResult);
/ / Set form properties
Text = "Addition Form";
Size = new System.Drawing.Size(300, 200);
}
rivate void BtnAdd_Click(object sender, EventArgs e)
p
{
try
{
double num1 = Convert.ToDouble(txtNum1.Text);
double num2 = Convert.ToDouble(txtNum2.Text);
lblResult.Text = $"Result: {num1 + num2}";
}
catch (Exception ex)
{
lblResult.Text = "Invalid input. Please enter numbers.";
}
}
[ STAThread]
public static void Main()
{
Application.Run(new AdditionForm());
}
}
Output:
● A Windows form will appear with two text boxes, a button, and a label.
● Enter numbers in the text boxes and click the "Add" button to see the sum in the
label.
. Write a program to convert an input string from lower to upper and upper to
4
lower case.
Code:
using System;
ublic class CaseConverter
p
{
public static void Main(string[] args)
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string result = "";
f oreach (char c in input)
{
if (char.IsLower(c))
{
result += char.ToUpper(c);
}
else if (char.IsUpper(c))
{
result += char.ToLower(c);
}
else
{
result += c; // Keep non-letter characters as they are
}
}
Console.WriteLine($"Converted string: {result}");
Console.ReadLine();
}
}
Output:
nter a string: Hello World
E
Converted string: hELLO wORLD
5. Write a program to a simple calculator using a Windows application.
Code:
sing System;
u
using System.Windows.Forms;
ublic class CalculatorForm : Form
p
{
private TextBox txtDisplay;
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0;
private Button btnPlus, btnMinus, btnMultiply, btnDivide, btnEqual, btnClear;
private double firstNum = 0;
private string operation = "";
ublic CalculatorForm()
p
{
// Initialize controls
txtDisplay = new TextBox();
txtDisplay.Location = new System.Drawing.Point(20, 20);
txtDisplay.Size = new System.Drawing.Size(200, 30);
txtDisplay.TextAlign = HorizontalAlignment.Right;
txtDisplay.Font = new System.Drawing.Font("Arial", 12);
tn1 = CreateButton("1", 20, 60);
b
btn2 = CreateButton("2", 80, 60);
btn3 = CreateButton("3", 140, 60);
btn4 = CreateButton("4", 20, 100);
btn5 = CreateButton("5", 80, 100);
btn6 = CreateButton("6", 140, 100);
btn7 = CreateButton("7", 20, 140);
btn8 = CreateButton("8", 80, 140);
btn9 = CreateButton("9", 140, 140);
btn0 = CreateButton("0", 20, 180);
tnPlus = CreateButton("+", 200, 60);
b
btnMinus = CreateButton("-", 200, 100);
btnMultiply = CreateButton("*", 200, 140);
tnDivide = CreateButton("/", 200, 180);
b
btnEqual = CreateButton("=", 140, 180);
btnClear = CreateButton("C", 80, 180);
/ / Add controls to the form
Controls.Add(txtDisplay);
Controls.Add(btn1);
Controls.Add(btn2);
Controls.Add(btn3);
Controls.Add(btn4);
Controls.Add(btn5);
Controls.Add(btn6);
Controls.Add(btn7);
Controls.Add(btn8);
Controls.Add(btn9);
Controls.Add(btn0);
Controls.Add(btnPlus);
Controls.Add(btnMinus);
Controls.Add(btnMultiply);
Controls.Add(btnDivide);
Controls.Add(btnEqual);
Controls.Add(btnClear);
/ / Set form properties
Text = "Simple Calculator";
Size = new System.Drawing.Size(300, 250);
}
rivate Button CreateButton(string text, int x, int y)
p
{
Button button = new Button();
button.Text = text;
button.Location = new System.Drawing.Point(x, y);
button.Size = new System.Drawing.Size(50, 30);
button.Click += Button_Click;
return button;
}
private void Button_Click(object sender, EventArgs e)
{
utton button = (Button)sender;
B
string buttonText = button.Text;
s witch (buttonText)
{
case "C":
txtDisplay.Text = "";
firstNum = 0;
operation = "";
break;
case "+":
case "-":
case "*":
case "/":
if (txtDisplay.Text != "")
{
firstNum = Convert.ToDouble(txtDisplay.Text);
operation = buttonText;
txtDisplay.Text = "";
}
break;
case "=":
if (txtDisplay.Text != "" && operation != "")
{
double secondNum = Convert.ToDouble(txtDisplay.Text);
double result = 0;
switch (operation)
{
case "+":
result = firstNum + secondNum;
break;
case "-":
result = firstNum - secondNum;
break;
case "*":
result = firstNum * secondNum;
break;
case "/":
if (secondNum != 0)
{
result = firstNum / secondNum;
}
else
{
MessageBox.Show("Cannot divide by zero.");
txtDisplay.Text = "";
return;
}
break;
}
txtDisplay.Text = result.ToString();
firstNum = 0;
operation = "";
}
break;
default:
txtDisplay.Text += buttonText;
break;
}
}
[ STAThread]
public static void Main()
{
Application.Run(new CalculatorForm());
}
}
Output:
● A Windows form will appear with a display and buttons for numbers and
perations.
o
Use the buttons to perform calculations, and the result will be shown in the
●
display.
6. Write a program working with Page using ASP.Net.
Code:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
<
Inherits="Default" %>
!DOCTYPE html>
<
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Page Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblMessage" runat="server" Text="Welcome to
ASP.NET!"></asp:Label>
<br />
<asp:Button id="btnClick" runat="server" Text="Click Me"
OnClick="btnClick_Click" />
</div>
</form>
</body>
</html>
Code Behind (Default.aspx.cs):
using System;
ublic partial class Default : System.Web.UI.Page
p
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMessage.Text = "Welcome to ASP.NET!";
}
}
rotected void btnClick_Click(object sender, EventArgs e)
p
{
lblMessage.Text = "You clicked the button!";
}
}
Output:
● When the page loads, it will display "Welcome to ASP.NET!"
● When the "Click Me" button is clicked, the text will change to "You clicked the
button!"
7. Write a program working with forms using ASP.NET.
Code:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormDemo.aspx.cs"
<
Inherits="FormDemo" %>
!DOCTYPE html>
<
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Form Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox id="txtName" runat="server"></asp:TextBox>
<br />
<asp:Label id="lblEmail" runat="server" Text="Email:"></asp:Label>
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<br />
<asp:Button id="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" />
<br />
<asp:Label id="lblMessage" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Code Behind (FormDemo.aspx.cs):
using System;
ublic partial class FormDemo : System.Web.UI.Page
p
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMessage.Text = "Please enter your details.";
}
}
rotected void btnSubmit_Click(object sender, EventArgs e)
p
{
string name = txtName.Text;
string email = txtEmail.Text;
lblMessage.Text = $"Name: {name}, Email: {email}";
}
}
Output:
● The page will display labels and text boxes for entering name and email.
● After entering the details and clicking the "Submit" button, the entered name and
email will be displayed.
8. Write a program to connectivity with Microsoft SQL database.
Code:
sing System;
u
using System.Data.SqlClient;
ublic class DatabaseConnection
p
{
public static void Main(string[] args)
{
s tring connectionString = "Data Source=your_server_name;Initial
Catalog=your_database_name;User ID=your_user_id;Password=your_password;";
sing (SqlConnection connection = new SqlConnection(connectionString))
u
{
try
{
connection.Open();
Console.WriteLine("Connected to the database successfully!");
/ / Perform database operations here (e.g., read data, insert data)
string query = "SELECT * FROM your_table_name";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
hile (reader.Read())
w
{
Console.WriteLine($"{reader["column1_name"]} -
{reader["column2_name"]}");
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.ReadLine();
}
}
}
}
Output:
Connected to the database successfully!
//Output of the query
Note:
● Replace "your_server_name", "your_database_name", "your_user_id", and
" your_password" with your actual SQL Server credentials.
Replace "your_table_name", "column1_name", and "column2_name" with your
●
actual table and column names.
9. Write a program to access data source through ADO.NET.
Code:
sing System;
u
using System.Data.SqlClient;
using System.Data;
ublic class DataAccess
p
{
public static void Main(string[] args)
{
string connectionString = "Data Source=your_server_name;Initial
Catalog=your_database_name;User ID=your_user_id;Password=your_password;";
sing (SqlConnection connection = new SqlConnection(connectionString))
u
{
try
{
connection.Open();
s tring query = "SELECT * FROM your_table_name";
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "your_table_name");
DataTable dataTable = dataSet.Tables["your_table_name"];
f oreach (DataRow row in dataTable.Rows)
{
Console.WriteLine($"{row["column1_name"]} - {row["column2_name"]}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.ReadLine();
}
}
}
}
Output:
//Output of the data from the table
Note:
● Replace "your_server_name", "your_database_name", "your_user_id", and
" your_password" with your actual SQL Server credentials.
Replace "your_table_name", "column1_name", and "column2_name" with your
●
actual table and column names.
10. Write a program to display the following feedback form.
Code:
sing System;
u
using System.Windows.Forms;
ublic class FeedbackForm : Form
p
{
private Label lblName;
private TextBox txtName;
private Label lblEmail;
private TextBox txtEmail;
private Label lblFeedback;
rivate TextBox txtFeedback;
p
private Button btnSubmit;
private Label lblMessage;
ublic FeedbackForm()
p
{
// Initialize controls
lblName = new Label();
lblName.Text = "Name:";
lblName.Location = new System.Drawing.Point(20, 20);
lblName.Size = new System.Drawing.Size(80, 20);
t xtName = new TextBox();
txtName.Location = new System.Drawing.Point(100, 20);
txtName.Size = new System.Drawing.Size(200, 20);
lblEmail = new Label();
lblEmail.Text = "Email:";
lblEmail.Location = new System.Drawing.Point(20, 50);
lblEmail.Size = new System.Drawing.Size(80, 20);
t xtEmail = new TextBox();
txtEmail.Location = new System.Drawing.Point(100, 50);
txtEmail.Size = new System.Drawing.Size(200, 20);
lblFeedback = new Label();
lblFeedback.Text = "Feedback:";
lblFeedback.Location = new System.Drawing.Point(20, 80);
lblFeedback.Size = new System.Drawing.Size(80, 20);
t xtFeedback = new TextBox();
txtFeedback.Location = new System.Drawing.Point(100, 80);
txtFeedback.Size = new System.Drawing.Size(200, 100);
txtFeedback.Multiline = true;
txtFeedback.ScrollBars = ScrollBars.Vertical;
tnSubmit = new Button();
b
btnSubmit.Text = "Submit";
btnSubmit.Location = new System.Drawing.Point(100, 200);
btnSubmit.Click += BtnSubmit_Click;
lblMessage = new Label();
lblMessage.Location = new System.Drawing.Point(20, 230);
lblMessage.Size = new System.Drawing.Size(300, 20);
/ / Add controls to the form
Controls.Add(lblName);
Controls.Add(txtName);
Controls.Add(lblEmail);
Controls.Add(txtEmail);
Controls.Add(lblFeedback);
Controls.Add(txtFeedback);
Controls.Add(btnSubmit);
Controls.Add(lblMessage);
/ / Set form properties
Text = "Feedback Form";
Size = new System.Drawing.Size(350, 300);
}
rivate void BtnSubmit_Click(object sender, EventArgs e)
p
{
string name = txtName.Text;
string email = txtEmail.Text;
string feedback = txtFeedback.Text;
/ / In a real application, you would save this data to a database or file.
lblMessage.Text = $"Thank you for your feedback, {name}!";
MessageBox.Show($"Name: {name}\nEmail: {email}\nFeedback: {feedback}",
"Feedback Submitted");
t xtName.Text = "";
txtEmail.Text = "";
txtFeedback.Text = "";
}
[ STAThread]
public static void Main()
{
Application.Run(new FeedbackForm());
}
}