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

C#.NET-1

The document outlines various C#.NET and VB.NET applications demonstrating programming concepts including conditional statements, control statements, multithreading, functions, OOP concepts, and database connectivity. It includes code examples for while loops, for loops, if-else statements, switch cases, and GUI applications. Additionally, it covers creating a web application for login processing and a Windows application for core banking transactions.

Uploaded by

Sowmya Santosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C#.NET-1

The document outlines various C#.NET and VB.NET applications demonstrating programming concepts including conditional statements, control statements, multithreading, functions, OOP concepts, and database connectivity. It includes code examples for while loops, for loops, if-else statements, switch cases, and GUI applications. Additionally, it covers creating a web application for login processing and a Windows application for core banking transactions.

Uploaded by

Sowmya Santosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

SHREE MEDHA COLLEGE, BALLARI

1.Develop a C#.NET console application to demonstrate the conditional


statements.

a. Develop a C#.NET console application to demonstrate while loop

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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
b. Develop a C#.NET console application to demonstrate For loop.

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

namespace Conditional4
{
class Program
{
static void Main(string[] args)
{
int n, i;
Console.WriteLine("Enter the number");
n = Convert.ToInt32(Console.ReadLine());

for (i = 0; i <= n; i++)


{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}

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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
2.Develop a C#.NET console application to demonstrate the control statements.

a. Develop a C#.NET console application to demonstrate if else statement.

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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
b. Develop a C#.NET console application to demonstrate switch case statement.

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

namespace Conditional5
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
char op;
Console.WriteLine("Enter the values of A and B");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(1.Addition(+)\n2.Subtraction()\n3.Multiplication(*)\n4.Divison(/)\
n5.Modulus(%)");
Console.WriteLine("Enter the operation");
op = Convert.ToChar(Console.ReadLine());

switch (op)
{
case '+': c = a + b;
Console.WriteLine("Addition =" + c);
break;
case '-': c = a - b;
Console.WriteLine("Subtraction =" + c);
break;
case '*': c = a * b;
Console.WriteLine("Multiplication =" + c);
break;
case '/': c = a / b;
Console.WriteLine("Division =" + c);
break;
case '%': c = a % b;
Console.WriteLine("Mod =" + c);
break;
default: Console.WriteLine("you entered wrong operator");
break;
}
Console.ReadLine();
}
}
}

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
Dim net As Integer
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
8. Construct a console application to demonstrate the OOP concepts.

a. Construct a console application to demonstrate the Class and Objects.

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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
public static void Main(String[] args)
{
Student s = new Student("Giriraj", "BCA", 20, "Ballari");
Console.WriteLine(s.ToString());
Console.ReadLine();
}
}
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
b. Construct a console application to demonstrate the Inheritance.

using System;
public class Address
{
public string addressLine, city, state;
public Address(string addressLine, string city, string state)
{
this.addressLine = addressLine;
this.city = city;
this.state = state;
}
}
public class Employee
{
public int id;
public string name;
public Address address;//Employee HAS-A Address
public Employee(int id, string name, Address address)
{
this.id = id;
this.name = name;
this.address = address;
}
public void display()
{
Console.WriteLine(id + " " + name+ "\n " +address.addressLine +"\n"+address.city
+"\n"+address.state);
}
}
public class TestAggregation
{
public static void Main(string[] args)
{
Address a1 = new Address("Fort, Main Road", "Ballari", "Karnataka");
Employee e1 = new Employee(1, "Girirajshekar", a1);
e1.display();
Console.ReadLine();
}
}

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
C. Construct a console application to demonstrate the polymorphism.

using System;
public class Shape
{
public virtual void draw()
{
Console.WriteLine("drawing...");
}
}
public class Rectangle : Shape
{
public override void draw()
{
Console.WriteLine("drawing rectangle...");
}

}
public class Circle : Shape
{
public override void draw()
{
Console.WriteLine("drawing circle...");
}
}
public class TestPolymorphism
{
public static void Main()
{
Shape s;
s = new Shape();
s.draw();
s = new Rectangle();
s.draw();
s = new Circle();
s.draw();
Console.ReadLine();

}
}
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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 SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
********************OUTPUT********************

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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


> Add table bank with fields
(Name(varchar),AccNo(varchar),AccType(Varchar),Amount
(int) ,Pin(int))

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI

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.


Step1:

File> New> Project

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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
Step2:

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

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

Balance Button Code :

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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
End Class
Step3:

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

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()

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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
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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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()
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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
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
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

DEPT. OF COMPUTER SCIENCE Page No.___


SHREE MEDHA COLLEGE, BALLARI
Balance Form Design Accordingly:

Change textbox and label Properties accordingly:


TextBox name properties: txtAccNo, txtPin
Labels Text properties: Account Number, Pin
Button text : Balance
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 SCIENCE Page No.___

You might also like