1) Write A Program in C# To Demonstrate Command Line Arguments Processing. Command Line Arguments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 57

.

Net Technologies Laboratory (PCA552L) 2BA18MCA01

1) Write a Program in c# to demonstrate Command line arguments


processing.

Command Line Arguments


The arguments which are passed by the user or programmer to the Main() method is
termed as Command-Line Arguments. Main() method is the entry point of execution of a
program. Main() method accepts array of strings. But it never accepts parameters from
any other method in the program. In C# the command line arguments are passed to the
Main() method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace commandline
{
class Program
{
static void Main(string[] args)

{
int SUM = 0;
int X = 0;
int Y = 0;

X = Convert.ToInt32(args[0]);
Y = Convert.ToInt32(args[1]);

SUM = X + Y;

Console.WriteLine("Sum is: " + SUM);


Console.ReadLine();

}
}
}

Dept. of MCA, BECBGK 2020-21 Page 1


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 2


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

2. Write a C# program to demonstrate the following concepts:

A. Value type and Reference type.

B. Boxing and Unboxing.


Value Type
Value type variables can be assigned a value directly. They are derived from the class
System. value Type. The value types directly contain data. When you declare an int type,
the system allocates memory to store the value.

Value Type variables are stored in the stack.


Examples are int, char, and float, which stores numbers, alphabets, and floating point
numbers, respectively.

Reference Type
It refers to a memory location. Using multiple variables, the reference types can refer to a
memory location. If the data in the memory location is changed by one of the variables,
the other variable automatically reflects this change in value.

Reference Type variables are stored in the heap.


Example of built-in reference types are −

 object
 dynamic
 string

Boxing
Boxing is used to store value types in the garbage-collected heap. Boxing is an
implicit conversion of a value type to the type object or to any interface type
implemented by this value type. Boxing a value type allocates an object instance on the
heap and copies the value into the new object.

Dept. of MCA, BECBGK 2020-21 Page 3


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Unboxing

Unboxing is an explicit conversion from the type object to a value type or from an
interface type to a value type that implements the interface. An unboxing operation
consists of:

 Checking the object instance to make sure that it is a boxed value of the given value type.

 Copying the value from the instance into the value-type variable.

2. write a program in c# to demonstrate the following concepts:


a) Value type and reference type.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace boxing
{
class Program
{
static void Main(string[] args)
{

int num = 2020;


object obj = num;
num = 100;

Console.WriteLine("Value - type value of num is : {0}", num);


Console.WriteLine("Object - type value of obj is : {0}", obj);
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 4


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

B) Boxing and Unboxing.


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

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

int num = 23;


object obj = num;
int i = (int)obj;
Console.WriteLine("Value of ob object is : " + obj);
Console.WriteLine("Value of i is : " + i);
Console.ReadLine();

}
}
}

Dept. of MCA, BECBGK 2020-21 Page 5


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 6


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

3) Write a C# program to demonstrate the constructor concepts using


this keyword.

 When you use this keyword to call a constructor, the constructor should
belong to the same class.
 You can also pass parameter in this keyword.
 This keyword always pointing to the members of the same class in which it is
used.
 When you use this keyword, it tells the compiler to invoke the default
constructor. Or in other words, it means a constructor that does not contain arguments.
 This keyword contains the same type and the same number of parameters that
are present in the calling constructor.
 This concept removes the assignment of replication of properties in the same class.

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

namespace thiskeyword
{
class Program
{
static void Main(string[] args)
{
Amount amt = new Amount();
amt.Balance();
amt.GetData();
Console.WriteLine("\n Press Enter to Quit....");
Console.ReadLine();

} // end of Main ();


}
}
public class Amount{

double totalamount,deduction,Balanceamount;

Dept. of MCA, BECBGK 2020-21 Page 7


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

public void Balance(){


this.totalamount=120000;
this.deduction=2000;
Balanceamount=totalamount-deduction;
Console.WriteLine(" Your Current Balanceamount={0}",Balanceamount);
}//End of Amount()

public void Debit(){


int Debitamount=0;
Console.WriteLine("Enter Amount:");
Debitamount=int.Parse(Console.ReadLine());
Balanceamount=Balanceamount-Debitamount;
Console.WriteLine(" Your Current Balanceamount={0}",Balanceamount);
} // End of Balance ()

public void Credit(){


int Creditamount=0;
Console.WriteLine("Enter Amount:");
Creditamount=int.Parse(Console.ReadLine());
Balanceamount=Balanceamount+Creditamount;
Console.WriteLine(" Your Current Balanceamount={0}",Balanceamount);
} // End of Credit ()

public void GetData(){


char Data;

Console.WriteLine("-------------------------");
Console.WriteLine("Press (d) for the Debit and (c) for Credit:");
Data=char.Parse(Console.ReadLine());
if(Data=='d'){
Debit();
}
if(Data=='c'){
Credit();
}
} // end of GetData()

} // End of Amount class

