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

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

1) The document discusses command line arguments in C# and provides a sample program to demonstrate how to process command line arguments passed to the Main method. 2) It explains value types and reference types in C#, and provides sample programs to demonstrate call by value vs call by reference. It also discusses boxing and unboxing. 3) The document discusses using the 'this' keyword to call overloaded constructors in C# and provides a sample program. 4) It discusses private, public, and other method modifiers in C# and provides a sample program to demonstrate their usage.

Uploaded by

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

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

1) The document discusses command line arguments in C# and provides a sample program to demonstrate how to process command line arguments passed to the Main method. 2) It explains value types and reference types in C#, and provides sample programs to demonstrate call by value vs call by reference. It also discusses boxing and unboxing. 3) The document discusses using the 'this' keyword to call overloaded constructors in C# and provides a sample program. 4) It discusses private, public, and other method modifiers in C# and provides a sample program to demonstrate their usage.

Uploaded by

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

.

NET Technology (PCA552L) 2BA18MCA05

Assignment 1

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.

/* Write a Program in c# to demonstrate Command line arguments processing. */

/**********************************************************************************/

using System;

namespace CSharpProgram1

class Program

// Main function, execution entry point of the program

static void Main (string[]args) // string type parameters

// Command line arguments

Console.WriteLine ("Argument length: " + args.Length);

Console.WriteLine ("Supplied Arguments are:");

foreach (Object obj in args)

Console.WriteLine (obj);

Dept of MCA, BEC BGK 2020-21 Page 1


.NET Technology (PCA552L) 2BA18MCA05

Output:

Dept of MCA, BEC BGK 2020-21 Page 2


.NET Technology (PCA552L) 2BA18MCA05

Assignment 2

Value Type and Reference, both are types in C# −


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

Call by Value means calling a method with a parameter as value. Through this, the argument
value is passed to the parameter.
While Call by Reference means calling a method with a parameter as a reference. Through this,
the argument reference is passed to the parameter.
In call by value, the modification done to the parameter passed does not reflect in the caller's
scope while in the call by reference, the modification done to the parameter passed are persistent
and changes are reflected in the caller's scope.

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, BEC BGK 2020-21 Page 3


.NET Technology (PCA552L) 2BA18MCA05

Un boxing
Un boxing 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.

/* Write a program in c# to demonstrate the following concepts: */

/********* *******************************************************************/

a) Value type and reference type.

using System;

