0% found this document useful (0 votes)
30 views

C#Program

3rd Sem BCA Bangalore north university C # .NET Framework lab manual

Uploaded by

Asaniya Sen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

C#Program

3rd Sem BCA Bangalore north university C # .NET Framework lab manual

Uploaded by

Asaniya Sen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

C# and Dot Net Framework lab manual

PROGRAM LIST
PART A
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. Develop an application in C#. NET that demonstrates the window controls
4. Demonstrate Multithreaded Programming in C#.NET
5. Demonstrate subroutines and functions in C#.Net
6. Develop an application for deploying various built-in functions in VB .Net
7. Develop an MDI application for Employee Pay-roll transactions in VB.NET
8. Construct a console application to demonstrate the OOP Concepts
9. Develop a web application in VB.NET for dynamic Login Processing
10. Develop a Windows application with database connectivity for core-banking
transactions

PART B
1. C# console program to check whether the number is palindrome or not
2. C# console program to display Factorial of a number
3. Write a C# Program for Dental Payment Form. Calculate total on selected options
from check boxes.

4. Write a C# Program to display the reverse of a given number using function. (Accept
number through textbox and display result using message box)

5. Write a C# program to simulate the traffic signal


6. Write a VB Program to design a simple calculator to perform addition, subtraction,
multiplication and division
7. Write a VB Program to accept birth date from user and calculate age.

8. Write a VB.Net program to calculate the sum of all elements of an integer array
9. Write a Vb.net program to accept a string from keyboard and count the number of
vowels
10. Create a windows Login application with database connectivity in C#.
2
PART A
1. Develop a C# .NET console application to demonstrate the
conditional statements
using System;

namespace pg1
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("Enter your choice to demonstrate");


Console.WriteLine("1:if else \t 2: nested if \t 3:else if 4:exit");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:

Console.WriteLine("Demonstrating if-else checking number is


odd or even");
Console.WriteLine("Enter a number");
int num = Convert.ToInt32(Console.ReadLine());
if (num % 2 == 0)
Console.WriteLine(num + "is even");
else
Console.WriteLine(num + "is odd");
break;

case 2:

Console.WriteLine("Demonstrating nested if checking largest


of three numbers");
Console.WriteLine("Enter 3 numbers");
int n1 = Convert.ToInt32(Console.ReadLine());
int n2 = Convert.ToInt32(Console.ReadLine());
int n3 = Convert.ToInt32(Console.ReadLine());
if (n1 > n2)
{
if (n1 > n3)
Console.WriteLine(n1 + "is largest");
else
Console.WriteLine(n3 + "is largest");
}
else
{
if (n2 > n3)
3
Console.WriteLine(n2 + "is largest");
else
Console.WriteLine(n3 + "is largest");
}
break;

case 3:

Console.WriteLine("Demonstrating nested if :checking


NUMBER IS LESS THAN or GREATER THAN OR EQUAL TO ZERO");
Console.WriteLine("Enter a number");
int n= Convert.ToInt32(Console.ReadLine());
if (n > 0)
Console.WriteLine("{0} IS greater than zero",n);
else if(n==0)
Console.WriteLine("{0} IS equal to zero",n);
else
Console.WriteLine("{0} IS less than zero", n);
break;

default:
Console.WriteLine("invalid");
break;

}
}
}

4
2. Develop a C# .NET console application to demonstrate the
control statements
using System;

namespace pg2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your choice to demonstrate");
Console.WriteLine("1:while \t 2: do-while \t 3:for 4:foreach");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:

Console.WriteLine("Demonstrating while :display numbers 1 to n");


Console.WriteLine("Enter a number");
int num = Convert.ToInt32(Console.ReadLine());
int i = 1;
while (i <= num)
{
Console.WriteLine("i={0}", i);
i++;
}

break;
case 2:
Console.WriteLine("Demonstrating do while :display numbers 1 to
5");
int x = 1;

do
{
Console.WriteLine("i = {0}", x);
x++;

} while (x <=5);
break;

case 3:

Console.WriteLine("Demonstrating for loop :read and sum the


elements in an array");
int[] a = new int[5];
int total = 0;
Console.WriteLine("Enter 5 elements into array");
for (int k = 0; k < a.Length; k++)
{
a[k] = Convert.ToInt32(Console.ReadLine());
total += a[k];
5
}
Console.WriteLine(" the sum of elements of array are" + total);
break;

case 4:
Console.WriteLine("Demonstrating foreach loop :read and sum the
elements in an array");
int[] b = new int[5];
int sum = 0;
Console.WriteLine("Enter 5 elements into array");
foreach (int j in b)
{
b[j] = Convert.ToInt32(Console.ReadLine());
sum += b[j];

}
Console.WriteLine(" the sum of elements of array are" + sum);
break;

default:
Console.WriteLine("invalid");
break;

}
}
}
}

