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

C#.Net Lab Record Programs

The document contains 9 code examples demonstrating different C# programming concepts like checking if a number is a palindrome, prime number checking, command line arguments, finding the largest element in an array, matrix multiplication, summing a jagged array, string manipulation, different types of constructors, and inheritance. Each code example is preceded by its aim and followed by sample output.

Uploaded by

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

C#.Net Lab Record Programs

The document contains 9 code examples demonstrating different C# programming concepts like checking if a number is a palindrome, prime number checking, command line arguments, finding the largest element in an array, matrix multiplication, summing a jagged array, string manipulation, different types of constructors, and inheritance. Each code example is preceded by its aim and followed by sample output.

Uploaded by

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

Aim:

1. Write a program to check whether a number is Palindrome or not.

Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Palindrome
{
class Program
{
static void Main(string[] args)
{
int n, r, sum = 0, temp;
Console.Write("Enter the Number: ");
n = int.Parse(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
Console.ReadLine();
}
}
}
Output:
Aim:
2. Write a program to check whether a number is Prime or not.

Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prime
{
class Program
{
static void Main(string[] args)
{
int n, i, m = 0, flag = 0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
Console.Write("Number is not Prime.");
flag = 1;
break;
}
}
if (flag == 0)
Console.Write("Number is Prime.");

Console.ReadLine();
}
}
}
Output:
Aim:
3. Write a program to demonstrate Command line arguments processing.

Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// Command line arguments
Console.WriteLine("Argument length: " + args.Length);
Console.WriteLine("Supplied Arguments are:");
foreach (Object obj in args)
{
Console.WriteLine(obj);
}
}
}
}
Output:
Aim:
4. Write a program to find the largest and second largest element in a single dimensional
array.

Program Code:
namespace Largest
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter the size of the array: ");
int size = int.Parse(Console.ReadLine());

int[] arr = new int[size];

Console.WriteLine("Enter the array elements:");


for (int i = 0; i < size; i++)
arr[i] = int.Parse(Console.ReadLine());

int large = arr[0];


int large2 = arr[0];

for (int i = 1; i < arr.Length; i++)


{
if (arr[i] > large)
{
large2 = large;
large = arr[i];
}
else if (arr[i] > large2 && arr[i] != large)
{
large2 = arr[i];
}
}

Console.WriteLine("The largest element is: " + large);


Console.WriteLine("The second largest element is: " + large2);
}
}
}
Output:
Aim:
5. Write a program to multiply two matrices using rectangular arrays.

Program Code:
using System;

namespace RectArray
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows & columns for the 1st matrix: ");
int r1 = int.Parse(Console.ReadLine());
int c1 = int.Parse(Console.ReadLine());
int[,] m1 = new int[r1,c1];

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


for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
m1[i, j] = int.Parse(Console.ReadLine());

Console.Write("Enter the number of rows & columns for the 2nd matrix: ");
int r2 = int.Parse(Console.ReadLine());
int c2 = int.Parse(Console.ReadLine());
int[,] m2 = new int[r1,c1];

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


for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
m2[i, j] = int.Parse(Console.ReadLine());

if (c1 != r2)
{
Console.WriteLine("The number of column in m1 not equal to number of row in
m2");
return;
}

int[,] m3 = new int[r1, c2];

for (int i = 0; i < r1; i++)


for (int j = 0; j < c2; j++)
for (int k = 0; k < c1; k++)
m3[i,j] += m1[i,k] * m2[k,j];

Console.WriteLine("The product of the two matrices is:");

for (int i = 0; i < r1; i++)


{
for (int j = 0; j < c2; j++)
Console.Write($"{m3[i,j]} ");
Console.WriteLine();
}
}
}
}
Output:
Aim:
6. Find the sum of all the elements present in a jagged array of 3 inner arrays.

Program Code:
using System;