Dept. of MCA, BECBGK 2020-21 Page 8


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 9


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

4) Write a C# program to demonstrate the method modifiers.


Private Modifier

If you declare a field with a private access modifier, it can only be accessed within the
same class:

Public Modifier

If you declare a field with a public access modifier, it is accessible for all classes:

//private modifiers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab4
{
class student
{
private string Name="Abhishek";
}
class Program
{
static void Main(string[]args)
{
student myObj = new student();
Console.WriteLine(myObj.Name);

Dept. of MCA, BECBGK 2020-21 Page 10


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Console.ReadLine();
}} }

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 11


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

//PublicModifier.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab4
{
class student
{
public string Name="Abhishek";
}
class Program
{
static void Main(string[]args)
{
student myObj = new student();
Console.WriteLine(myObj.Name);
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 12


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 13


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

5. Write a C# program to demonstrate use of static methods and


variables.

Static methods and variables


A static class is basically the same as a non-static class, but there is one
difference: a static class cannot be instantiated. In other words, you cannot use the new
operator to create a variable of the class type. Because there is no instance variable, you
access the members of a static class by using the class name itself. For example, if you
have a static class that is named Utility Class that has a public static method named
Method.

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

namespace staticmethod
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please Select the convertor");
Console.WriteLine("1. Celsius To Fahrenheit");
Console.WriteLine("2. Fahrenheit To Celsius");
Console.Write("\n Press either 1 or 2 : ");
string choice;
choice = Console.ReadLine();
double F, C = 0;

switch (choice)
{
case "1":
Console.WriteLine(" Enter Temparature in Celsius");
F = temparature_converter.CelsiusToFahrenheit(Console.ReadLine());
Console.WriteLine("Temparature in Fahrenheit: {0}", F);
break;

Dept. of MCA, BECBGK 2020-21 Page 14


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

case "2":
Console.WriteLine(" Enter Temparature in Fahrenheit");
C = temparature_converter.FahrenheitToCelsiu(Console.ReadLine());
Console.WriteLine("Temparature in Celsiu: {0}", C);
break;

default: Console.WriteLine("Invalid choice"); break;

}// end of switch


Console.ReadLine();
} // end of main method

}// end of main class

static class temparature_converter


{

public static double CelsiusToFahrenheit(string temparature)


{
double C = double.Parse(temparature);
return ((C * 9 / 5) + 32);
}
public static double FahrenheitToCelsiu(string temparature)
{
double F = double.Parse(temparature);
return ((F - 32) * 5 / 9);

}// end of static_class


}

Dept. of MCA, BECBGK 2020-21 Page 15


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 16


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

6) Write a program to implement C# normal arrays and jagged arrays.

A jagged array is an array whose elements are arrays. The elements of a jagged array
can be of different dimensions and sizes. A jagged array is sometimes called an "array of
arrays." The following examples show how to declare, initialize, and access jagged
arrays.

The following is a declaration of a single-dimensional array that has three elements, each
of which is a single-dimensional array of integers:

 int[][] jaggedArray = new int[3][];

You can initialize the elements like this:


 jaggedArray[0] = new int[5];
 jaggedArray[1] = new int[4];
 jaggedArray[2] = new int[2];

A jagged array is an array of arrays, and therefore its elements are reference types and are
initialized to null.

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

