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

C#.NET LAB PROGRAMS

The document outlines a series of programming tasks and applications to be developed using C# and VB.NET, including console applications demonstrating conditional and control statements, multithreading, and object-oriented programming concepts. It also includes the creation of a web application for dynamic login processing and a Windows application with database connectivity for core banking transactions. Each task is accompanied by example code and expected outputs, aimed at enhancing understanding of the .NET framework and programming principles.

Uploaded by

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

C#.NET LAB PROGRAMS

The document outlines a series of programming tasks and applications to be developed using C# and VB.NET, including console applications demonstrating conditional and control statements, multithreading, and object-oriented programming concepts. It also includes the creation of a web application for dynamic login processing and a Windows application with database connectivity for core banking transactions. Each task is accompanied by example code and expected outputs, aimed at enhancing understanding of the .NET framework and programming principles.

Uploaded by

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

C# AND .

NET FRAMEWORK
SL.NO NAME OF PROGRAMS
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 and demonstrates the


windows 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.
C# and .Net framework lab programs
1. Develop a C#.NET console application to demonstrate the conditional statements.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Conditional3
{
class Program
{
static void Main(string[] args)
{
int i = 1;
Console.WriteLine("1 to 10 Numbers");
while(i<=10)
{
Console.WriteLine(i);
i++;
}
Console.ReadLine();
}
}
}

********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
2. Develop a C#.NET console application to demonstrate the control statements.

using System;

class Test
{
static public void Main()
{
string x = "Shree medha Degree";
string y = "BCA";

if (x == y)
{
Console.WriteLine("Both strings are equal..!!");
}
else
{
Console.WriteLine("Both strings are not equal..!!");
}
Console.ReadLine();
}
}

********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
3. Develop an application in C#.NET and demonstrates the windows controls.

using System;
using System.Windows.Forms;

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

private void button1_Click(object sender, EventArgs e)


{
String name, course, year, gender=" ", hobbies=" ";
String date;
name = textBox1.Text;
date = Convert.ToString(dateTimePicker1.Text);
course = comboBox1.Text;
if (radioButton1.Checked == true)
gender = radioButton1.Text;
else if (radioButton2.Checked == true)
gender = radioButton2.Text;
if (checkBox1.Checked == true)
hobbies = checkBox1.Text;
if (checkBox2.Checked == true)
hobbies =hobbies +" "+ checkBox2.Text;
MessageBox.Show("Name : " + name + "\nDOB : " + date + "\nGender : " + gender +
"\nCourse : " + course + "\nHobbies : " + hobbies);
}

private void button2_Click(object sender, EventArgs e)


{
Close();
}
}
}

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
4. Demonstrate Multithreaded Programming in C#.NET.
using System;
using System.Threading;

public class AOne


{
public void First()
{
Console.WriteLine("First method of AOne class is running on T1 thread.");
Thread.Sleep(1000);
Console.WriteLine("The First method called by T1 thread has ended.");
}

public static void Second()


{
Console.WriteLine("Second method of AOne class is running on T2 thread.");
Thread.Sleep(2000);
Console.WriteLine("The Second method called by T2 thread has ended.");
}
}

public class ThreadExp


{
public static int Main(String[] args)
{
Console.WriteLine("Example of Threading");
AOne a = new AOne();
Thread T1 = new Thread(new ThreadStart(a.First));
T1.Start();
Console.WriteLine("T1 thread strated.");
Thread T2 = new Thread(new ThreadStart(AOne.Second));
T2.Start();
Console.WriteLine("T2 thread started.");
Console.ReadLine();
return 0;
}
}

********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
5. Demonstrate subroutines and functions in C#.NET.
using System;
class Program
{
public void Function(int x, int y)
{
int res = x + y;
Console.WriteLine("Sum of two Values=" + res);
}
public int Subroutine(int x, int y)
{
return x + y;
}
class test
{
static void Main(string[] args)
{
Program p = new Program();
int x = 18, y = 20;
Console.WriteLine("Demonstration of function and Subroutine using C#");
p.Function(x, y);
p.Subroutine(x, y);
Console.ReadLine();
}
}
}

********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
6. Develop an application for deploying various built-in functions in VB.NET.
Public Class Form1

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


