0% found this document useful (0 votes)
3 views35 pages

Rohit Hooda .Net File

This document is a practical file for a .NET programming course using C#. It includes various programming exercises in C# such as creating patterns, counting characters in a string, displaying date and time, implementing multiple inheritance, and developing Windows Forms applications including a calculator and a login page. The document is structured with an index and detailed code examples for each program.

Uploaded by

krishnanandtutu
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)
3 views35 pages

Rohit Hooda .Net File

This document is a practical file for a .NET programming course using C#. It includes various programming exercises in C# such as creating patterns, counting characters in a string, displaying date and time, implementing multiple inheritance, and developing Windows Forms applications including a calculator and a login page. The document is structured with an index and detailed code examples for each program.

Uploaded by

krishnanandtutu
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/ 35

PRACTICAL FILE

OF
.NET USING C#

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

Submitted To : Submitted by :
Mrs. Nisha Rani Name : Rohit Hooda
Department of CSE Roll No :210010140069
Class :B.Tech IT 5th Sem

GURU JAMBESHWAR UNIVERSITY OF


SCIENCE AND TECHNOLOGY
Hisar – Haryana ( India )

i
INDEX

S. NO TOPIC PAGE NO. REMARKS


1. Write a program in C# to print 1-6
different ‘*’ patterns
2. Write a console program in C# to count the 7
number of alphabets, digits and special
characters in a
string
3. Write a program in C# to display the Date 8
and Time properties in
different format

4. Write a program in C# that receives the 9-10


following information from a set of students –
Student Id, Student Name, Course Name and
Date of Birth

5. Write a program to implement multiple 11-12


inheritance using interface
6. Create a windows form application 13-18
a.) to make a calculator using virtual buttons

6. Create a windows form application 19-21


b.) to make a calculator using keyboards

7. Create a windows form application to create a 22-25


login page with all buttons functioning (sign
in/login/cancel)

8. Create a .dll file 26

9. Create a web page using ASP.NET 27

10 Create a window based application using 28-33


ADO.Net by using database
(to retrieve and modify the data)

ii
PROGRAM 1 :- Write a program in C# to print different ‘*’ pattern.

Example 1:

using System.IO;
using System;
class Program {
static void Main() {
for (int i = 1; i <= 6; ++i) {
for (int j = 1; j <= i; ++j) {
Console.Write("*");
}
Console.WriteLine();
}
}
}

Output :-
*
**
***
****
*****
******

1
Example 2 :