namespace CallByValue {

class Program

public void Show(int val) // User defined function

val *= val; // Manipulating value

Console.WriteLine("Value inside the show function "+val);

static void Main(string[] args) {

int val = 20;

Program program = new Program(); // Creating Object

Console.WriteLine("Value before calling the function "+val);

program.Show(val); // Calling Function by passing value

Console.WriteLine("Value after calling the function " + val);

Dept of MCA, BEC BGK 2020-21 Page 4


.NET Technology (PCA552L) 2BA18MCA05

Output:

using System;

namespace CallByReference {

class Program

public void Show(ref int val) // User defined function

val *= val; // Manipulating value

Console.WriteLine("Value inside the show function "+val);

// Main function, execution entry point of the program

static void Main(string[] args) {

int val = 5;

Program program = new Program(); // Creating Object

Console.WriteLine("Value before calling the function "+val);

program.Show(ref val); // Calling Function by passing reference

Console.WriteLine("Value after calling the function " + val);

Dept of MCA, BEC BGK 2020-21 Page 5


.NET Technology (PCA552L) 2BA18MCA05

Output:

b) Boxing and Unboxing.

class Test_Boxing

static void Main()

int i = 56 // Boxing copies the value of i into object obj.

object obj = i;

i = 25; // Change the value of i.

// The change in i doesn't affect the value stored in obj.

System.Console.WriteLine("The value-type value = {0}", i);

System.Console.WriteLine("The object-type value = {0}", obj);

Output:

Dept of MCA, BEC BGK 2020-21 Page 6


.NET Technology (PCA552L) 2BA18MCA05

class Test_Unboxing

static void Main()

int i = 15;

object o = i; // implicit boxing

try {

int j = (int)o; // attempt to unbox

System.Console.WriteLine("Unboxing OK.");

catch (System.InvalidCastException e)

System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);

Output:

Dept of MCA, BEC BGK 2020-21 Page 7


.NET Technology (PCA552L) 2BA18MCA05

Assignment 3

Invoking an overloaded constructor using this keyword in C#


C# provides a powerful keyword known as this keyword and this keyword has many usages. Here
we use this keyword to call an overloaded constructor from another constructor.
Important Points:
 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.
Below programs illustrate how to call the overloaded constructor using this keyword.

/* Write a C# program to demonstrate the constructor concepts using this keyword. */


/*************************************************************************/

using System;

public class Employee

public int id;

public String name;

public float salary;

public Employee()

Console.WriteLine("This is default constructor...!");

public Employee(int id, String name, float salary)

this.id = id;

this.name = name;

Dept of MCA, BEC BGK 2020-21 Page 8


.NET Technology (PCA552L) 2BA18MCA05

this.salary = salary;

public void display()

Console.WriteLine(id + " " + name+" "+salary);

class TestEmployee{

public static void Main(string[] args)

Employee e1 = new Employee( );

Employee e2 = new Employee(12, "Kumar", 50000f);

e2.display();

Output:

Dept of MCA, BEC BGK 2020-21 Page 9


.NET Technology (PCA552L) 2BA18MCA05

Assignment 4

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

/* Write a C# program to demonstrate the method modifiers.*/


/****************************************************************/
using System;

namespace accessspecifier

class Program

static void Main(string[] args)

two B = new two();

B.show();

class one

private int x;

protected int y;

internal int z;

public int a;

Dept of MCA, BEC BGK 2020-21 Page 10


.NET Technology (PCA552L) 2BA18MCA05

protected internal int b;

class two : one

public void show()

Console.WriteLine("Values are : ");

// x = 10;

y = 20;

z = 30;

a = 40;

b = 50;

// Console.WriteLine(+x); // Error x is not accessible

Console.WriteLine(y);

Console.WriteLine(z);

Console.WriteLine(a);

Console.WriteLine(b);

Console.ReadLine();

Dept of MCA, BEC BGK 2020-21 Page 11


.NET Technology (PCA552L) 2BA18MCA05

Output:

Dept of MCA, BEC BGK 2020-21 Page 12


.NET Technology (PCA552L) 2BA18MCA05

Assignment 5

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 A, you call the

The following list provides the main features of a static class:

 Contains only static members.


 Cannot be instantiated.
 Is sealed.
 Cannot contain Instance Constructors.

Creating a static class is therefore basically the same as creating a class that contains only
static members and a private constructor. A private constructor prevents the class from being
instantiated. The advantage of using a static class is that the compiler can check to make sure that
no instance members are accidentally added. The compiler will guarantee that instances of this
class cannot be created.

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class
except Object. Static classes cannot contain an instance constructor. However, they can contain a
static constructor. Non-static classes should also define a static constructor if the class contains
static members that require non-trivial initialization. For more information, see Static
Constructors.

/* Write a C# program to demonstrate use of static methods and variables. */

/*************************************************************************/

using System;

namespace Static_var_and_fun
Dept of MCA, BEC BGK 2020-21 Page 13
.NET Technology (PCA552L) 2BA18MCA05

class number

public static int num; // Create static variable

public static void power() //Create static method

Console.WriteLine("Power of {0} = {1}", num, num * num);

Console.ReadLine();

class Program

static void Main(string[] args)

Console.WriteLine("Enter a number:");

number.num = Convert.ToInt32(Console.ReadLine());

number.power();

Output:

Dept of MCA, BEC BGK 2020-21 Page 14


.NET Technology (PCA552L) 2BA18MCA05

Assignment 6

Jagged Array

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.

/* Write a program to implement C# normal arrays and jagged arrays. */


/*************************************************************************/

using System;
class Jagged_ArrayTest
{
static void Main()
{

int[][] arr = new int[2][]; // Declare the array of two elements.


arr[0] = new int[6] { 1, 3, 5, 9, 7, 9 }; // Initialize the elements.
arr[1] = new int[4] { 2, 4, 6, 8 };
for (int i = 0; i < arr.Length; i++) // Display the array elements.
{

Dept of MCA, BEC BGK 2020-21 Page 15


.NET Technology (PCA552L) 2BA18MCA05

System.Console.Write("Element({0}): ", i);


for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");

}
System.Console.WriteLine();
}
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}

Output:

Dept of MCA, BEC BGK 2020-21 Page 16


.NET Technology (PCA552L) 2BA18MCA05

Assignment 7

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.

Handling Exceptions

C# provides a structured solution to the exception handling in the form of try and catch
blocks. Using these blocks the core program statements are separated from the error-handling
statements.

These error handling blocks are implemented using the try, catch, and finally keywords.
Following is an example of throwing an exception when dividing by zero condition occurs .

Dept of MCA, BEC BGK 2020-21 Page 17


.NET Technology (PCA552L) 2BA18MCA05

/* Write a program to implement the exception handling keywords in c#. */

/***************************************************************************/

using System;

namespace ErrorHandlingApplication

class DivNumbers

int result;

DivNumbers() {

result = 0;

public void division(int num1, int num2) {

try {

result = num1 / num2;

catch (DivideByZeroException e)

Console.WriteLine("Exception caught: {0}", e);

finally {

Console.WriteLine("Result: {0}", result);

static void Main(string[] args)

Dept of MCA, BEC BGK 2020-21 Page 18


.NET Technology (PCA552L) 2BA18MCA05

DivNumbers d = new DivNumbers();

d.division(25, 0); // if it is passing variable changed as 25,3 - d.division(25, 3);

Console.ReadKey();

Output:

// if it is passing variable changed as 25,3 - d.division(25, 3);

Dept of MCA, BEC BGK 2020-21 Page 19


.NET Technology (PCA552L) 2BA18MCA05

Assignment 8

Virtual and Override in C#

 Virtual keyword is used for generating a virtual path for its derived classes on implementing
method overriding.

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.

Override keyword is used with virtual keyword.

/* Write a Program to Demonstrate Use of Virtual and override key words in C# with a
simple program.*/

/******************************************************************************/

using System;
namespace VirtualKey
{
class person

{
protected string fname;
protected string lname;
public person(string fn, string ln)
{
fname = fn;

lname = ln;
}
public virtual void display()
{
Console.WriteLine("Person :" + fname + " " + lname);

Dept of MCA, BEC BGK 2020-21 Page 20


.NET Technology (PCA552L) 2BA18MCA05

}
}
class emp:person
{

public ushort year;


public emp(string fn, string ln, ushort yr) : base(fn, ln)
{
year = yr;
}
public override void display()

{
Console.WriteLine ("Employee :" + fname + " " + lname + " " + year);
}
}
class worker : person
{

public String company;


public worker(string fn, string ln, string c):base (fn, ln)
{
company = c;
}
public override void display()

{
Console.WriteLine("Worker :" + fname + " " + lname + " " + company);
}
}
class Program

Dept of MCA, BEC BGK 2020-21 Page 21


.NET Technology (PCA552L) 2BA18MCA05

{
static void Main(string[]args)
{
Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***");

person p1 = new person("RAVI", "KUMAR");


person p2 = new emp("RAVI", "KUMAR", 2012);
person p3 = new worker("RAVI", "KUMAR", "XYZ SOLUTIONS");
p1.display ();
p2.display ();
p3.display ();

Console.ReadLine ();
}
}
}

Output:

Dept of MCA, BEC BGK 2020-21 Page 22


.NET Technology (PCA552L) 2BA18MCA05

Assignment 9

C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference


type variable that holds the reference to a method. The reference can be changed at runtime.

Delegates are especially used for implementing events and the call-back methods. All
delegates are implicitly derived from the System Delegate class.

Declaring Delegates

Delegate declaration determines the methods that can be referenced by the delegate. A
delegate can refer to a method, which has the same signature as that of the delegate.

For example, consider a delegate:

public delegate int MyDelegate (string s);


The preceding delegate can be used to reference any method that has a
single string parameter and returns an int type variable.

Syntax for delegate declaration is:

delegate <return type> <delegate-name> <parameter list>

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.

Dept of MCA, BEC BGK 2020-21 Page 23


.NET Technology (PCA552L) 2BA18MCA05

/* Write a Program in C# to create and implement a delegate for any two arithmetic
operations. */

/************************************************************************/

using System;

namespace delegate_demo

public delegate int DelegateSample(int a,int b);

public class sampleclass

public int add(int x, int y)

return x + y;

public int sub(int x, int y)

return x - y;

class program {

static void Main(String[] args)

sampleclass s = new sampleclass();

DelegateSample del = s.add;

int i = del(100, 20);

Console.WriteLine("Add result is: "+i);

DelegateSample del1 = s.sub;

Dept of MCA, BEC BGK 2020-21 Page 24


.NET Technology (PCA552L) 2BA18MCA05

int j = del1(10, 2);

Console.WriteLine("Sub result is: " + j);

Console.ReadLine();

Output:

Dept of MCA, BEC BGK 2020-21 Page 25


.NET Technology (PCA552L) 2BA18MCA05

Assignment 10

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.

The syntax of using the abstract method is as follows:

<access-modifier>abstract<return-type>method name (parameter)

/* Write a Program in C# to demonstrate abstract class and abstract methods in C#. */


/**********************************************************************************/

using System;

namespace abstract_demo {

abstract class person

protected string fname;

protected string lname;

public person(string fn, string ln)

fname = fn;

lname = ln;

Dept of MCA, BEC BGK 2020-21 Page 26


.NET Technology (PCA552L) 2BA18MCA05

public abstract void display() ;

class emp : person

public ushort year;

public emp(string fn, string ln, ushort yr): base(fn, ln)

year = yr;

public override void display()

Console.WriteLine("Employee :" + fname + " " + lname + " " + year);

class worker : person

public String company;

public worker(string fn, string ln, string c) : base(fn, ln)

company = c;

public override void display()

Console.WriteLine("Worker :" + fname + " " + lname + " " + company);

Dept of MCA, BEC BGK 2020-21 Page 27


.NET Technology (PCA552L) 2BA18MCA05

class Program

static void Main(string[] args)

Console.WriteLine("**ABSTRACT CLASS AND ABSTRACT METHODS DEMO **");

person p2 = new emp("Ram", "kumar", 2020);

person p3 = new worker("Ram", "kumar", " Infy solution");

p2.display();

p3.display();

Console.ReadLine();

Output:

Dept of MCA, BEC BGK 2020-21 Page 28


.NET Technology (PCA552L) 2BA18MCA05

Assignment 11

Properties are named members of classes, structures, and interfaces. Member variables or
methods in a class or structures are called Fields. Properties are an extension of fields and are
accessed using the same syntax. They use accessors through which the values of the private
fields can be read, written or manipulated.

Properties do not name the storage locations. Instead, they have accessors that read,
write, or compute their values.

For example, let us have a class named Student, with private fields for age, name, and
code. We cannot directly access these fields from outside the class scope, but we can have
properties for accessing these private fields.

Accessors

The accessor of a property contains the executable statements that helps in getting
(reading or computing) or setting (writing) the property. The accessor declarations can contain a
get accessor, a set accessor, or both.

/* 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#. */

/************************************************************************/

using System;
namespace properties_demo {

class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a Code and name property of type string:

public string Code {

Dept of MCA, BEC BGK 2020-21 Page 29


.NET Technology (PCA552L) 2BA18MCA05

get {
return code;
}
set {

code = value;
}
}
public string Name {
get {
return name;

}
set {
name = value;
}
}
// Declare a Age property of type int:

public int Age {


get {
return age;
}
set {
age = value;

}
}
public override string ToString() {
return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
}

Dept of MCA, BEC BGK 2020-21 Page 30


.NET Technology (PCA552L) 2BA18MCA05

}
class ExampleDemo {
public static void Main() {

// Create a new Student object:


Student s = new Student();

// Setting code, name and the age of the student


s.Code = "Arun01cs";
s.Name = "Aeunkumar";

s.Age = 26;
Console.WriteLine("Student Info: {0}", s);

// increasing age
s.Age += 1;
Console.WriteLine("Student Info: {0}", s);

Console.ReadKey();
}
}
}

Output:

Dept of MCA, BEC BGK 2020-21 Page 31


.NET Technology (PCA552L) 2BA18MCA05

Assignment 12

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

We declare interface by using the keyword interface which is as follows:-

 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.

/* Write a Program in C# Demonstrate arrays of interface types (for runtime


polymorphism).*/

/****************************************************************************/
using System;

namespace IntrDemo {

public interface Shape

void area();

public class circle : Shape

Dept of MCA, BEC BGK 2020-21 Page 32


.NET Technology (PCA552L) 2BA18MCA05

public void area()

double pi = 3.142;

Console.WriteLine("Calculating area of circle --------");

Console.WriteLine("Enter the radius : ");

float r = float.Parse(Console.ReadLine());

Console.WriteLine("Area of Circle is: "+ (pi * r * r));

public class square : Shape

public void area()

Console.WriteLine("Calculating area of Square --------");

Console.WriteLine("Enter the length : ");

float side = float.Parse(Console.ReadLine());

Console.WriteLine("Area of Square is: "+(side*side));

class program

static void Main(string[] args)

Shape[] s = new Shape[2];

s[0] = new circle();

s[1] = new square();

for (int i = 0; i < s.Length; i++)

Dept of MCA, BEC BGK 2020-21 Page 33


.NET Technology (PCA552L) 2BA18MCA05

s[i].area();

Output:

Dept of MCA, BEC BGK 2020-21 Page 34


.NET Technology (PCA552L) 2BA18MCA05

Assignment 13

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

/*****************************************************************/
Design page view

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;

Dept of MCA, BEC BGK 2020-21 Page 35


.NET Technology (PCA552L) 2BA18MCA05

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)

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

Dept of MCA, BEC BGK 2020-21 Page 36


.NET Technology (PCA552L) 2BA18MCA05

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

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

Dept of MCA, BEC BGK 2020-21 Page 37


.NET Technology (PCA552L) 2BA18MCA05

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

dataGridView1.DataSource = ds.Tables[0];

con.Close();

Dept of MCA, BEC BGK 2020-21 Page 38


.NET Technology (PCA552L) 2BA18MCA05

Output: view data and update data.

Dept of MCA, BEC BGK 2020-21 Page 39


.NET Technology (PCA552L) 2BA18MCA05

Assignment 14
Design and implement ASP.NET page to demonstrate basic validation specification.
/****************************************************************************/

<!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>
<style type="text/css">
.style1
{
height: 31px;
}
</style>
</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"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="rdvalid1" runat="server" ControlToValidate="uname"
ErrorMessage="Enter your name">
</asp:RequiredFieldValidator>
<br />
</td>
</tr>
<tr>
<td><asp:Label ID="Label5" Text="Gender" runat="server">
</asp:Label></td>
<td>
<asp:RadioButtonList ID="gender" runat="server" RepeatLayout="Flow">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="gender" ErrorMessage="Select your gender">
</asp:RequiredFieldValidator>

Dept of MCA, BEC BGK 2020-21 Page 40


.NET Technology (PCA552L) 2BA18MCA05

</td>
</tr>

<tr>
<td><asp:Label ID="semlb" Text="Semester" runat="server"></asp:Label></td>
<td><asp:TextBox ID="sem" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="sem" ErrorMessage="Enter your semester">
</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="Label6" Text="Phone Number" runat="server"></asp:Label></td>
<td><asp:TextBox ID="phone" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="phone" ErrorMessage="Enter your phone number">
</asp:RequiredFieldValidator>

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


ControlToValidate="phone" ErrorMessage="Enter valid Number"
ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
</tr>

<tr>
<td class="style3">
Email:
</td>

<td class="style2">
<asp:TextBox ID="stdemail" runat="server" style="width:250px">
</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="stdemail" ErrorMessage="Enter your valid email id"
ValidationExpression="\w+([-+.']\w+)@\w+([-.]\w+)\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>

Dept of MCA, BEC BGK 2020-21 Page 41


.NET Technology (PCA552L) 2BA18MCA05

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

Output:

Dept of MCA, BEC BGK 2020-21 Page 42


.NET Technology (PCA552L) 2BA18MCA05

Assignment 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];

Dept of MCA, BEC BGK 2020-21 Page 43


.NET Technology (PCA552L) 2BA18MCA05

count = datatable.Rows.Count;

if (count > 0)
{

Response.Write("Login Successful");

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

Output:

Dept of MCA, BEC BGK 2020-21 Page 44


.NET Technology (PCA552L) 2BA18MCA05

Dept of MCA, BEC BGK 2020-21 Page 45

You might also like