6
3. Develop an application in C# . NET that demonstrates the
window controls

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("Registration successfull");
}

7
4. Demonstrate Multithreaded Programming in C#.NET
using System;

using System.Threading;

class Program {

public static void Main() {

Thread ThreadObject1 = new Thread(Example1); //Creating the Thread

Thread ThreadObject2 = new Thread(Example2);

ThreadObject1.Start(); //Starting the Thread

ThreadObject2.Start();

static void Example1() {

Console.WriteLine("Thread1 Started");

for (int i = 0; i <= 5; i++) {

Console.WriteLine("Thread1 Executing");

Thread.Sleep(5000); //Sleep is used to pause a thread and 5000 is MilliSeconds that means 5 Seconds

static void Example2() {

Console.WriteLine("Thread2 Started");

for (int i = 0; i <= 5; i++) {

Console.WriteLine("Thread2 Executing");

Thread.Sleep(5000);

Output:

8
5. Demonstrate subroutines and functions in C#.Net
using System;
public class A
{
public static void Main(string[] args)
{
A ob=new A();
int sum;
ob.greetme(); // calls subroutine
Console.WriteLine ("Enter 2 numbers to add");
int a=Convert.ToInt32(Console.ReadLine());
int b=Convert.ToInt32(Console.ReadLine());
sum=ob.add(a,b); //calls function
Console.WriteLine ("sum is {0}",sum);

}
public void greetme() //This is a subroutine
{
Console.WriteLine ("Hello World");
}
public int add(int x, int y) // This is a function
{
int total=x+y;
return total;
}
}

9
6. Develop an application for deploying various built-in
functions in VB .Net

Public Class UserControl1


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As String
a = "I am new to VB"
Dim l As Integer
l = Len(a)
MsgBox(l)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Dim i As Integer
i = Asc("A")
MsgBox(i)

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


Dim i As String
i = Chr(66)
MsgBox(i)
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click


Dim i As Boolean
Dim a As String
a = "asdfg"
i = IsNumeric(a)

10
MsgBox(i)
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


Dim i As Integer
Dim a As String
i = 20
a = Format(i, "Currency")
MsgBox(a)
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click


Dim i As String
i = LCase("GOOD")
MsgBox(i)
End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click


Dim i As String
i = UCase("asdf")
MsgBox(i)
End Sub
End Class
output :

11
7. Develop an MDI application for Employee Pay-roll
transactions in VB.NET

Add menu strip to the form, add the sub menus required.

To set form1 as MDI under properties set is MDI container as true

To add new forms go to solution explorer right click on project name, click on add, then forms, create.

// code to make personal info click to take to form 2


Public Class Form1
Private Sub PersonalinfoToolStripMenuItem_Click(sender As Object, e As
EventArgs) Handles PersonalinfoToolStripMenuItem.Click
Form2.Show()
End Sub
End Class

// to make the form 1 as the parent form of form 2.


12
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles


Button1.Click
MessageBox.Show("saved successfully")
End Sub

Pri vate Sub Button2_Click(sender As Object, e As EventArgs) Handles


Button2.Click
Form3.Show()
End Sub
End Class

Public Class Form3

Dim ans As Integer


Dim deduc As Integer
Dim tax As Integer
Dim phil As Integer
Dim s As Integer
Dim net As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
ans = ((TextBox2.Text * TextBox3.Text) * (TextBox1.Text))
TextBox4.Text = ans
TextBox9.Text = ans

13
tax = ans * 0.15
TextBox5.Text = tax
phil = ans * 0.05
TextBox6.Text = phil
s = ans * 0.11
TextBox7.Text = s
deduc = tax + phil + s
TextBox8.Text = deduc
TextBox10.Text = deduc

net = TextBox9.Text - TextBox10.Text


TextBox11.Text = net
End Sub
End Class

8. Construct a console application to demonstrate the OOP


Concepts
class abc
{
public virtual void operation(int a, int b)
{
int mul;
mul = a * b;
Console .WriteLine ("OPERATION IS MULTIPLICATION {0}*{1}={2}",a,b,mul);

}
}
class Pqr : abc
{
public override void operation(int a, int b)
{
int total;
total = a + b;
Console.WriteLine("OPERATION IS ADDITIION {0}+{1}={2}", a, b, total);
base.operation(a, b);
}
}
Class program
14
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Enter 2 numbers ");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Pqr ob1 = new Pqr();
ob1.operation(a,b);
}
}

15
9. Develop a web application in VB.NET for dynamic Login
Processing

Public Class Form1


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim username As String
Dim password As String
username = Textuser.Text
password = Textpass.Text
If (username.Equals("admin") And password.Equals("admin")) Then
MessageBox.Show("Login Successfull", "information",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Error", "information", MessageBoxButtons.OK,
MessageBoxIcon.Information)

End If
End Sub
End Class

(* windows application simulating login processing)

16
Part B
1. C# console program to check whether the
number is palindrome or not
using System;
public class Palindrome
{
public static void Main(string[] args)
{
int n,r,sum=0,temp;
Console.Write("Enter the Number: ");
n = Convert.ToInt32(Console.ReadLine());
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10) +r;
n=n/10;
}
if(temp==sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
}
}
Output:
Enter the Number=121
Number is Palindrome.
Enter the number=113
Number is not Palindrome

17
2. C# console program to display Factorial of a number
using System;
public class Factorial
{
public static void Main(string[] args)
{
int i,fact=1,number;
Console.Write("Enter any Number: ");
number= Convert.ToInt32(Console.ReadLine());
for(i=1;i<=number;i++){
fact=fact*i;
}
Console.Write("Factorial of " +number+" is: "+fact);
}
}

18
3. Write a C# Program for Dental Payment Form. Calculate
total on selected options from check boxes.

private void button1_Click(sender object, e EventArgs)


{
int sum = 0;
if (checkBox1.Checked == true)
{
sum += 35;
}
if (checkBox2.Checked==true)
{
sum += 150;
}
if (checkBox3.Checked==true)
{
sum += 85;
}
if (checkBox4.Checked==true)
{
sum += 50;
}
if (checkBox5.Checked==true)
{
sum += 800;
19
}
if (checkBox6.Checked==true)
{
sum =sum+(Convert.ToInt32(textBox4.Text));
}

label3.Text = sum.ToString();
}

4. Write a C# Program to display the reverse of a given


number using function. (Accept number through textbox
and display result using message box)

private void button1_Click( sender Object, e EventArgs )


{
int n,a,r=0;
n = Convert.ToInt32(textBox4.Text);
while(n !=0)
{
a = n %10;
r = r * 10 + a;
n = n /10;
}
MessageBox.Show ("reverse is" + r)
}
20
5. Write a C# program to simulate the traffic signal

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace traffic
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button4.Visible = true; //button 4 is red
button5.Visible = false; // button 5 is yellow
button6.Visible = false; // button 6 is green
}

private void button7_Click(object sender, EventArgs e)// start button


{
timer2.Enabled = true;
}

private void button8_Click(object sender, EventArgs e)// stop button


{
timer2.Enabled = false ;
21
}

private void timer2_Tick(object sender, EventArgs e)


{
if (button4.Visible == true)
{
button4.Visible = false;
button5.Visible = false;
button6.Visible = true;
}
else if (button6.Visible == true)
{
button4.Visible = false;
button5.Visible = true;
button6.Visible = false;
}
else if (button5.Visible == true)
{
button4.Visible = true;
button5.Visible = false;
button6.Visible = false;
}
}
}
}