using System.IO;
using System;
class Program {
static void Main() {
int i, j, k;
int n = 6,
// loop to print the series
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
Console.Write(" ");
}
for (k = 1; k <= i; k++) {
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}

Output :-

*
**
***
****
*****
******

2
Example 3:

using System.IO;
using System;
class Program {
static void Main() {
for (int i = 6; i >= 1; --i) {
for (int j = 1; j >= i; ++j) {
Console.Write("*");
}
Console.WriteLine();
}
}
}
OUTPUT :

******
*****
****
***
**
*

3
Example 4:

using System.IO;
using System;
class Demo {
static void Main() {
int a = 1, spaces, k = 6, i = 0, j = 0;
spaces = k - 1;
for (i = 1; i < k * 2; i++) {
for (j = 1; j <= spaces; j++) {
Console.Write(" ");
}
for (j = 1; j < a * 2; j++) {
Console.Write("*");
}
Console.WriteLine("");
if (i < k) {
spaces--;
a++;
} else {
spaces++;
a--;
}
}
}
}

4
OUTPUT :
*
***
*****
*******
*********
***********
*********
*******
*****
***
*

5
Example 5:

using System;
public class StarPattern {
public static void Main(string[] args) {
int val = 5;
int i, j, k;
for (i = 1; i <= val; i++) {
for (j = 1; j <= val - i; j++) {
Console.Write(" ");
}
for (k = 1; k <= i; k++) {
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
OUTPUT :
*
**
** *
** **
** ***

6
PROGRAM 2 :- Write a C# program to count a total number
alphabets, digits & special characters in a string.

Program :
using System;
namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
Console.WriteLine("Enter a string : ");
string str = Console.ReadLine();
int alpha = 0, digit = 0, spChar = 0;
for (int i = 0; i < str.Length; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
alpha++;
else if (str[i] >= '0' && str[i] <= '9')
digit++;
else
spChar++;
}
Console.WriteLine("Alphabets : {0}", alpha);
Console.WriteLine("Digits : {0}", digit);
Console.WriteLine("Special Characters : {0}", spChar);
}
}
OUTPUT :

7
PROGRAM 3 :- Write a program in c# to display Date Time
properties in different formats.

Program :
using System;
namespace DateAndTime {
class Program {
static int Main() {
DateTime date = new DateTime(2013, 6, 23);
Console.WriteLine("Some Date Formats : ");
Console.WriteLine("Date and Time: {0}", date);
Console.WriteLine(date.ToString("yyyy-MM-dd"));
Console.WriteLine(date.ToString("dd-MMM-yy"));
Console.WriteLine(date.ToString("M/d/yyyy"));
Console.WriteLine(date.ToString("M/d/yy"));
Console.WriteLine(date.ToString("MM/dd/yyyy"));
Console.WriteLine(date.ToString("MM/dd/yy"));
Console.WriteLine(date.ToString("yy/MM/dd"));
Console.Read();
return 0;
}
}
}
OUTPUT :

8
PROGRAM 4 :- Write an application that receives the following
information from a set of students: Student ID, Student Name,
Student DOB, Course Name. Also display the information of all the
students once the data is entered. Implement this using an array of
structure.

Program :
using System;
namespace ConsoleApp1 {
class Program {
struct student {
public long rollNo;
public string name;
public string course;
public string dob;
}
static void Main(string[] args) {
Console.WriteLine("Enter no. of students :");
int n = Convert.ToInt32(Console.ReadLine());
student[] s = new student[n];
for (int i = 0; i < n; i++) {
Console.WriteLine("\nEnter details of student {0}:", i + 1);
Console.Write("Enter Roll No.: ");
s[i].rollNo = Convert.ToInt64(Console.ReadLine());
14 Console.Write("Enter name : ");
s[i].name = Console.ReadLine();
Console.Write("Enter Course : ");
s[i].course = Console.ReadLine();
Console.Write("Enter Date of Birth : ");
s[i].dob = Console.ReadLine();
}
Console.WriteLine("\nStudent details are :\n");

9
for (int i = 0; i < n; i++) {
Console.WriteLine("\nDetails of student {0}:", i + 1);
Console.WriteLine("Roll No. : ", s[i].rollNo);
Console.WriteLine("Name : ", s[i].name);
Console.WriteLine("Course : ", s[i].course);
Console.WriteLine("Date of Birth : ", s[i].dob);
}
}
}
}
OUTPUT :

10
PROGRAM 5 :- Write a program to implement multiple inheritance
using interface.

Program :
// First interface
public interface IDrawable
{
void Draw();
}

// Second interface
public interface IResizable
{
void Resize();
}

// Class implementing both interfaces


public class Shape : IDrawable, IResizable
{
public void Draw()
{
Console.WriteLine("Drawing shape");
}

public void Resize()


{
Console.WriteLine("Resizing shape");
}
}

// Main class
class Program
{
static void Main()
{
// Creating an instance of Shape
Shape myShape = new Shape();

// Calling methods from both interfaces


myShape.Draw();
myShape.Resize();
11
}
}

OUTPUT :

12
PROGRAM 6 :-
A.) Create a window form application to make a calculator using
virtual buttons.

Program :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinFormsApp1 {
public partial class Calculator: Form {
private TextBox textBox1;
private TableLayoutPanel tableLayoutPanel1;
private TableLayoutPanel tableLayoutPanel2;
private Button n1;
private Button n2;
private Button n3;
private Button n4;
private Button n5;
private Button n6;
private Button n7;
private Button n8;
private Button n9;
private Button n0;
private Button badd;
private Button bsub;
private Button bmul;
private Button bdiv;
private Button bdot;

13
private Button bclear;
private Button bequal;
double FirstNumber;
string Operation;
public Calculator() {
InitializeComponent();
}
private void n1_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "1";
} else {
textBox1.Text = textBox1.Text + "1";
}
}
private void n2_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "2";
} else {
textBox1.Text = textBox1.Text + "2";
}
}
private void n3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "3";
} else {
textBox1.Text = textBox1.Text + "3";
}
}
private void n4_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "4";

