BCA - III Semester - C# and .
Net Technologies Lab
Practicals:
1. Develop a C# .NET console application to demonstrate the conditional
statements.
2. Develop a C# .NET console application to demonstrate the control statements.
3. Demonstrate Multithreaded Programming in C#.NET
4. Demonstrate subroutines and functions in C#.NET
5. Construct a console application to demonstrate the OOP Concepts
--------
6. Develop an application in C#.NET that demonstrates the windows controls
7. Develop a web application in VB.NET for dynamic Login Processing
8. Develop an application for deploying various built-in functions in VB.NET
9. Develop an MDI application for Employee Pay-roll transactions in VB.NET
10. Develop a Windows application with database connectivity for core-banking
transactions
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 1
1. Develop a C# .NET console application to demonstrate the conditional
statements.
// 1.a. Program using if else
using System;
class ifdemo
{
public static void Main()
{
int a,b;
Console.WriteLine("enter 2 no ");
a=int.Parse (Console.ReadLine());
b=int.Parse(Console.ReadLine());
if(a>b)
{
Console.WriteLine(a+" is greater");
}
else if(a< b)
{
Console.WriteLine(b+" is greater");
}
else
{
Console.WriteLine("Both "+a+" and "+b+" are Equal");
}
Console.ReadLine();
}
}
// 1.b. Program using switch
using System;
namespace ConditionalStatementDemo
{
class Switchdemo
{
public static void Main()
{
Console.WriteLine("Which is your fav. color");
Console.WriteLine("1. Red");
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 2
Console.WriteLine("2. Green");
Console.WriteLine("3. Pink");
int ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
Console.WriteLine("you choose Red");
break;
case 2 :
Console.WriteLine("you choose Green");
break;
case 3:
Console.WriteLine("you choose Pink");
break;
default:
Console.WriteLine("None of given colors..");
break;
}
Console.ReadLine();
}
}
}
Output:
Which is your fav. color
1. Red
2. Green
3. Pink
8
None of given colors..
Which is your fav. color
1. Red
2. Green
3. Pink
2
you choose Green
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 3
2. Develop a C# .NET console application to demonstrate the control
statements.
// 2.a. Program using for loop
using System;
namespace ConditionalStatementDemo
{
class ForLoop
{
public static void Main()
{
Console.WriteLine("Printing first 20 numbers using For");
for (int i = 0; i <= 20; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Output:
Printing first 20 numbers using For
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 4
17
18
19
20
// 2.b. program using while loop
using System;
namespace ConditionalStatementDemo
{
class WhileDemo
{
public static void Main()
{
int x=0;
Console.WriteLine("Printing Even nos. less than 50 using while: ");
while (x <= 50)
{
Console.WriteLine(x);
x=x+2;
}
}
}
}
3. Demonstrate Multithreaded Programming in C#.NET
// 3. Multithreading
using System;
using System.Threading;
namespace ThreadingDemo
{
class Program1
{
public static void Main()
{
Console.WriteLine("Starting Main Thread");
Thread t1 = new Thread(Table7)
{
Name = "Table-7"
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 5
};
Thread t2 = new Thread(Table3)
{
Name = "Table-3"
};
t1.Start();
t2.Start();
Console.WriteLine("Exiting Main Thread");
Console.Read();
}
static void Table7()
{
Console.WriteLine("Starting " + Thread.CurrentThread.Name);
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("7 * " + i + " = " + 7 * i);
Thread.Sleep(2000);
}
Console.WriteLine("Exiting " + Thread.CurrentThread.Name);
}
static void Table3()
{
Console.WriteLine("Staring " + Thread.CurrentThread.Name);
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("3 * " + i + " = " + 3 * i);
Thread.Sleep(3000);
}
Console.WriteLine("Exiting " + Thread.CurrentThread.Name);
}
}
}
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 6
4. Demonstrate subroutines and functions in C#.NET.
// 4. Subroutine and functions
using System;
namespace ConsoleApplication2
{
class Subroutine
{
public static void Main()
{
Console.WriteLine("Arithmetic Operations:");
Console.WriteLine("Enter two numbers:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.WriteLine("1. Add");
Console.WriteLine("2. Sub");
Console.WriteLine("Enter Your Choice: ");
int ch = int.Parse(Console.ReadLine());
if (ch != 1 && ch != 2)
{
Console.WriteLine("Wrong Choice!!");
}
else if (ch == 1)
{
add(a, b);
}
else
{
int result = sub(a, b);
Console.WriteLine("Subtraction=" + result);
}
Console.Read();
}
static void add(int n, int m)
{
Console.WriteLine("Addition=" + (n + m));
}
static int sub(int n, int m)
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 7
{
return (n - m);
}
}
5. Construct a console application to demonstrate the OOP Concepts
// 5. Oops concepts – class, object, polymorphism
using System;
namespace ConsoleApplication1
{
class student
{
int roll;
String name;
int m1, m2, m3;
int tot;
float avg;
student(int r,String n) //constructor
{
roll=r;
name=n;
}
void marks() //Polymorphism
{
m1 = 90; m2 = 95; m3 = 80;
}
void marks(int a,int b,int c) //Polymorphism
{
m1 = a; m2 = b; m3 = c;
}
void cal()
{
tot = m1 + m2 + m3;
avg = tot / 3;
}
void put()
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 8
{
Console.WriteLine("Student Roll: " + roll);
Console.WriteLine("Student Name: " + name);
Console.WriteLine("Total: " + tot);
Console.WriteLine("Percentage: " + avg);
}
public static void Main()
{
student s1 = new student(10, "Anu");
student s2 = new student(12, "Rakesh");
s1.marks();
s2.marks(50, 60, 70);
s1.cal();
s2.cal();
Console.WriteLine("Student details:");
s1.put();
Console.WriteLine();
s2.put();
Console.Read();
}
}
}
6. Develop an application in C#.NET that demonstrates the windows controls.
Select New Project-> Visual C# -> Windows Forms Application.
Use Toolbox and Properties window.
Create the design in the form.
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 9
Design of Form1.cs
NOTE:
● For textBox3 -> In Properties window -> ReadOnly is set to True.
● For ListBox2 -> In Properties window -> in the Items property -> the
different colour names are entered.
C# code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox3.Text = int.Parse(textBox1.Text) + int.Parse(textBox2.Text)+"";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 10
textBox3.Text = int.Parse(textBox1.Text) - int.Parse(textBox2.Text)+"";
}
private void button1_Click(object sender, EventArgs e)
{
switch(listBox1.SelectedIndex)
{
case 0:
listBox1.BackColor = Color.Red;
break;
case 1:
listBox1.BackColor=Color.Blue;
break;
case 2:
listBox1.BackColor=Color.Yellow;
break;
case 3:
listBox1.BackColor=Color.Green;
break;
}
}
7. Develop a web application in VB.NET for dynamic Login Processing
Select New Project-> Under Installed Templates-> Visual Basic -> Windows
Forms Application.
Use Toolbox and Properties window.
Create the design in the form.
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 11
Design of Form1.vb:
NOTE:
● For textBox2 -> In Properties window -> PasswordChar is set to *.
VB code:
Public Class Form1
Private Sub Button1_Click( ) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Enter Username.." , , "Login Screen")
TextBox1.Focus()
ElseIf TextBox2.Text = "" Then
MsgBox("Enter Password.." , , "Login Screen")
TextBox2.Focus()
ElseIf TextBox1.Text = "MSCW" And TextBox2.Text = "BCA" Then
MsgBox("Login Success!!" , , "Login Screen")
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 12
Else
MsgBox("Incorrect Username/Password. Try Again!!" , , "Login
Screen")
End If
End Sub
Private Sub Button2_Click() Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub
End Class
8. Develop an application for deploying various built-in functions in VB.NET
Select New Project-> Under Installed Templates-> Visual Basic -> Windows
Forms Application.
Use Toolbox and Properties window.
Create the design in the form.
Design of Form1.vb:
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 13
NOTE:
· To create shortcut key for Button1, In Properties window -> Text is
entered as &Length
o So now L is the shortcut key. During execution, instead of
button click, Alt+L is pressed.
· Similarly shortcuts are assigned for all the remaining Buttons.
VB code:
Public Class Form1
Private Sub Button1_Click() Handles Button1.Click
MsgBox(Len(TextBox1.Text))
End Sub
Private Sub Button2_Click() Handles Button2.Click
MsgBox(StrReverse(TextBox1.Text), , "String Reverse")
End Sub
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 14
Private Sub Button3_Click() Handles Button3.Click
Dim str As String
str = InputBox("Enter a string to compare:", "String Comparison")
If StrComp(TextBox1.Text, str) = 0 Then
MsgBox("Both are Equal", , "String Comparison")
Else
MsgBox("Not Equal Strings", , "String Comparison")
End If
End Sub
Private Sub Button4_Click() Handles Button4.Click
TextBox1.Text = Trim(TextBox1.Text)
End Sub
Private Sub Button5_Click() Handles Button5.Click
TextBox1.Text = UCase(TextBox1.Text)
End Sub
Private Sub Button6_Click() Handles Button6.Click
TextBox1.Text = LCase(TextBox1.Text)
End Sub
Private Sub Button7_Click() Handles Button7.Click
Dim c As Char
c = InputBox("Enter the character to replace:", "Replace")
TextBox1.Text = Replace(TextBox1.Text, c, "#")
End Sub
Private Sub Button8_Click( ) Handles Button8.Click
Close()
End Sub
End Class
9. Develop an MDI application for Employee Pay-roll transactions in VB.NET
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 15
Select New Project-> Under Installed Templates-> Visual Basic -> Windows
Forms Application.
Use Toolbox and Properties window.
Create the design in the form.
Design of Form1.vb:
NOTE:
● In Form1 - IsMdiContainer is set to True.
Design of Form2.vb:
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 16
VB Code in Form1:
Public Class Form1
Public empname As String
Public desig As String
Public basic As Decimal
Public grosssal As Decimal
Public netsal As Decimal
Private Sub Button1_Click() Handles Button1.Click
Form2.MdiParent = Me
empname = TextBox1.Text
desig = TextBox2.Text
basic = Int(TextBox3.Text)
grosssal = basic + (basic * 0.34) + (basic * 0.16)
netsal = grosssal - (Int(TextBox4.Text) + Int(TextBox5.Text))
Form2.Show()
End Sub
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 17
Private Sub Button3_Click() Handles Button3.Click
Me.Close()
End Sub
End Class
VB Code in Form2:
Public Class Form2
Private Sub Form2_Load() Handles MyBase.Load
Label3.Text = DateTime.Now.ToString("MMMM")
Label4.Text = Form1.empname
Label5.Text = Form1.desig
Label6.Text = Str(Form1.grosssal)
Label8.Text = Str(Form1.netsal)
End Sub
End Class
Output Screen:
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 18
Smt.Sripavithra C K, Asst. Prof., MSCW, Mysore. 19