namespace JaggedArray
{
internal class Program
{
static void Main(string[] args)
{
int[][] jaggedArray = new int[3][];

Console.WriteLine("Enter 3 inner arrays:");


for (int i = 0; i < 3; i++)
{
Console.Write("Enter the size of inner array " + (i + 1) + ": ");
int size = int.Parse(Console.ReadLine());
jaggedArray[i] = new int[size];

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


for (int j = 0; j < size; j++)
jaggedArray[i][j] = int.Parse(Console.ReadLine());
}

int sum = 0;
foreach (int[] innerArray in jaggedArray)
foreach (int element in innerArray)
sum += element;

Console.WriteLine("\nThe sum of all elements in the jagged array is " + sum);


}
}}
Output:
Aim:
7. Write a program to demonstrate string manipulation.

Program Code:
namespace StringManip
{
internal class Program
{
static void Main(string[] args)
{
string str1 = "Hello World!";
string str2 = " Welcome to C# Programming! ";

// 1. Length
Console.WriteLine("Length of str1: " + str1.Length);

// 2. ToUpper
Console.WriteLine("str1 in uppercase: " + str1.ToUpper());

// 3. ToLower
Console.WriteLine("str1 in lowercase: " + str1.ToLower());

// 4. Concat
Console.WriteLine("Concatenated string: " + String.Concat(str1, str2));

// 5. IndexOf
Console.WriteLine("Index of 'World' in str1: " + str1.IndexOf("World"));

// 6. Substring
Console.WriteLine("Substring of str1: " + str1.Substring(0, 5));

// 7. Replace
Console.WriteLine("Replaced string: " + str1.Replace("Hello", "Hi"));

// 8. Trim
Console.WriteLine("Trimmed string: " + str2.Trim());

// 9. Split
string[] words = str2.Split(' ');
Console.WriteLine("Words in str2:");
foreach (string word in words)
{
Console.WriteLine(word);
}

// 10. StartsWith
Console.WriteLine("str1 starts with 'Hello': " + str1.StartsWith("Hello"));

// 11. EndsWith
Console.WriteLine("str1 ends with 'World!': " + str1.EndsWith("World!"));
// 12. Contains
Console.WriteLine("str1 contains 'llo': " + str1.Contains("llo"));

// 13. Compare
Console.WriteLine("str1 compared to 'hello world': " + String.Compare(str1, "hello
world"));

// 14. Equals
Console.WriteLine("str1 equals 'Hello World!': " + str1.Equals("Hello World!"));

// 15. IsNullOrEmpty
Console.WriteLine("str1 is null or empty: " + String.IsNullOrEmpty(str1));

// 16. IsNullOrWhiteSpace
Console.WriteLine("str2 is null or whitespace: " + String.IsNullOrWhiteSpace(str2));

// 17. PadLeft
Console.WriteLine("str1 padded on the left: " + str1.PadLeft(15));

// 18. PadRight
Console.WriteLine("str1 padded on the right: " + str1.PadRight(15));

// 19. Insert
Console.WriteLine("str1 with 'Hey ': " + str1.Insert(0, "Hey "));

// 20. Remove
Console.WriteLine("str1 with 'World' removed: " + str1.Remove(6, 5));
}
}
}
Output:
Aim:
8. Write a program to demonstrate different types of constructors.

Program Code:
namespace constructors
{
class Person
{
public string Name;
public int Age;

//default constructor
public Person()
{
Name = "Unknown";
Age = 0;
}
// Constructor with parameters
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Copy constructor
public Person(Person other)
{
Name = other.Name;
Age = other.Age;
}
}
internal class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
Console.WriteLine("Name: "+p1.Name+", Age: "+p1.Age);

Person p2 = new Person("John", 30);


Console.WriteLine("Name: "+ p2.Name +", Age: "+p2.Age);

Person p3 = new Person(p2);


Console.WriteLine("Name: "+p3.Name+", Age: "+p3.Age);
}
}
}
Output:
Aim:
9. Write a program to demonstrate inheritance.