14
} else {
textBox1.Text = textBox1.Text + "4";
}
}
private void n5_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "5";
} else {
textBox1.Text = textBox1.Text + "5";
}
}
private void n6_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "6";
} else {
textBox1.Text = textBox1.Text + "6";
}
}
private void n7_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "7";
} else {
textBox1.Text = textBox1.Text + "7";
}
}
private void n8_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "8";
} else {
textBox1.Text = textBox1.Text + "8";
}
}

15
private void n9_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null)
{
textBox1.Text = "9";
}
else {
textBox1.Text = textBox1.Text + "9";
}
}
private void n0_Click(object sender, EventArgs e) {
textBox1.Text = textBox1.Text + "0";
}
private void badd_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "+";
}
private void bsub_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "-";
}
private void bmul_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "*";
}
private void bdiv_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0";
Operation = "/";
}

16
private void bclear_Click(object sender, EventArgs e) {
textBox1.Text = "0";
}
private void bdot_Click(object sender, EventArgs e) {
textBox1.Text = textBox1.Text + ".";
}
private void bequal_Click(object sender, EventArgs e) {
double SecondNumber;
double Result;
SecondNumber = Convert.ToDouble(textBox1.Text);
if (Operation == "+") {
Result = (FirstNumber + SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "-") {
Result = (FirstNumber - SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "*") {
Result = (FirstNumber * SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
if (Operation == "/") {
if (SecondNumber == 0) {
textBox1.Text = "Cannot divide by zero";
} else {
Result = (FirstNumber / SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;

17
}
}
}
}
}
OUTPUT :

18
B ) Create a window form application to make a calculator using
Keyboard.

Program :
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String num1;
String num2;
String result;

private void label1_Click(object sender, EventArgs e)


{

19
private void textBox1_TextChanged(object sender, EventArgs e)
{
num1= (String)textBox1.Text;
}

private void textBox2_TextChanged(object sender, EventArgs e)


{
num2=(String)textBox2.Text;
}

private void button1_Click(object sender, EventArgs e)


{
int res = Convert.ToInt32(num1)+ Convert.ToInt32(num2);
label3.Text = Convert.ToString(res);
}

private void button2_Click(object sender, EventArgs e)


{
int res = Convert.ToInt32(num1) - Convert.ToInt32(num2);
label3.Text = Convert.ToString(res);
}

private void button3_Click(object sender, EventArgs e)


{
int res = Convert.ToInt32(num1) * Convert.ToInt32(num2);
label3.Text = Convert.ToString(res);
}

private void button4_Click(object sender, EventArgs e)


{
double res = Convert.ToDouble(num1) / Convert.ToDouble(num2);
label3.Text = Convert.ToString(res);

20
}
}
}
OUTPUT :

21
PROGRAM 7 :- Create a window form application to create a Login page
with all buttons functioning (Sign in, Login, Cancel).

LOGIN PAGE:-
Program :
namespace login {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e) {}
private void Form1_Load(object sender, EventArgs e) {}
private void label3_Click(object sender, EventArgs e) {}
private void label2_Click(object sender, EventArgs e) {}
private void textBox2_TextChanged(object sender, EventArgs e) {}
private void label1_Click(object sender, EventArgs e) {}
private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == ”Admin” && textBox2.Text == ”Pass”) {
this.Hide();
Form2 ss = new Form2();
ss.Show();
} else {
MessageBox.Show(“Faild”);
}
}
private void button2_Click(object sender, EventArgs e) {
this.Hide();
Form4 s = new Form4();
s.Show();
}
private void textBox2_TextChanged_1(object sender, EventArgs e) {}
private void textBox1_TextChanged_1(object sender, EventArgs e) {}
22
}
}
SIGNUP PAGE :
Program :
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;
using System.Data.SqlClient;
using System.Configuration;
namespace login {
public partial class Form4: Form {
public Form4() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
String mainconn =
ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "insert into [dbo].[login] values
(@Uname, @Upwd, @Urepwd, @Email, @Gender @Location)
";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlcomm, sqlconn);
sqlcomm.Parameters.AddWithValue("@Uname", textBox1.Text);
sqlcomm.Parameters.AddWithValue("@Upwd", textBox2.Text);
sqlcomm.Parameters.AddWithValue("@Urepwd", textBox3.Text);

23
sqlcomm.Parameters.AddWithValue("@UEmail", textBox4.Text);
sqlcomm.Parameters.AddWithValue("@UEmail", textBox5.Text);
sqlcomm.Parameters.AddWithValue("@Location", textBox6.Text);
sqlcomm.ExecuteNonQuery();
labMsg.Text = "User" + textBox1.Text + " Is Successfully Registered";
lnkLogin.VIsible = "true";
sqlconn.Close();
}
private void textBox3_leave(object sender, EventArgs e) {
if (textBox2.Text != textBox3.Text) {
MessageBox.Show("Password not same");
textBox3.Focus();
return;
}
}
}
}
OUTPUT :