namespace jaggedArray
{
class Program
{
static void Main(string[] args)
{
// Declare the array of four elements:
int[][] jaggedArray = new int[4][];
// Initialize the elements:
jaggedArray[0] = new int[2] { 7, 9 };
jaggedArray[1] = new int[4] { 12, 42, 26, 38 };
jaggedArray[2] = new int[6] { 3, 5, 7, 9, 11, 13 };
Dept. of MCA, BECBGK 2020-21 Page 17
.Net Technologies Laboratory (PCA552L) 2BA18MCA01

jaggedArray[3] = new int[3] { 4, 6, 8 };


// Display the array elements:
for (int i = 0; i < jaggedArray.Length; i++)
{
Console.WriteLine("Element({0}): ", i + 1);
for (int j = 0; j < jaggedArray[i].Length; j++)
{
System.Console.Write(jaggedArray[i][j] + "\t");
}
System.Console.WriteLine();
}
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 18


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 19


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

7) a)Write a program to implement the exception handling keywords in c#.

An exception is a problem that arises during the execution of a program. A C# exception is a


response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C#
exception handling is built upon four keywords: try, catch, finally, and throw.

 try − A try block identifies a block of code for which particular exceptions is activated.
It is followed by one or more catch blocks.

 catch − A program catches an exception with an exception handler at the place in a


program where you want to handle the problem. The catch keyword indicates the
catching of an exception.

