Beruflich Dokumente
Kultur Dokumente
namespace ConsoleApplication1
{
class Class3
{
static void Main()
{
Double r;
Double area;
Console.WriteLine("Enter radius:");
r = Convert.ToDouble(Console.ReadLine());
area = 3.14*r*r;
Console.WriteLine("Area of Circle:" +area);
Console.ReadLine();
}}}
Command line argument:
----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class4
{
static void Main(String[] args)
{
Console.WriteLine("Hello,{0}",args[0]);
Console.WriteLine("Welcome to C#");
}}}
simple interest
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class5
{
static void Main()
{
Double P, r, n;
Double si;
Console.WriteLine("Enter Principle Amount");
P = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter number of years:");
n = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Rate");
r = Convert.ToDouble(Console.ReadLine());
si = P * n * r / 100;
Console.WriteLine("Simple Interest:" + si);
}}}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class6
{
public static void Main()
{
Console.Write("What is your name?");
Console.Write("Hello, {0}!" , Console.ReadLine());
Console.WriteLine("Welcome to C#");
Console.ReadLine();
}}}
maths operation:
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class7
{
static void Main(string[]args)
{
Double a = -345.34, b = 23.87;
int i = 10, j = 20;
Console.WriteLine(Math.Abs(a));
Console.WriteLine(Math.Ceiling(b));
Console.WriteLine(Math.Floor(b));
Console.WriteLine(Math.Max(i, j));
Console.WriteLine(Math.Min(i, j));
Console.WriteLine(Math.Pow(2, 5));
Console.ReadLine();
}
}
}
area of rectangle
------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class rectangle
{
int height, width;
public void accept()
{
Console.WriteLine("Enter height and width");
height = Convert.ToInt32(Console.ReadLine());
width = Convert.ToInt32(Console.ReadLine());
}
public int area()
{
return height * width;
}
}
class Class8
{
static void Main()
{
rectangle r = new rectangle();
r.accept();
int a;
a = r.area();
Console.WriteLine("Area of rectangle:"+a);
}
}
}
Factorial of no.
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Class9
{
static void Main()
{int n;
Console.WriteLine("Enter number");
n = Convert.ToInt32(Console.ReadLine());
int f = fact(n);
Console.WriteLine("Factorial:" + f);
}
static int fact(int n)
{
if (n > 1)
return n*fact(n - 1);
else
return 1;
}
}
}
Leap year
-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int year;
Console.WriteLine("Enter the year Value :-");
year=Int32.Parse(Console.ReadLine());
if(year % 4 == 0 && year % 100 !=0) || year % 400 ==0 )
Console.WriteLine("The Year {0} is Leap year",year);
else
Console.WriteLine("The Year {0} is not Leap year",year);
Console.WriteLine("\n Press Enter to Quit......." );
Console.ReadLine();
}
}
}
jagged array
------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
public class Class1
{
public static void Main()
{
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[4];
a[2] = new int[3];
a[0][0] = 10; a[0][1] = 20;
a[1][0] = 30; a[1][1] = 40;
a[1][2] = 50; a[1][3] = 60;
a[2][0] = 70; a[2][1] = 80; a[2][2] = 90;
for (int i = 0; i <= a.GetUpperBound(0); i++)
{
for (int j = 0; j <= a[i].GetUpperBound(0); j++)
{
Console.Write(a[i][j] + " ");
}
Console.WriteLine();
}
}
}
}
drive information:
--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
foreach (DriveInfo d in DriveInfo.GetDrives())
{
try
{
Console.WriteLine(d.Name + " " + d.VolumeLabel + " " + d.TotalFreeSpace + " "
+ d.TotalSize);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
}} }
finally
----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sam_Console_App
{
class Class6
{
public static void Main()
{
int[] a = { 10, 20, 30, 40, 50, 60 };
int n;
try
{
Console.WriteLine("Enter Index Number");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Value " + a[n]);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Program End........");
}}}}
try catch
----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication12
{
class myexception : Exception
{
public myexception(string msg)
: base(msg)
{}
}
class Program
{
static void Main(string[] args)
{
int bal;
try
{
Console.WriteLine("Enter balance");
bal = Convert.ToInt32(Console.ReadLine());
if (bal < 500)
throw new myexception("Balanace is less");
Console.WriteLine("Balance is "+bal);
}
catch(myexception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}}}
user defined exception:
-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication12
{
class myexception : Exception
{
public myexception(string msg)
: base(msg)
{}
}
class Program
{
static void Main(string[] args)
{
int bal;
try
{
Console.WriteLine("Enter balance");
bal = Convert.ToInt32(Console.ReadLine());
}
catch(myexception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
} }}
create a file:
--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = null;
String data;
try
{
sw = new StreamWriter("c:\\b.txt");
Console.WriteLine("Enter data");
data = Console.ReadLine();
sw.WriteLine(data);
Console.WriteLine("File created");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (sw != null)
sw.Close();
}
Console.Read();
} }}
Random number
---------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
Random r = new Random();
Console.WriteLine(r.Next(10));
Console.WriteLine(r.Next(10,100));
Console.ReadLine();
}}}
5 string functions
------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication14
{
class Class1
{
static void Main(string[] args)
{
String s = " Hello ";
Console.WriteLine("Length:"+s.Length);
Console.WriteLine("Uppercase:"+s.ToUpper());
Console.WriteLine("Lowercase:"+s.ToLower());
Console.WriteLine("String:"+s.Trim());
String s1 = "Hello";
Console.WriteLine("Substring :"+s1.Substring(1));
Console.WriteLine("Substring :" + s1.Substring(1,3));
String s2 = "hello";
if (s1.Equals(s2))
{
Console.WriteLine("Equals");
}
else
{
Console.WriteLine("Not Equals");
}
Console.WriteLine("Replace : "+s2.Replace("e","i"));
String s3 = "This is a line of Text ";
Console.WriteLine("Index of : " + s3.IndexOf("is"));
Console.Read();
}}}
existence of file:
------------------
name and size of file:
------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication14
{
class Class2
{
static void Main(string[] args)
{
String fname;
Console.WriteLine("Enter the File Name :");
fname = Console.ReadLine();
FileInfo f = new FileInfo(fname);
if (f.Exists)
{
Console.WriteLine(" Name :" + f.FullName);
Console.WriteLine("Size:" + f.Length);
}
else
{
Console.WriteLine("File does not Exists ");
}
Console.Read();
}}}
Directory
-------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication14
{ class Class3
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("HTMLPage.htm");
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Default3.aspx");
}
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
Response.Redirect("Default.aspx");
}
}
checkbox control
-----------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
Label1.Font.Bold = true;
}
else
{
Label1.Font.Bold = false;
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox2.Checked == true)
{
Label1.Font.Italic = true;
}
else
{
Label1.Font.Italic = false;
}
}
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox3.Checked == true)
{
Label1.Font.Underline = true;
}
else
{
Label1.Font.Underline = false;
}
}
}
Dropdown list
--------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue;
}
}
adrotator control
------------------
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>
D:\Documents and Settings\Sameer\My Documents\My Pictures\the_fallen_-_wallpaper
</ImageUrl>
<NavigateUrl>Default.aspx</NavigateUrl>
<AlternateText>Flower</AlternateText>
<Keyword>a</Keyword>
<Impression>100</Impression>
</Ad>
<Ad>
<ImageUrl>
C:\nature_zh2urmp4.jpg
</ImageUrl>
<NavigateUrl>HTMLPage.htm</NavigateUrl>
<AlternateText>Nature</AlternateText>
<Keyword>b</Keyword>
<Impression>300</Impression>
</Ad>
<Ad>
<ImageUrl>C:\Winter.jpg</ImageUrl>
<NavigateUrl>Default3.aspx</NavigateUrl>
<AlternateText>winter</AlternateText>
<Keyword>c</Keyword>
<Impression>200</Impression>
</Ad>
<Ad>
<ImageUrl>C:\Sunset.jpg</ImageUrl>
<NavigateUrl>Default5.aspx</NavigateUrl>
<AlternateText>Sunset</AlternateText>
<Keyword>d</Keyword>
<Impression>400</Impression>
</Ad>
</Advertisements>
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx");
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs
args)
{
string s = args.Value;
if (s.Equals("M1") || s.Equals("M2") || s.Equals("M3"))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
}
add modify and delete DATABASE
-------------------------------
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new OleDbConnection
("provider=microsoft.jet.oledb.4.0;data source=d:\\smita\\db1.mdb");
protected void Page_Load(object sender, EventArgs e)
{
display();
}
void display()
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from emp", con);
DataSet ds = new DataSet();
da.Fill(ds, "emp");
GridView1.DataSource = ds.Tables["emp"].DefaultView;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string s;
s = string.Format("insert into emp values ({0},'{1}',{2},{3})", txteno.Text,
txtename.Text, txtsal.Text, txtdeptno.Text);
try
{
OleDbCommand cmd = new OleDbCommand(s, con);
con.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Record inserted";
}
catch (Exception ex)
{
Label1.Text = "Insertion failed!!";
}
finally
{
con.Close();
display();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string s;
s = string.Format("update emp set ename='{0}',sal={1},deptno={2} where eno={3}",
txtename.Text, txtsal.Text, txtdeptno.Text, txteno.Text);
try
{
OleDbCommand cmd = new OleDbCommand(s, con);
con.Open();
if (cmd.ExecuteNonQuery() == 1)
Label1.Text = "Record updated";
else
Label1.Text = "Empno does not exist";
display();
}
catch (Exception ex)
{
Label1.Text = "Updation failed!!";
}
finally
{
con.Close();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string s;
s = "delete from emp where eno=" + txteno.Text;
try
{
OleDbCommand cmd = new OleDbCommand(s, con);
con.Open();
if (cmd.ExecuteNonQuery() == 1)
Label1.Text = "Record deleted";
else
Label1.Text = "Empno does not exist";
display();
}
catch (Exception ex)
{
Label1.Text = "deletion failed";
}
finally
{
con.Close();
}}}
calender control:
----------------
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString();
}
}