Login Page :

24
Registration page

25
PROGRAM 8 :- Write a program to create a dll file and use it.
-Creating DLL
Program :
namespace ClassLibrary1 {
Public class Class1 {
Public int Add(int a, int b) {
return a + b;
}
Public int Sub(int a, int b) {
return a - b;
}
}
}
-Using dll file
Program :
using System;
using ClassLibrary1;
namespace ConsoleApp3 {
internal class Program
{
static void Main(string[] args) {
Class1 obj = new Class1();
Console.WriteLine("Class Library function Add=" + obj.Add(l0,20));
Console.WriteLine("Class Library function Sub=” + obj.Sub(30,25));
Console.Read();
}
}
}
OUTPUT :
Class Library function Add=30
Class Library function Sub=5

26
PROGRAM 9 :- Program to create a web page using asp.net.
Program :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Welcome to the Web Forms!</h2>
</div>
</form>
</body>
</html>
OUTPUT :

27
PROGRAM 10 :- Create a window from an application usingASP.Net.
By using a database(to retrieve and modify the data).

Establish connection and create a table


Program :
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication {
class Program {
static void Main(string[] args) {
new Program().CreateTable();
}
public void CreateTable() {
SqlConnection con = null;
try {
// Creating Connection
con = new SqlConnection("data source=.; database=student;
integrated security = SSPI ");
// writing sql query
SqlCommand cm = new SqlCommand("create table student(id
int not null, name varchar(100), email varchar(50), join_date date)
", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Table created Successfully");
}
catch (Exception e) {
Console.WriteLine("OOPs, something went wrong." + e);

28
}
// Closing the connection
finally {
con.Close();
}
}
}
}
OUTPUT :

Insert Data into the Table:-


Program :
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication {
class Program {
static void Main(string[] args) {
new Program().CreateTable();
}
public void CreateTable() {
SqlConnection con = null;
try
{
// Creating Connection
29
con = new SqlConnection("data source=.; database=student; integrated
security = SSPI ");
// writing sql query
SqlCommand cm = new SqlCommand("insert into student (id, name,
email, join_date) values('101', 'Ronald Trump', '[email protected]', '1/12/201
7 ')", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Record Inserted Successfully");
}
catch (Exception e) {
Console.WriteLine("OOPs, something went wrong." + e);
}
// Closing the connection
finally {
con.Close();
}
}
}
OUTPUT :

30
Retrieve Record :-
Program :
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication {
class Program {
static void Main(string[] args) {
new Program().CreateTable();
}
public void CreateTable() {
SqlConnection con = null;
try {
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated
security = SSPI ");
// writing sql query
SqlCommand cm = new SqlCommand("Select * from student", con);
// Opening Connection
con.Open();
// Executing the SQL query
SqlDataReader sdr = cm.ExecuteReader();
// Iterating Data
while (sdr.Read()) {
Console.WriteLine(sdr["id"] + " " + sdr["name"] + " " + sdr["email"]); //
Displaying Record
}
}
catch (Exception e) {
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
// Closing the connection
finally {

31
con.Close();
}
}
}
}
OUTPUT :

Record Delete:-

Program :
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication {
class Program {
static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable() {
SqlConnection con = null;
try {
// Creating Connection
con = new SqlConnection("data source=.; database=student; integrated
security = SSPI ");
32
// writing sql query
SqlCommand cm = new SqlCommand("delete from student where id =
'101'
", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery(); Console.WriteLine("Record Deleted Successfully");
}
catch (Exception e) {
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
// Closing the connection
finally {
con.Close();
}
}
}
OUTPUT :

33

You might also like