TextBox3.Text = Len(TextBox1.Text)
End Sub

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


TextBox3.Text = StrReverse(TextBox1.Text)
End Sub

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


TextBox3.Text = UCase(TextBox1.Text)
End Sub

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


TextBox3.Text = LCase(TextBox1.Text)
End Sub

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


TextBox3.Text = Trim(TextBox1.Text)
End Sub

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


Dim n1, n2 As Integer
n1 = Val(TextBox1.Text)
n2 = Val(TextBox2.Text)
TextBox3.Text = Math.Max(n1, n2)

End Sub

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


Dim n1, n2 As Integer
n1 = Val(TextBox1.Text)
n2 = Val(TextBox2.Text)
TextBox3.Text = Math.Min(n1, n2)

End Sub

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


Dim n1 As Integer
n1 = Val(TextBox1.Text)
TextBox3.Text = Math.Round(n1)

End Sub

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


TextBox3.Text = Math.Sqrt(TextBox1.Text)

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
End Sub

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


TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()

End Sub
End Class

********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
7. Develop an MDI application for Employee Pay-roll transactions in VB.NET.
Form_1.vb :

Public Class Form1


Private Sub CreateSalaryToolStripMenuItem_Click(sender As Object, e As EventArgs)
Handles CreateSalaryToolStripMenuItem.Click
Hide()
Dim sf As Form2 = New Form2()
sf.ShowDialog()
sf.Name = "Create Salary"

End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs)


Handles MyBase.FormClosing
Application.Exit()
End Sub
End Class

Form2.vb :
Public Class Form2

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


Dim eid As String = TextBox1.Text
Dim ename As String = TextBox2.Text
Dim basic As Integer = Convert.ToInt32(TextBox3.Text)
Dim ta As Integer = Convert.ToInt32(TextBox4.Text)
Dim da As Integer = Convert.ToInt32(TextBox5.Text)
Dim pt As Integer = Convert.ToInt32(TextBox6.Text)
Dim gross As Integer
gross = basic + ta + da
Dim net As Integer

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
net = gross - pt
MsgBox(String.Format("Eid:{0} Ename:{1} Gross:{2} Net:{3}", eid, ename, gross,
net))
End Sub
End Class

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
8. Construct a console application to demonstrate the OOP’S concepts.

using System;
public class Student
{
String name;
String course;
int age;
String address;

public Student(String name, String course,


int age, String address)
{
this.name = name;
this.course = course;
this.age = age;
this.address = address;
}

public String GetName()


{
return name;
}

public String GetCourse()


{
return course;
}

public int GetAge()


{
return age;
}

public String GetAddress()


{
return address;
}

public String ToString()


{
return ("Hi my name is " + this.GetName()+ ".\nMy course, age and address are " +
this.GetCourse()+ ", " + this.GetAge() + ", " + this.GetAddress());
}

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
public static void Main(String[] args)
{
Student s = new Student("Giriraj", "BCA", 20, "Ballari");
Console.WriteLine(s.ToString());
Console.ReadLine();
}
}
********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs

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 uname As String
Dim pwd As String
uname = TextBox1.Text
pwd = TextBox2.Text
If (uname = "Student" And pwd = "1234") Then
MsgBox("Login Successful!..")
Else
MsgBox("Login Failed!.." & vbCrLf & "Incorrect Username or Password!..")
End If
End Sub

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


Me.Close()
End Sub
End Class

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs

********************OUTPUT********************

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs

10. Develop a Windows application with database connectivity for core banking
transactions

 Design table in Sqlserver Object Explorer

Goto -> Sql Server Object Explorer

> Create database Csharp Lab

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
> Add table bank with fields
(Name(varchar),AccNo(varchar),AccType(Varchar),Amount
(int) ,Pin(int))

Connection String:

Rightclick on Csharp Database > Properties > Find connection String

Copy Connection String paste in Vb.net Code.

 Connection string changing depending on your database and location.

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
Step1:

File> New> Project

 Select Visual Basic Left side Panel and Right Side Windows Forms App

Step2:

 Add 4 Buttons from tools Change ‘Text ‘ Properties Buttons as per design.

Double click on Create Button , Deposit Button, Withdraw Button,

Balance ButtonCode :

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
Public Class Form1

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