Program Code:
namespace Inheritance
{
class Vehicle
{
public string Brand;
public string Model;
public int Year;
public void Start()
{
Console.WriteLine("Vehicle started.");
}
public void Stop()
{
Console.WriteLine("Vehicle stopped.");
}
}

class Car : Vehicle


{
public int NumWheels;
public void Accelerate()
{
Console.WriteLine("Car accelerating.");
}
public void Brake()
{
Console.WriteLine("Car braking.");
}
}

class Motorcycle : Vehicle


{
public bool HasSidecar;
public void Wheelie()
{
Console.WriteLine("Motorcycle doing a wheelie.");
}
}

class SUV : Car


{
public int CargoCapacity;

public void OffRoad()


{
Console.WriteLine("SUV off-roading.");
}
}

internal class Program


{
static void Main(string[] args)
{
Vehicle vehicle = new Vehicle();
Car car = new Car();
Motorcycle motorcycle = new Motorcycle();
SUV suv = new SUV();

vehicle.Start();
car.Accelerate();
motorcycle.Wheelie();
suv.OffRoad();

Console.ReadLine();
}
}
}
Output:
Aim:
10. Write a program to demonstrate interface.

Program Code:
using System;
namespace Interface
{
interface IPolygon
{
void calcArea(int l, int b);
}
class Rectangle: IPolygon
{
public void calcArea(int l, int b)
{
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
internal class Program
{
static void Main(string[] args)
{
Rectangle r1= new Rectangle();
Console.Write("Enter Length: ");
int l = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Breadth: ");
int b = Convert.ToInt32(Console.ReadLine());
r1.calcArea(l, b);
}
}
}
Output:
Aim:
11. Demonstrate arrays of interface types with a C# program.

Program Code:
using System.Transactions;

namespace InterfaceTypes
{
interface IShape
{
double CalArea();
}

class Circle : IShape


{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double CalArea()
{
return Math.PI * radius * radius;
}
}

class Rectangle : IShape


{
private double width, height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double CalArea()
{
return width * height;
}
}
internal class Program
{
static void Main(string[] args)
{
IShape[] shapes = new IShape[3];

Console.WriteLine("Enter the radius of circle 1: ");


int r1 = int.Parse(Console.ReadLine());
shapes[0] = new Circle(r1);

Console.WriteLine("Enter the length and breadth of rectangle 1:");


int l1 = int.Parse(Console.ReadLine());
int b1 = int.Parse(Console.ReadLine());
shapes[1] = new Rectangle(l1,b1);

Console.WriteLine("Enter the radius of circle 2: ");


int r2 = int.Parse(Console.ReadLine());
shapes[2] = new Circle(r2);

Console.WriteLine("Areas of shapes:");
foreach (IShape shape in shapes)
Console.WriteLine(shape.CalArea());
}
}
}
Output:
Aim:
12. Write a program to demonstrate Operator overloading.

Program Code:
namespace OperatorOverloading
{
class Calculator
{
public int num;
public Calculator(){
num = 0;
}
public Calculator(int num1){
this.num = num1;
}
public static Calculator operator +(Calculator c1,Calculator c2)
{
Calculator c3 = new Calculator();
c3.num = c1.num +c2.num;
return c3;
}
public static Calculator operator -(Calculator c1, Calculator c2)
{
Calculator c3 = new Calculator();
c3.num = c1.num - c2.num;
return c3;
}
public void display()
{
Console.WriteLine(num);
}
}
internal class Program{
static void Main(string[] args){
Console.Write("Enter number 1: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter number 2: ");
int num2 = int.Parse(Console.ReadLine());
Calculator c1 = new Calculator(num1);
Calculator c2 = new Calculator(num2);
Calculator c3,c4;
c3 = c1 + c2;
Console.Write("Addition: ");
c3.display();
c4 = c2 - c1;
Console.Write("Subtraction: ");
c4.display();
}
}
}
Output:
Aim:
13. Write a program to demonstrate delegates.

Program Code:
using System;
namespace Delegates
{
internal class Program
{
public delegate void addnum(int a,int b);
public delegate void subnum(int a, int b);

public void add(int a,int b)


{
Console.WriteLine("{0} + {1} = {2}",a, b, a + b);
}
public void sub(int a,int b)
{
Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
}
static void Main(string[] args)
{
Program obj=new Program();

addnum del1 = new addnum(obj.add);


subnum del2 = new subnum(obj.sub);

Console.Write("Enter first number: ");


int a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int b = Convert.ToInt32(Console.ReadLine());

del1(a, b);
del2(a, b);
}
}
}
Output:
Aim:
14. Write a program to demonstrate events.

Program Code:
using System;
namespace Events
{
class Calculator
{
public delegate void Cal(int result);
public event Cal CalCompleted;
public void Add(int num1, int num2)
{
int result = num1 + num2;
CalCompleted?.Invoke(result);
}
}
internal class Program
{
static void Main(string[] args)
{
Calculator cal = new Calculator();
cal.CalCompleted += Complete;
Console.Write("Enter number 1: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Enter number 2: ");
int num2 = int.Parse(Console.ReadLine());
cal.Add(num1,num2);
}
private static void Complete(int result)
{
Console.WriteLine("Calculation completed. Result: " + result);
}
}
}
Output:
Aim:
15. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.

Program Code:
namespace ExceptionHandling
{
class DivByZero : Exception
{
public DivByZero()
{
Console.WriteLine("Exception has occurred: ");
}
}
internal class Program
{
public double DivisionOperation(double num,double den)
{
if (den== 0)
throw new DivByZero();

return num/den;
}

static void Main(string[] args)


{
Program obj = new Program();
Console.Write("Enter Numerator: ");
double num = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Denominator: ");
double den = Convert.ToDouble(Console.ReadLine());
double quotient;
try
{
quotient = obj.DivisionOperation(num,den);
Console.WriteLine("Quotient = {0}", quotient);
}
catch (Exception e){
Console.Write(e.Message);
}
finally
{
Console.WriteLine("finally block executed");
}
}
}
}
Output:
Aim:
16. Write a program to design a registration form.

Program Code:
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.OleDb;

namespace registration
{
public partial class Form1 : Form
{
private bool rb;
private bool cb;

public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
if (radioButton1.Checked != false || radioButton2.Checked != false)
rb = true;
if (checkBox1.Checked != false || checkBox2.Checked != false || checkBox3.Checked
!= false || checkBox4.Checked != false)
cb = true;
if (textBox1.Text != "" && textBox2.Text != "" && (rb) &&
dateTimePicker1.Checked != false && textBox3.Text != "" && (cb))
MessageBox.Show("Registration was successfully");
else
MessageBox.Show("Please enter all fields");
}

private void button2_Click_1(object sender, EventArgs e)


{
MessageBox.Show("Form cleared");
}
}
}
Output:
Aim:
17. Write a program to design an application that uses Menu Control.

Program Code:
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;

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

private void addToolStripMenuItem_Click(object sender, EventArgs e)


{
int a = int.Parse(textBox1.Text), b = int.Parse(textBox2.Text), c;
c = a + b;
MessageBox.Show("Addition: " + c.ToString());
}

private void subToolStripMenuItem_Click(object sender, EventArgs e)


{
int a = int.Parse(textBox1.Text), b = int.Parse(textBox2.Text), c;
c = a - b;
MessageBox.Show("Subtraction: " + c.ToString());
}

private void mulToolStripMenuItem_Click(object sender, EventArgs e)


{
int a = int.Parse(textBox1.Text), b = int.Parse(textBox2.Text), c;
c = a * b;
MessageBox.Show("Multiplication: " + c.ToString());
}

private void divToolStripMenuItem_Click(object sender, EventArgs e)


{
int a = int.Parse(textBox1.Text), b = int.Parse(textBox2.Text), c;
c = a / b;
MessageBox.Show("Division: " + c.ToString());
}
private void modToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text), b = int.Parse(textBox2.Text), c;
c = a % b;
MessageBox.Show("Modulus: " + c.ToString());
}