 finally − The finally block is used to execute a given set of statements, whether an
exception is thrown or not thrown. For example, if you open a file, it must be closed
whether an exception is raised or not.
 throw − A program throws an exception when a problem shows up. This is done using a
throw keyword.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace exceptionhandling
{
class Program
{
static void Main()
{

Dept. of MCA, BECBGK 2020-21 Page 20


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

int number = 0;
int div = 0;
Console.WriteLine("Enter the number");
number = int.Parse(Console.ReadLine());
try
{
div = 150 / number;
Console.WriteLine(" You are in try block");
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Exception occurs: " + ex.Message);
}
finally
{
Console.WriteLine(" You are in finally block");
Console.WriteLine(" Div=" + div);
}
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 21


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 22


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

B) C# program to handle excption using throw statement.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace exceptionhandling
{
class Program
{
static void Main()
{
int number = 0;
int div = 0;
Console.WriteLine("Enter the number");
number = int.Parse(Console.ReadLine());
try
{
div = 150 / number;
throw new DivideByZeroException("you have entered the zero value");
Console.WriteLine(" You are in try block");
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Exception occurs: " + ex.Message);
}
finally

Dept. of MCA, BECBGK 2020-21 Page 23


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

{
Console.WriteLine(" You are in finally block");
Console.WriteLine(" Div=" + div);
}
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 24


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 25


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

8) Write a Program to Demonstrate Use of Virtual and override key words in


C# with a simple program.

Virtual and Override in C#

 Virtual keyword is used for generating a virtual path for its derived classes on implementing
method overriding.
o Virtual keyword is used within a set with override keyword. 
 Override keyword is used in the derived class of the base class in order to override the
base class method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Virtualandoverride
{
public class Program
{
public virtual void getdata()
{
Console.WriteLine("Enter the name of the student:");
string name = Console.ReadLine();
Console.WriteLine("Enter the USN:");
string usn = Console.ReadLine();
}
}
class student:Program

Dept. of MCA, BECBGK 2020-21 Page 26


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

{
public override void getdata()
{
base.getdata();
Console .WriteLine ("Enter the marks for differnt Subjects");
Console .WriteLine ("C#:");
int csharp=int.Parse (Console .ReadLine());
Console .WriteLine ("\n CPP:");
int cpp=int.Parse (Console .ReadLine ());
Console .WriteLine ("\n C:");
int c=int.Parse (Console .ReadLine ());
Console .WriteLine ("\n Total={0}",csharp +cpp +c );
}
static void Main()
{
// LABPGM6 obj = new LABPGM6 ();
//obj.getdata();
student obj1 = new student();
obj1.getdata();
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 27


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 28


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

9) Write a Program in C# to create and implement a delegate for any two


arithmetic operations.

A delegate is a type-safe object that can point to another method (or possibly multiple
methods) in the application, which can be invoked at later time.

Defining a Delegate.

When you want to create a delegate in C# you make use of delegate keyword.The name of your
delegate can be whatever you desire. However, you must define the delegate to match the
signature of the method it will point to.

Instantiating Delegates

Once a delegate type is declared, a delegate object must be created with


the new keyword and be associated with a particular method. When creating a delegate, the
argument passed to the new expression is written similar to a method call, but without the
arguments to the method.

Multicasting of a Delegate
Delegate objects can be composed using the "+" operator. A composed delegate calls the
two delegates it was composed from. Only delegates of the same type can be composed. The "-"
operator can be used to remove a component delegate from a composed delegate.

Using this property of delegates you can create an invocation list of methods that will be
called when a delegate is invoked. This is called multicasting of a delegate. 

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

namespace Delegate

Dept. of MCA, BECBGK 2020-21 Page 29


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

{
// Delegate Definition
public delegate int operation(int x, int y);

class Program
{
// Method that is passes as an Argument
// It has same signature as Delegates
static int Add(int a, int b)
{
return a + b;
}
static int sub(int a, int b)
{
return a - b;
}
static int mul(int a, int b)
{
return a * b;
}
static int div(int a, int b)
{
return a / b;
}
static void Main(string[] args)
{
// Delegate instantiation
operation obj = new operation(Program.Add);
operation obj1 = new operation(Program.mul);
operation obj2 = new operation(Program.sub);
operation obj3 = new operation(Program.div);

// output
Console.WriteLine("Addition is={0}",obj(23,27));
Console.WriteLine("Subtraction is={0}", obj2(10, 30));
Console.WriteLine("multiplication is={0}", obj1(10, 30));
Console.WriteLine("Division is={0}", obj3(10,2));
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 30


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 31


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

10) Write a Program in C# to demonstrate abstract class and abstract


methods in C#.

Abstract class

If a class is defined as abstract then we can't create an instance of that class. By the
creation of the derived class object where an abstract class is inherit from, we can call the
method of the abstract class.

Abstract method

An Abstract method is a method without a body. The implementation of an abstract


method is done by a derived class. When the derived class inherits the abstract method from the
abstract class, it must override the abstract method. This requirment is enforced at compile time
and is also called dynamic polymorphism.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abstractclass
{
abstract class vehicle
{
protected float price;
protected string model;
public abstract double CalculateEMI(int months, double rate);
}
class car : vehicle

Dept. of MCA, BECBGK 2020-21 Page 32


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

{
public car(int price, string model)
{
this.price = price;
this.model = model;
}
public override double CalculateEMI(int months, double rate)
{
double total = price + price * rate / 100;
double totalEMI = total / months;
return totalEMI;
}
public void display()
{
Console.WriteLine("Car Model={0}", model);
Console.WriteLine("Car Price={0}", price);
}
}
class Pragram
{
public static void Main()
{
car c = new car(500000, "Indica Vz");
c.display();
Console.WriteLine("Enter the no.of months and interest rate");
int mnt = int.Parse(Console.ReadLine());
float rate = float.Parse(Console.ReadLine());

Dept. of MCA, BECBGK 2020-21 Page 33


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Console.WriteLine("The EMI for Car " + mnt + "Months:" + c.CalculateEMI(mnt, rate));


Console.ReadKey(); } } }

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 34


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

11) Write a program to Set & Get the Name & Age of a person using
Properties of C# to illustrate the use of different properties in C#.

Property in C# is a member of a class that provides a flexible mechanism for classes to


expose private fields. Internally, C# properties are special methods called accessors. A C#
property have two accessors, get property accessor and set property accessor. A get accessor
returns a property value, and a set accessor assigns a new value. The value keyword represents
the value of a property.

Properties in C# and .NET have various access levels that is defined by an access
modifier. Properties can be read-write, read-only, or write-only. The read-write property
implements both, a get and a set accessor. A write-only property implements a set accessor, but
no get accessor. A read-only property implements a get accessor, but no set accessor.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace getandset
{
class Student
{
private string myName = "Raju";
private int myAge = 0;
public string Name
{
get
{

Dept. of MCA, BECBGK 2020-21 Page 35


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

return myName;
}
set
{
myName = value;
}
}
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}

public override string ToString()


{
return "Name = " + Name + ", Age = " + Age;
}

public static void Main()


{
Student Student = new Student();

Dept. of MCA, BECBGK 2020-21 Page 36


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Console.WriteLine("Student details - {0}", Student);


Student.Name = "Abhishek";
Student.Age = 99;
Console.WriteLine("Student details - {0}", Student);
Student.Age += 1;
Console.WriteLine("Student details - {0}", Student);
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 37


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 38


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

12) Write Program in C# Demonstrate arrays of interface types (for runtime


polymorphism).

Interface in C# is basically a contract in which we declare only signature. The class
which implemented this interface will define these signatures. Interface is also a way to
achieve runtime polymorphism. We can add method, event, properties and indexers in interface.

Syntax of Interface

 public interface interface_name { }

Runtime Polymorphism

 We can implement more than one interface in one class. This is the way to achieve the
multiple inheritances. Create two interfaces and create same method in both and implement in
same class.

As you can see that we implemented two interfaces into one class. We give one definition
to the method. We will not get any error as in interface we only declared the method. That is
only signature. So we can give one common definition to all same method having same
signature. When we create the object of implementation class we can easily call sum method
without any error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace polymorphism
{
public interface shape
{

Dept. of MCA, BECBGK 2020-21 Page 39


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

void area();
}
public class Circle : shape
{
public void area()
{
Console.WriteLine("Caluclatig area of Circle");
Console.WriteLine("-------------------------");
Console.WriteLine("Enter the radius of Circle:");
double radius = double.Parse(Console.ReadLine());
double area = 3.14 * radius * radius;
Console.WriteLine("Area of Circle is:{0}", area);
}
}
public class Square : shape
{
public void area()
{
Console.WriteLine("\n\nCaluclatig area of Square");
Console.WriteLine("-----------------------------");
Console.WriteLine("Enter the side:");
float side = float.Parse(Console.ReadLine());
Console.WriteLine("Area of Square is:{0}", side * side);
}
}
class program
{

Dept. of MCA, BECBGK 2020-21 Page 40


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

public static void Main()


{
shape[] findarea = new shape[2];
findarea[0] = new Circle();
findarea[1] = new Square();
for (int i = 0; i < findarea.Length; i++)
{
findarea[i].area();
}
Console.ReadLine();
}
}
}

Dept. of MCA, BECBGK 2020-21 Page 41


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 42


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

13. Write a C# program to implement CRUD operations using any popular


database.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;
namespace Student
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-
01APTPN\SQLEXPRESS;Initial Catalog=Student;Integrated Security=true");
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void uname_TextChanged(object sender, EventArgs e)

Dept. of MCA, BECBGK 2020-21 Page 43


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

{
}
private void submit_Click(object sender, EventArgs e)
{
String username;
username = uname.Text;
String pass;
pass = password.Text;
String query = "insert into student_info values ('" + username + "','" + pass + "')";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Records Sucessfully saved in the Database");
}
private void update_Click(object sender, EventArgs e)
{
String username;
username = uname.Text;

String pass;
pass = password.Text;
String query = "UPDATE student_info SET password='"+pass+"' WHERE
User_name='"+username+"'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();

Dept. of MCA, BECBGK 2020-21 Page 44


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

MessageBox.Show(" Value Updated");


}
private void delete_Click(object sender, EventArgs e)
{
String username;
username = uname.Text;
String query = "DELETE FROM student_info WHERE User_name='"+username+"'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(" Value Deleted");
}
private void clear_Click(object sender, EventArgs e)
{
MessageBox.Show("All Values Remove");
}
private void button1_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-
01APTPN\SQLEXPRESS;Initial Catalog=Student;Integrated Security=true");
con.Open();
string sql = "Select *from student_info";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteNonQuery();

Dept. of MCA, BECBGK 2020-21 Page 45


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

dataGridView1.DataSource = ds.Tables[0];
con.Close();} } }

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 46


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Dept. of MCA, BECBGK 2020-21 Page 47


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Dept. of MCA, BECBGK 2020-21 Page 48


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

14) Design and implement ASP.net page to demonstrate basic validation


facilities.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs"


Inherits="validation.Demo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="2"
<tr><td colspan="3" align="center"> Student Registration </td></tr>
<tr>
<td><asp:Label ID="user_name" Text="Student Name" runat="server"></asp:Label></td>
<td><asp:TextBox ID="uname" runat="server" placeholder="*" ></asp:TextBox></td><td>
<asp:RequiredFieldValidator ID="rdvalid1" runat="server" ControlToValidate="uname"
ErrorMessage="Enter your name" ForeColor="Red">
</asp:RequiredFieldValidator>
<br /></td></tr><tr>
<td><asp:Label ID="Label1" Text="Student USN" runat="server"></asp:Label></td>
<td><asp:TextBox ID="TextBox11" runat="server" placeholder="*"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="uname" ErrorMessage="Enter your USN" ForeColor="Red">
</asp:RequiredFieldValidator><br /></td></tr><tr>

Dept. of MCA, BECBGK 2020-21 Page 49


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

<td class="style3">Gender: </td>


<td class="style2">
<asp:RadioButtonList ID="rblhouse" runat="server" RepeatLayout="Flow">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td><td>
<asp:RequiredFieldValidator ID="rfvhouse" runat="server"
ControlToValidate="rblhouse" ErrorMessage="Select Genedr"ForeColor="Red" >
</asp:RequiredFieldValidator>
<br /> </td></tr><tr>
<td><asp:Label ID="semlb" Text="Semester" runat="server"></asp:Label></td>
<td><asp:TextBox ID="sem" runat="server" placeholder="*"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="sem" ErrorMessage="Enter your semester"ForeColor="Red">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="semrng" runat="server" MinimumValue="1" MaximumValue="6"
Type="Integer" ControlToValidate="sem" ErrorMessage="Enter your sem(1-
6)"></asp:RangeValidator></td></tr> <tr>
<td><asp:Label ID="Label2" Text="Contact Number" runat="server"></asp:Label></td>
<td> <asp:TextBox ID="TextBox12" runat="server" placeholder="*"></asp:TextBox> </td>
<td><asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox12" ErrorMessage="Enter valid Contact Number"
ForeColor="Red"ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator> </td>
</tr><tr><td class="style3">Email:</td>
<td class="style2">
<asp:TextBox ID="stdemail" runat="server" style="width:250px" placeholder="*">
</asp:TextBox>
</td><td>

Dept. of MCA, BECBGK 2020-21 Page 50


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"


ControlToValidate="stdemail" ErrorMessage="Enter your email"ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)@\w+([-.]\w+)\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td> </tr><tr><td class="style3" align="center" colspan="3"></td>
</tr><tr><td class="style3" align="center" colspan="3">
<asp:Button ID="btnsubmit" runat="server" Text="Submit" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Dept. of MCA, BECBGK 2020-21 Page 51


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

OUTPUT:-

Dept. of MCA, BECBGK 2020-21 Page 52


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Dept. of MCA, BECBGK 2020-21 Page 53


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

15 Design and implement web application to validate user login.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Drawing;

public partial class Login : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{

}
SqlConnection con = new SqlConnection("Data Source=DESKTOP-
01APTPN\\SQLEXPRESS;Initial Catalog=Logindatabase;Integrated Security=True");

protected void log_Click(object sender, EventArgs e)


{

int count;
DataTable datatable;
DataSet ds = new DataSet();

if (uname.Text == "")
{
Response.Write("Enter Username");
}

con.Open();
String Sql = "select * from login where (User_Name='" + uname.Text + "' and Password='"
+ pass.Text + "')";
SqlCommand cmd = new SqlCommand(Sql, con);
count = cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
datatable = ds.Tables[0];
count = datatable.Rows.Count;

if (count > 0)

Dept. of MCA, BECBGK 2020-21 Page 54


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Response.Write("Login Successful");

}
else
{
Response.Write("Login UnSuccessful");
}
con.Close();
}

OUTPUT:-
Dept. of MCA, BECBGK 2020-21 Page 55
.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Dept. of MCA, BECBGK 2020-21 Page 56


.Net Technologies Laboratory (PCA552L) 2BA18MCA01

Dept. of MCA, BECBGK 2020-21 Page 57

You might also like