Dim createfrm As New Create
createfrm.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim depositfrm As New Deposit

depositfrm.Show()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim withdrawfrm As New Withdraw
withdrawfrm.Show()
End Sub

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


Dim balancefrm As New Balance
balancefrm.Show()
End Sub
End Class
Step3:

In Solution Explorer Select Project Bank Right Click >Add New Windows Forms 4
times > Named Create, Deposit,Withdraw,Balance

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
Change Name Properties of Textboxes to -> txtName , txtAccNo, txtAccType,
txtAmount, txtPin Label Text properties to Name, Account Number,Account Type,
Amount, Pin as per requirement Button Text Property to Create

Double click on Create

Write Code:

Imports
System.Data.SqlClient
Public Class Create
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim constring As String = "Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Csharplab;Integrated Security=True;Connect
Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=Read
Write;MultiSubnetFailover=False"
Dim con As New SqlConnection() con.ConnectionString =
constring
Dim qry As String = "Insert into [dbo].Bank values('" + txtName.Text + "','" +
txtAccno.Text + "','" + txtAccType.Text + "'," + txtAmount.Text + "," + txtPin.Text + ")"
con.Open()

Dim cmd As New SqlCommand(qry, con)


Dim res As Integer
res=cmd.ExecuteNonQuery()
If res = 1 Then
MsgBox("Account created successfully!")
End If
con.Close()
End Sub

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
End Class

Design Deposit Form as shown Above

Change textbox properties and Label properties

Textbox1(name)- txtAccNo
Textbox2 (name)- txtAmount
Button (Text) – Deposit

Write Code:

Imports System.Data.SqlClient
Public Class Deposit
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim constring As String = "Data Source=(localdb)\MSSQLLocalDB;Initial


Catalog=Csharplab;Integrated Security=True;Connect
Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;
MultiSubnetFailover=False"
Dim con As New SqlConnection() con.ConnectionString =constring
Dim qry As String = "Update [dbo].bank set amount=amount+" + txtAmount.Text +"
where Accno='" + txtAccNo.Text + "'"
con.Open()
Dim cmd As New SqlCommand(qry, con)
Dim res As Integer
res = cmd.ExecuteNonQuery()

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
If res = 1 Then
MsgBox("Amount Deposited successfully!")
End If
con.Close()
End Sub
End Class

Design Withdraw form as specified above Change Textbox Properties and Label
Properties

Textbox Name properties : txtAccNo, txtAmount,txtPin


Label Text Properties: Account Number, Amount, Pin
Button Text Property: Withdraw
Double click on Withdraw button

Write Code in button click event:

Imports System.Data.SqlClient
Public Class Withdraw
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim
constring As String = "Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Csharplab;Integrated Security=True;Connect

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;
Mult iSubnetFailover=False"
Dim con As New SqlConnection() con.ConnectionString = constring
Dim qry As String = "Update [dbo].bank set amount=amount-" + txtAmount.Text + "
where Accno='" + txtAccNo.Text + "' and Pin=" + txtPin.Text + ""
con.Open()
Dim cmd As New SqlCommand(qry, con)
Dim res As Integer
res = cmd.ExecuteNonQuery() If res = 1 Then
MsgBox("Amount Withdrwan successfully!")
End If
con.Close()
End Sub
End Class

Balance Form Design Accordingly:

Change textbox and label Properties accordingly:


TextBox name properties: txtAccNo, txtPin
Labels Text properties: Account Number, Pin
Button text : Balance

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]


C# and .Net framework lab programs
Doubleclick on Button

Write Code:

Imports System.Data.SqlClient
Public Class Balance
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim
constring As String = "Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=Csharplab;Integrated Security=True;Connect
Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;
Mult iSubnetFailover=False"
Dim con As New SqlConnection() con.ConnectionString = constring
Dim qry As String = "Select amount from [dbo].bank where Accno='" + txtAccNo.Text +
"' and Pin=" + txtPin.Text + ""
con.Open()
Dim cmd As New SqlCommand(qry, con)
Dim balance As Object balance = cmd.ExecuteScalar()
MsgBox("Available Balance=" + balance.ToString()) con.Close()
End Sub
End Class

DEPT. OF COMPUTER APPLICATION Vidya Modi[Lecture]

You might also like