private void concatToolStripMenuItem_Click(object sender, EventArgs e)


{
string str1 = textBox1.Text, str2 = textBox2.Text, str3;
str3 = str1 + str2;
MessageBox.Show("String Concatenation: " + str3);
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)


{
string str1 = textBox1.Text, str2;
str2 = str1;
MessageBox.Show("String Copy: " + str2);
}

}
}
Output:
Aim:
18. Write a program to create windows database application.

Program Code:
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.OleDb;

namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=D:\Programming\Visual Programming Projects\Record\18.
WindowsFormsApplication7\dbapp.accdb");
//OleDbConnection con = new OleDbConnection(@"Provider=OraOLEDB.Oracle;Data
Source=XE;User ID=C##fossbin;Password=oracle;");
int count = 0;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into dbtab
values('"+textBox1.Text+"','"+textBox2.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record(s) inserted successfully!!!");
}

private void button4_Click(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from dbtab";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}

private void button2_Click(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from dbtab where name = '" + textBox1.Text + "' ";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record(s) deleted successfully!!!");
}

private void button3_Click(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update dbtab set city = '" + textBox2.Text + "' where name = '"
+ textBox1.Text + "' ";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record(s) updated successfully!!!");
}

private void button5_Click(object sender, EventArgs e)


{
count = 0;
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from dbtab where name = '" + textBox1.Text + "' ";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
count = Convert.ToInt32(dt.Rows.Count.ToString());
dataGridView1.DataSource = dt;
con.Close();
if (count == 0)
{
MessageBox.Show("Record not found!!!");
}
}