22
Along with car image
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Visible = true;
button2.Visible = false;
button3.Visible = false;
23
}

private void button1_Click(object sender, EventArgs e)


{

private void timer1_Tick(object sender, EventArgs e)


{
if (button1.Visible ==true)
{
button1.Visible = false;
button2.Visible = false;
button3.Visible = true;
}
else if(button3.Visible ==true)
{
button1.Visible = false;
button2.Visible = true ;
button3.Visible = false;

}
else if (button2.Visible ==true )
{
button1.Visible = true ;
button2.Visible = false;
button3.Visible = false ;

private void timer2_Tick(object sender, EventArgs e)


{
if(button1.Visible ==false && button3 .Visible ==true )
{
pictureBox1.Left = pictureBox1.Left + 5;

}
else if (button3.Visible ==false && button2 .Visible ==true )
{
pictureBox1.Left = pictureBox1.Left + 2 ;

24
}
else if (button2.Visible ==false && button1.Visible ==true)
{
pictureBox1.Left = pictureBox1.Left + 0; ;
}
}

private void button4_Click(object sender, EventArgs e)


{
timer1.Enabled = true;
timer2.Enabled = true;
}

private void button5_Click(object sender, EventArgs e)


{
timer1.Enabled = false;
timer2.Enabled = false;
}
}
}

6. Write a VB Program to design a simple calculator to


perform addition, subtraction, multiplication and division

25
Public Class Form1
Dim Operand1 As Double
Dim Operand2 As Double
Dim op As String

Dim Result As Double

Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click

Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
op = "+"
End Sub

'C to clear
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
TextBox1.Text = ""

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


TextBox1.Text = TextBox1.Text & sender.text

End Sub

Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


26
TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


TextBox1.Text = TextBox1.Text & sender.text
End Sub

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click


Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
op = "-"
End Sub

Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click


Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
op = "/"
End Sub

Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click


Operand1 = Val(TextBox1.Text)
TextBox1.Text = ""
op = "*"
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Operand2 = Val(TextBox1.Text)

Select Case op
Case "+"
Result = Operand1 + Operand2
TextBox1.Text = Result
Case "-"
Result = Operand1 - Operand2
TextBox1.Text = Result
Case "/"
Result = Operand1 / Operand2
TextBox1.Text = Result
Case "*"
Result = Operand1 * Operand2
TextBox1.Text = Result
End Select

End Sub

End Clas

27
7)Write a VB Program to accept birth date from user and
calculate age.