private void Form1_Load(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from dbtab";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}

}
}
Output:
Aim:
19. Write a program to create web database application.

Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=D:\Programming\Visual Programming Projects\Record\19.
WebApplication1\Database11.accdb");
//OleDbConnection con = new OleDbConnection(@"Provider=OraOLEDB.Oracle;Data
Source=XE;User ID=C##fossbin;Password=oracle;");

protected void Page_Load(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from mgu";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
GridView1.DataSource = dt;
da.Fill(dt);
GridView1.DataBind();
con.Close();
}

protected void Button1_Click1(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into mgu values('" + TextBox1.Text + "','" +
TextBox2.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
TextBox2.Text = "";
Response.Write("<script>alert('Record inserted successfully!');</script>");
}

protected void Button2_Click1(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update mgu set username='" + TextBox1.Text + "'where
password='" + TextBox2.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Record updated successfully!');</script>");
}

protected void Button3_Click(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from mgu where username='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Record deleted successfully!');</script>");
}

protected void Button4_Click1(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from mgu";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
GridView1.DataSource = dt;
da.Fill(dt);
GridView1.DataBind();
con.Close();
Response.Write("<script>alert('Record(s) displayed successfully!');</script>");
}

protected void Button5_Click(object sender, EventArgs e)


{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from mgu where username='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
GridView1.DataSource = dt;
da.Fill(dt);
GridView1.DataBind();
con.Close();
Response.Write("<script>alert('Record displayed successfully!');</script>");
}
}
}
Output:
Aim:
20. Write a program to create a web application that uses validation controls.

Program Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace validation
{
public partial class validation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RangeValidator1.MinimumValue = DateTime.Now.AddYears(-
45).ToShortDateString();
RangeValidator1.MaximumValue = DateTime.Now.AddYears(-
18).ToShortDateString();
}

protected void CustomValidator1_ServerValidate(object source,


ServerValidateEventArgs args)
{
int len = args.Value.Length;
if (len >= 8 && len <= 15)
args.IsValid = true;
else
args.IsValid = false;
}

protected void Button1_Click(object sender, EventArgs e)


{
}
}
}
Output:

You might also like