Public Class Form1


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim yr As Integer = DateDiff(DateInterval.Year, DateTimePicker1.Value,
Now)
Dim month As Integer = DateDiff(DateInterval.Month,
DateTimePicker1.Value, Now) Mod 12
Dim day As Integer = DateDiff(DateInterval.Day, DateTimePicker1.Value,
Now) Mod 30
TextBox1.Text = yr & " Years, " & month & " Months " & day & "days"
End Sub
End Class

28
8) Write a VB.Net program to calculate the sum of all
elements of an integer array
Module Module1

Sub Main()
Dim arr As Integer() = New Integer(5) {}
Dim sum As Integer = 0

Console.WriteLine("Enter array elements: ")


For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
sum = sum + arr(i)
Next

Console.WriteLine("Sum of array elements is: {0}", sum)


End Sub

End Module

Output:

9) Write a Vb.net program to accept a string from keyboard and


count the number of vowels

29
Public Class Form1

Dim i, length, vowel As Integer


Dim ex As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


length = Len(Trim(TextBox1.Text))
For i = 1 To length
ex = Mid(TextBox1.Text, i, 1)

vowel = vowel + 1
End If
Next i
TextBox2.Text = vowel
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


TextBox2.Text = length - vowel
End Sub
End Class

30
10. Create a windows Login application with database
connectivity.
Windows application with database connectivity steps

Step 1:Open C# windows Application in visual studio

31
Step 2: Design Login form

Label Text Box

Button

Step 3: Open Sql Server Management Tool and connect

Step 4: Steps to create database in sql server

32
Create database
Click on new database

Enter database name and click ok

Select the newly created database


Click on table-new table

Enter the column values with


datatypes

33
Save the table (press ctrl+s to save
table)

Click on newly created table select


edit top 200 rows

Enter row values

Step 5: Open visual studio and connect to database


Steps to Connect Database.

34
Go to View -Open
server explorer

Select data connection


click on add
connection(new
window will
appear)

Click on change
select Microsoft
sql server -click
ok

35
Enter server name and
select database from
dropdown list-click ok

Database will appear on


the server explorer

Step 6: Adding connection string in coding


Double click on login button

Add namespace using System.Data.SqlClient;

Add the following code


using System;
using System.Data.SqlClient;

namespace log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Loginform;Integrated Security=True");

36
SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM [logintable] WHERE
username='" + textBox1.Text + "' AND password='" + textBox2.Text + "'", con);

DataTable dt = new DataTable(); //this is creating a virtual table


sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
MessageBox.Show("login successfull");
}
else
MessageBox.Show("Invalid username or password");

}
}
}

Step 6: execute the login form

(* this is using sql server and SSMS)


(*This application can be created using the inbuilt service- based database also)

37

You might also like