Sei sulla pagina 1di 53

~1~

ASP .NET PROGRAMS


Unit 1 ( 5 and 7) :
( 1 ) Count Website...

First In website add “ Global.aspx ” File and Write Ciode...

And in defalt.aspx page pick one label..

Write Code in default.aspx page lode event...

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Application["c"].ToString();
}
}

Mayank Jayeshbhai Solanki


~2~
Unit 1 ( 4 and 9 ) :
( 2 ) Program 2:-

 First Create One new Empity website name


“StudentClassLibrary”. in website add a class file name is
“student” and Write code in a class file is.
 But You Remember the path where you save your website
“StudentClassLibrary”.
Code :-
namespace StudentClassLibrary
{
public class student
{
public int fect(int f)
{
int x = 1;
for (int i = 1; i <= f; i++)
{
x = x * i;
}
return x;
}
}
}

 After You write this code and next You Build thet website. Build
website Press F6 Button.
 When website is builded then close Your website and create new
website name any you give.
 In new website “Default.aspx” page in take One Textbox and One
Button. And write code in Button click
 Before or after take control first add your StudentClassLibrary
website in Your website.
 Adding StudentClassLibrary in Your website Open Solution
Explorer in Solution Explorer Select This
 And rightclick there and select
“AddReferance” then Opened one popup window and there find
your StudentClassLibrary website and go in to

Mayank Jayeshbhai Solanki


~3~
“ClassLibrary\bin\Debug” in Debug Folder one .dll file was there
the dll file was add in your website and write code in Button click..
 Before write code add Your Classlibreary header file...
using StudentClassLibrary;
 Then Next You Write code.....
Default.aspx :-

CODE :-
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 5; i++)
{
TextBox2.Text += "Gujarat Uni\n";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
student s = new student();
int v = Convert.ToInt32(TextBox1.Text);
int x = s.fect(v);
Label1.Text = x.ToString();
}
}

Mayank Jayeshbhai Solanki


~4~
: Write program To print “ Gujarat Univercity ” 5 Times.

Write This Code in default.aspx in sorce code.

( 3 ) Program : 3

Write a program to create sum, mul with function.

Default.aspx

Mayank Jayeshbhai Solanki


~5~

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click1(object sender, EventArgs e)
{
int a, b, c;
a = Convert.ToInt32(txtA.Text);
b = Convert.ToInt32(txtB.Text);
c = sum(a, b);
txtAdd.Text = c.ToString();
}
public int sum(int x, int y)
{
return x + y;
}
public int mul(int x, int y)
{
return x * y;
}
protected void btnmul_Click(object sender, EventArgs e)
{
int a, b, c;
a = Convert.ToInt32(txtA.Text);
b = Convert.ToInt32(txtB.Text);
c = mul(a,b);
txtMul.Text = c.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx");
}
}

Unit 2 ( 2 ) ,Unit 1 ( 8 and 2 ) :


( 4 ) Program : 4

: Write a program to take two link button and panel click one linkButton and
show one panel and click another link button to show another panel

: Write a program to change background color with use of


radiobrttonlistcontrol.

: Write a program to add item in lable when you check checkbox with use of
checkboxlist control.

Mayank Jayeshbhai Solanki


~6~
: Write a program to select one produde to dropdown list and show value of
product in lable and add qty in text box and click button to show total price of
the product in a lable.

In Radiobuttion change back color first add id of body in default.aspx page


sorce code and give id of body...
<body id="bodyid" runat="server">

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = false;
Panel3.Visible = false;
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Panel2.Visible = true;
Panel1.Visible = false;

Mayank Jayeshbhai Solanki


~7~
Panel3.Visible = false;
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
Panel3.Visible = true;
Panel2.Visible = false;
Panel1.Visible = false;
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
{
Label1.Text = "";
}
else if (DropDownList1.SelectedIndex == 1)
{
Label1.Text = (10).ToString();
}
else if (DropDownList1.SelectedIndex == 2)
{
Label1.Text = (40).ToString();
}
else if (DropDownList1.SelectedIndex == 3)
{
Label1.Text = (5).ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{

Label2.Text=((Convert.ToInt32(Txtcontity.Text))*(Convert.ToInt32(Label1.Text
))).ToString();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (RadioButtonList1.SelectedIndex == 0)
{
bodyid.Attributes.Add("bgcolor","red");
}
else if (RadioButtonList1.SelectedIndex == 1)
{
bodyid.Attributes.Add("bgcolor","green");
}
else if (RadioButtonList1.SelectedIndex == 2)
{
bodyid.Attributes.Add("bgcolor", "blue");
}
else if (RadioButtonList1.SelectedIndex == 3)
{
bodyid.Attributes.Add("bgcolor", "yellow");

Mayank Jayeshbhai Solanki


~8~
}
}
}

And End of the program last check boxlist program code


write in default.aspx page sorce code...

Unit 2 ( 9 ) :
( 5 ) Program 5:

Write a program to Advertisement with use of adrotator control.

First Pest three image in a web page in soluation Explorer.

Mayank Jayeshbhai Solanki


~9~
Default.aspx

Add XML file in website.

XMLFile.xml

Mayank Jayeshbhai Solanki


~ 10 ~
And this xml file to add in adrotator proparty.

Unit 2 ( 8 ) :
( 6 )Program 6 :

IMAGE MEP.

In 0 index Bottom = 150 and left = 100.

In 1 index Bottom = 150 and left = 200 and Right = 101.

In 2 index Bottom = 150 and left = 300 and Right = 201.

Mayank Jayeshbhai Solanki


~ 11 ~
( 7 ) Program 7:

COOKIS....

CODE In Button click :


public partial class Default2 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie c = new HttpCookie("c");
c["no"] = TextBox1.Text;
c["name"] = TextBox2.Text;
c["price"] = TextBox3.Text;
c["qty"] = TextBox4.Text;
Response.Cookies.Add(c);
Response.Redirect("cookis_page.aspx");
}
}

Add second page in website.

And write code in page lode event.

Default2.aspx

Mayank Jayeshbhai Solanki


~ 12 ~

CODE :
public partial class cookis_page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookies = Request.Cookies["c"];
if (cookies != null)
{
TextBox1.Text = cookies["no"].ToString();
TextBox2.Text = cookies["name"].ToString();
TextBox3.Text = cookies["price"].ToString();
TextBox4.Text = cookies["qty"].ToString();
}
}
}

Unit 2 ( 7 ) :

( 8 )Program 8:

COOKIS AND SESSION.

Default.aspx

Mayank Jayeshbhai Solanki


~ 13 ~

CODE ON BUTTON CLICK :


public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// Using Cookis...

HttpCookie c = new HttpCookie("username");


c.Value = TextBox1.Text;
c.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(c);
Response.Redirect("Default2.aspx");

//USING SESSION

/*
Session["username"] = TextBox1.Text;
Response.Redirect("Default2.aspx");
*/
}
}

Add Default2.aspx page.

And second page is blank.

Write code in page load event.

Mayank Jayeshbhai Solanki


~ 14 ~
CODE :
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//USING COOKIS..

if (Request.Cookies["username"] != null)
{
Response.Write(Request.Cookies["username"].Value.ToString());
}

//USING SESSION.

/*if (Session["username"] != null)


{
Response.Write(Session["username"].ToString());
}*/
}
}

( 9 ) Program 9 : (UNIT 2 : 1)

Default.aspx

Mayank Jayeshbhai Solanki


~ 15 ~
CODE :
public partial class _Default : System.Web.UI.Page
{
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
{
Label1.Text = "";
}
else if (DropDownList1.SelectedIndex == 1)
{
Label1.Text = (10).ToString();
}
else if (DropDownList1.SelectedIndex == 2)
{
Label1.Text = (20).ToString();
}
else if (DropDownList1.SelectedIndex == 3)
{
Label1.Text = (50).ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text =
(Convert.ToInt32(Label1.Text)*Convert.ToInt32(TextBox1.Text)).ToString();
}
}

Unit 2 ( 5 ) :
( 10 )Program 10: ( UNIT : 2_5)

: Write a program to select on city select in dropdown list and show states in
another dropdown list.

Mayank Jayeshbhai Solanki


~ 16 ~

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
DropDownList2.Items.Clear();
if (DropDownList1.SelectedIndex == 1)
{
DropDownList2.Items.Add("abc");
DropDownList2.Items.Add("def");
DropDownList2.Items.Add("ghi");
}
else if (DropDownList1.SelectedIndex == 2)
{
DropDownList2.Items.Add("jkl");
DropDownList2.Items.Add("mno");
DropDownList2.Items.Add("pqr");
}
else if (DropDownList1.SelectedIndex == 3)
{
DropDownList2.Items.Add("stu");
DropDownList2.Items.Add("vwx");
DropDownList2.Items.Add("yz");
}
}
}

Mayank Jayeshbhai Solanki


~ 17 ~
{ Image Upload Program Unit 2 ( 6 ) }
(11)Program 11:Write a Program To create Website in image
wa Upload in Spacific Folder with Use of FileUpload
Control.

 First Create New website and add naw item Default.aspx.


 In Default.aspx page controls: 2 Label, 1 FileUpload, 1
Button.
 Default.aspx

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExt =
System.IO.Path.GetExtension(FileUpload1.FileName);
int len = FileUpload1.PostedFile.ContentLength;
if (len > 10000)
{
Label2.Text = "Image is More then 10KS";
return;
}

Mayank Jayeshbhai Solanki


~ 18 ~
if (fileExt == ".jpg" || fileExt == ".bmp" || fileExt == ".png")
{
try
{
FileUpload1.SaveAs("E:\\Visual Studio Project\\som\\" +
FileUpload1.FileName);
Label2.Text = "File Name is: " +
FileUpload1.PostedFile.FileName + "<br>" + "Length :" +
FileUpload1.PostedFile.ContentLength + "Kb<br>" + "File Type :" +
FileUpload1.PostedFile.ContentType;
}
catch (Exception e1)
{
Label2.Text = e1.Message;
}
}
else
{
Label2.Text = "Only Jpg or bmp and png File is Upladed";
}
}
}
}

( 12 )Program 12 :

{ CLASSLIBRARY }
 First Create New project and celect Class library.
 In class library change class name in soluation Explorer. select class.cs
and press F2 function key and change class name. Not Change directly.
 And write code in a class.cs
 Remeber where Your classlibrary was saved ?

CODE :
namespace ClassLibrary2
{
public class student
{
int x = 1;
public int fact(int m)
{
for (int i = 1; i <= m; i++)
{
x = x * i;
}
return (x);

Mayank Jayeshbhai Solanki


~ 19 ~
}
}
//Buile this File..
}

 Then create New Website.


 And add Your class file in soluation Explorer.select website name and
right click there and select add reference and brouse your class file and
Select .dll file click ok.
 In default.aspx page pick one textbox.
 And write code in page lode event.

CODE :

 First add Your classlibreary file in hader.


 using ClassLibrary2;
 then second write code.

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

{
protected void Page_Load(object sender, EventArgs e)
{
student s = new student();
int x=s.fact(5);
TextBox1.Text = x.ToString();
}
}

( 13 )Program 13:

{ DATABASE }
Unit 4 ( 8 ) :
1. Insert, update, Delete.
 First create database in Access.
 Create table student and fild is id1,name1,dob1.
 Where dob fild change Formate = Short Date. And input Mask =
99/99/9999.

Mayank Jayeshbhai Solanki


~ 20 ~
 Then Opeon visiual studio and add database in Server explorer.
 Add One new web form in website.
 And write code on button click. ( Default.aspx ).

 First Add Hader File.


using System.Data.OleDb;

 Write Code :

CODE :
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\std.mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("insert into student
values('"+TextBox1.Text+"','"+TextBox2.Text+"',#"+TextBox3.Text+"#)",con);
cmd.ExecuteNonQuery();
Label4.Text = "data saved successfully...";
cmd.Dispose();

Mayank Jayeshbhai Solanki


~ 21 ~
}
protected void Button2_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("delete from student where
no1='"+TextBox1.Text+"'", con);
cmd.ExecuteNonQuery();
Label4.Text = "data Deleted successfully...";
cmd.Dispose();
}
protected void Button3_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("update student set
name1='"+TextBox2.Text+"',dob1=#"+TextBox3.Text+"# where
no1='"+TextBox1.Text+"'", con);
cmd.ExecuteNonQuery();
Label4.Text = "data Updated successfully...";
cmd.Dispose();
}
}

Unit 3 ( 2 ) :
2. LOGIN FORM WITH FORGET PASSOWRD.
 First Create Database In Access.
 And create table login and fild is uname1,password1,email1,user1.
 And insert two any data one admin and second local user.
 Opeon visual studio and add database in surver explorer.
 Write code in button Click event (Default.aspx).

Mayank Jayeshbhai Solanki


~ 22 ~
 First we insert hader file .
using System.Data.OleDb;

CODE :-
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\login.mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
TextBox1.Focus();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
OleDbDataReader red;
OleDbCommand cmd = new OleDbCommand("select * from login1 where
uname1='" + TextBox1.Text + "' and password1='" + TextBox2.Text + "'", con);
red = cmd.ExecuteReader();
if (red.Read() == true)
{
Label4.Text = "Authorised User..";
Label5.Text = red["user1"].ToString();
cmd.Dispose();
TextBox1.Text = "";
TextBox1.Focus();
}
else
{
Label4.Text = "Invalid user username or password";
Label5.Text = "";
TextBox2.Text = "";
TextBox2.Focus();
}
}
catch (Exception e1)
{
Label5.Text = e1.Message;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx");
}
}

Mayank Jayeshbhai Solanki


~ 23 ~
 Next Add new Web Foem For forgot password In Website.
 Default.aspx

 Write Code in Button click.


 First insert hader file.
using System.Data.OleDb;

CODE :
public partial class Default2 : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\login.mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
TextBox1.Focus();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
OleDbDataReader red;
OleDbCommand cmd = new OleDbCommand("select * from login1 where
uname1='"+TextBox1.Text+"'",con);
red = cmd.ExecuteReader();

Mayank Jayeshbhai Solanki


~ 24 ~
if (red.Read() == true)
{
Label4.Text = red["password1"].ToString();
cmd.Dispose();
}
else
{
Label4.Text = "Uname is Wrong";
TextBox1.Focus();
TextBox1.Text = "";
TextBox2.Text = "";
}
}
catch (Exception e1)
{
Label4.Text = e1.Message;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
}

3.select Doctor or patetion in Dropdownlist and show table


in Gridview.

 First Create One Database in create two table doctor


and petation.
 And doctor fild is id1,name1;
 And same fild was petation Table.
 And Insert value of those table.
 Opeon Visual studio and add adtbase in our website.
 Default.aspx

Mayank Jayeshbhai Solanki


~ 25 ~

 Write Code on dropdown list event.


 First Insert hader file.
using System.Data.OleDb;
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

CODE :
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\doctor_pateton.mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
if(DropDownList1.SelectedIndex==1)
{
try
{
OleDbCommand cmd = new OleDbCommand("select * from
doc",con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);

Mayank Jayeshbhai Solanki


~ 26 ~
if (dt.Rows.Count != 0)
{
Label1.Text = "Doctor table";
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
Label1.Text = "No Data Founded";
}
}
catch (Exception e1)
{
Label1.Text = e1.Message;
}
}
if (DropDownList1.SelectedIndex == 2)
{
try
{
OleDbCommand cmd = new OleDbCommand("select *from
pate",con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != null)
{
Label1.Text = "Petation Table";
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (Exception e1)
{
Label1.Text = e1.Message;
}
}
}
}

Unit 2 ( 10 ) :
4. Select Date From Calender Control And press Button then
Show Name Of person how Was Born in selected Date Othervise
give message.

 First Create Database person In Access.


 And in database Create table birthdate Fild was
id1,name1,dob1.

Mayank Jayeshbhai Solanki


~ 27 ~
 Where dob fild change Formate = Short Date. And input Mask =
99/99/9999.
 Opeon visual Studio and add database in our website.
 Default.aspx

 Write code in Button click Event and calendar event.


 First Insert hader file.
using System.Data.OleDb;
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
try
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\birthdate.mdb");
OleDbCommand cmd = new OleDbCommand("select * from student where
bdate=#"+TextBox1.Text+"#",con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);

Mayank Jayeshbhai Solanki


~ 28 ~
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
Label1.Text = "";
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
DateTime d;
d = Calendar1.SelectedDate;
TextBox1.Text = d.ToString("dd/MM/yyyy");
}
}

Unit 4 ( 6 ) :

5.Select student no on dropdown list and shaow detail of


student in repiter.and create database and tow table select
student detail of two diffrant table.

 First Create one database and two Table(std,marks).


 Std table fild was : no1(Primary key),name1,address1.
 Marks table fild was : no1,html,c_sharp,java.
 Second open visual studio and create new website.
 In form add controll : button,DropdownList,DetailView.
 Default.aspx

Mayank Jayeshbhai Solanki


~ 29 ~

CODE :

 First add Hader file.


using System.Data.OleDb;
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

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


{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\unit4(1).mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
public void demo1()
{
OleDbCommand cmd = new OleDbCommand("select * from std",con);
OleDbDataReader red;
red = cmd.ExecuteReader();
while (red.Read())
{
DropDownList1.Items.Add(red["no1"].ToString());
}
}
public void demo2()
{

Mayank Jayeshbhai Solanki


~ 30 ~
OleDbCommand cmd = new OleDbCommand("select
s.no1,s.name1,s.address1,m.html,m.c_sharp,m.java from std s,marks m where
s.no1='"+DropDownList1.SelectedIndex+"' and s.no1=m.no1",con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
protected void Button1_Click1(object sender, EventArgs e)
{
demo1();
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
demo2();
}
}

Unit 2 ( 3 ) :
6.Write a program to show your database table data on
repiter for button click.

 First create one database and create table(std).


 Std table fild is : no1,name1,address1.
 Add some data on std table.
 Opeon visual studio and create new website.
 In form add controls : repiter,button.

Mayank Jayeshbhai Solanki


~ 31 ~

 Write in sorce code.

 Write code on button click.


 First add Hader file.
using System.Data.OleDb;
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

CODE :

Mayank Jayeshbhai Solanki


~ 32 ~
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\unit4(1).mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("select * from std",con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}

Unit 4 ( 9 ) :

7.Write a program to create one database and insert value


with Quary and database fild eno was generated
Automaticaly and post and city is select into a
dropdownlist and insert data of button click.

 First Create database and create Table(emp).


 The Emp table Fild is:eno(Primary key), ename, epost,
ecity.
 Second opeon visual studio and create new website and
add new item.
 Default.aspx

Mayank Jayeshbhai Solanki


~ 33 ~

 Write Code on Button Click.


 First Add Hader file.
using System.Data.OleDb;

CODE :
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\unit4_9.mdb");
protected void Button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("insert into emp
values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+DropDownList1.SelectedValu
e+"','"+DropDownList2.SelectedValue+"')",con);
cmd.ExecuteNonQuery();
Label5.Text = "Data Saved Successfully...";
demo1();
TextBox2.Text = "";
TextBox2.Focus();
}
public void demo1()
{
int x=0;
OleDbCommand cmd = new OleDbCommand("Select *from emp",con);
OleDbDataReader red;
red = cmd.ExecuteReader();

Mayank Jayeshbhai Solanki


~ 34 ~
while (red.Read())
{
x = x + 1;
}
if(x<9)
{
TextBox1.Text = "E00";
}
else if (x >= 9 || x < 99)
{
TextBox1.Text = "E0";
}
else if (x >= 99)
{
TextBox1.Text = "E";
}
TextBox1.Text += (x+1).ToString();
}

protected void Page_Load(object sender, EventArgs e)


{
con.Open();
demo1();
TextBox2.Focus();
}
}

8.Write a program to show customer and product detail in a


gridview with join quary (unit 4_10).

 First Create one database and table(customer and


produdt).
 In customer table fild: cid,cname,caddress.
 In Product Table fild : pid,pname,pprice,pqty.
 Next opeon Visual studio and create one website.
 Default.aspx

Mayank Jayeshbhai Solanki


~ 35 ~

 Write code in Button click..


 First add hader file for database connection.
using System.Data.OleDb;
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

CODE :
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\unit4(10).mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd = new OleDbCommand("select c.name1 as Customer,
p.pcode As Product_Number,p.name1 As Product_Name, (p.price1*p.qty1) As
Total from pro p,cus c where c.ccode=p.pcode", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

Mayank Jayeshbhai Solanki


~ 36 ~
}
catch (Exception e1)
{
Label1.Text = e1.Message;
}
}
}

9[Unit 4(4)].Write a Program to create new website and pick


two LinkButton(electronic,book) when user can click one
linkButton then jump another page and there second page
lable print electronic and data was show on a gridview,and
same when user click second linkButton jump another page
and there lable print book and data was show in gridview.

 First create new website.


 In Default.aspx page pick two linkButton.
 And second Default2.aspx page pick one label,one
gridview.
 Then next Opeon acces and create one database.in
database create one table ele.
 In ele table fild was : id1, pname, ptype, price, qty.
 In ptype fild add 3 time electronic and 3 time add book
data and another fild you add your salf.
 And next opeon visual studio Opeon default.aspx page
 And write code on linkButton.
 DATABASE :

 Opeon visual studio..

Mayank Jayeshbhai Solanki


~ 37 ~

CODE :
public partial class _Default : System.Web.UI.Page
{
protected void LinkButton1_Click(object sender, EventArgs e)
{
HttpCookie c = new HttpCookie("product");
c["pname"] = LinkButton1.Text;
Response.Cookies.Add(c);
Response.Redirect("Default2.aspx");
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
HttpCookie c = new HttpCookie("product");
c["pname"] = LinkButton2.Text;
Response.Cookies.Add(c);
Response.Redirect("Default2.aspx");
}
}

 Next opeon Default2.aspx page.


 And in page write code in a page Lode event.

Mayank Jayeshbhai Solanki


~ 38 ~

CODE :

 First add hader file.


using System.Data.OleDb;
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

 Conneting a database connection and continye.


public partial class Default2 : System.Web.UI.Page
{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\electronic.mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
HttpCookie rc = Request.Cookies["product"];
if (rc != null)
{
Label1.Text = rc["pname"].ToString();
add1();
}
HttpCookie rc1 = Request.Cookies["product"];
if (rc1 != null)
{
Label1.Text = rc["pname"].ToString();
add1();
}
}

Mayank Jayeshbhai Solanki


~ 39 ~
public void add1()
{
OleDbCommand cmd = new OleDbCommand("select * from ele1 where
ptype='" + Label1.Text + "'", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

Unit 4 ( 5 ) :
5[Unit:-4(5)] : Write a program to create new website and
in a website user upload image in a database with use of
fileUpload control. And user is also show individual image
with use image no. When user add image no and click display
button and show image in image control.when user upload new
image in database gridview was atomomaticaly updated and
show data in a gridview.

 First create new website.


 And in Default.aspx page controls : 6 label, 4 Textbox,
1 FileUpload, 2 Button, 1 Image, 1 Gridview.
 Next Opeon Souation Explorer select your program
name(in second row round icon select) and right click
then show one pop up window and select New Folder.
 If You used visual studio 2012 then select your project
then opeon one popup window and select Add Option then
again show pop up window then select New Folder.
 When You select New Folder then one folder was created
in a soluation explorer and folder name was ‘Images’.
 In images folder any three images copy in your drive
folder and pest in the images folder.
 Then next create open Access.
 And create one database and in database create one
table(imagedata).
 In imagedata table fild was : pno, pname, pimage.
 DATABASE IMAGE IS :

Mayank Jayeshbhai Solanki


~ 40 ~

 Then Opeon visual studio.


 Default.aspx

 Then next select gridview control and click arrow


button then show one window. In window click Edit
Columns..
 Then opeon again one popup window and in window select
Boundfild and click Add Button 2 time the two Boundfild
was added in under blank box.
 Then next select Imagefild and click Add button then
one Imagefild was added in blank box.

Mayank Jayeshbhai Solanki


~ 41 ~
 Then change the fild name to select first Boundfild
then show proparties in right side window.
 Then select HadetText and wite your column name there.
 Then change all column name.
 In Imagefild change width and height in style select
controlstyle.
 Change Proparty...

CODE :

 Firat write code on Button Save(Button1) and


display(Button2).
 Second add hader file..
using System.Data.OleDb;

Mayank Jayeshbhai Solanki


~ 42 ~
using System.Data; (if You Use Visual Studio 2012, For DataTable Hint).

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


{
OleDbConnection con = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\unit4(5).mdb");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
add1();
}
public void add1()
{
try
{
OleDbCommand cmd = new OleDbCommand("select * from
imagedata",con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception e1)
{
Label1.Text = e1.Message;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string str = "images\\";
FileUpload1.SaveAs("E:\\Visual Studio Project\\som\\" +
FileUpload1.FileName);
str += FileUpload1.FileName;
OleDbCommand cmd = new OleDbCommand("insert into imagedata
values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+ str.ToString()
+"')",con);
cmd.ExecuteNonQuery();
Label1.Text = "data Saved";
cmd.Dispose();
add1();
}
}
protected void Button2_Click(object sender, EventArgs e)
{

Mayank Jayeshbhai Solanki


~ 43 ~
OleDbCommand cmd = new OleDbCommand("select * from imagedata where
pno='"+ TextBox3.Text.Trim() +"'",con);
OleDbDataReader red;
red = cmd.ExecuteReader();
if (red.Read() == true)
{
TextBox4.Text = red["pname"].ToString();
Image1.ImageUrl = red["pimage"].ToString();
}
}
}

Validation Controls (Unit-3[3,4])


Use Visual Studio 2008. Because 2012 is Not Support This Validation Controls
 Null value is not allowed.
 First Create new Website.
 Second Pick One Textbox,Button and
RequiredFieldValidator
 Set Error message and Control to validate.

 The birth date should appear between “1/1/1980” and


“1/1/2000”.
 Pick One Textbox,Button and RangeValidator.
 Set Error message and Control to validate.

Mayank Jayeshbhai Solanki


~ 44 ~

 Email should be valid id.


 Pick One Textbox,Button and
RegularExpressionValidator.
 Set Error message and Control to validate.

 Contact number exactly of 10 digits


 Pick One Textbox,Button and
RegularExpressionValidator.
 Set Error message and Control to validate.

Mayank Jayeshbhai Solanki


~ 45 ~

 To compare new password and retype password


 Pick One 2 Textbox,1 Button and CompareValidator.
 Set Error message and Control to valid

 The rollno should contain first 3 characters BCA. Example


BCA01, BCA02
 Pick One Textbox,Button and
RegularExpressionValidator.

Mayank Jayeshbhai Solanki


~ 46 ~
 Set Error message and Control to validate.

 Age should be between 18 to 35


 Pick One Textbox,Button and RangeValidator.
 Set Error message and Control to validate.

Mayank Jayeshbhai Solanki


~ 47 ~

Unit 4( program 7 )
 First Create New website in web site add two form
1) Register.aspx page
2) Login.aspx page
 Second Opeon Microsoft Access and create one database
and create one table reg.

 Database :-

1) Register.aspx page
 In Register.aspx page take 6 TextBox and 8 Label and 1
Button (Register).
 Write code on Button Click.

Mayank Jayeshbhai Solanki


~ 48 ~

Code :-

 First add Header File :

using System.Data.OleDb;

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


{
OleDbConnection conn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\Unit4_7.mdb");
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("insert into reg values('" +
TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text +
"','"+TextBox4.Text+"','"+TextBox5.Text+"','"+TextBox6.Text+"')", conn);
cmd.ExecuteNonQuery();
Label5.Text = "Data Saved successfully..";
Response.Redirect("Default2.aspx");
}
}

2) Login.aspx page..
 In Register.aspx page take 3 TextBox, 6 Label, 1 Button
(Login) and 1 Gridview.
 Write code on Button click..

Mayank Jayeshbhai Solanki


~ 49 ~

Code :
 First add Header file..

using System.Data.OleDb;
using System.Data;(if You Use Visual Studio 2012, For DataTable Hint).

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


{
OleDbConnection conn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Users\\DELL\\Desktop\\Mayank\\COLLAGE\\C
Sharp\\Database\\Unit4_7.mdb");
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("select username,upassword from
reg where username='" + TextBox1.Text + "' and
upassword='"+TextBox2.Text+"'", conn);
OleDbDataReader red;
red=cmd.ExecuteReader();
if (red.Read() == true)
{
red.Close();
cmd.Dispose();
Label3.Text = "Login Successfull..";

Mayank Jayeshbhai Solanki


~ 50 ~
OleDbCommand data = new OleDbCommand("select
cid,cname,caddress,cphone from reg where cid='" + TextBox3.Text + "'",
conn);
OleDbDataAdapter da = new OleDbDataAdapter(data);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
else
{
Label3.Text = "Sorry Something Was Wrong...";
}
}
}

MasterPage
 First Create New Empity website.
 Next open add new item popup box and select “Master page”.
 And press add.
 In master page write HTML code in sorce code.
 In website add 4 .aspx pages.and give link in html code.

Mayank Jayeshbhai Solanki


~ 51 ~
HTML Code :-

<%@ Master Language="C#" AutoEventWireup="true"


CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div class="wrapper">
<div class="manu">
<ul>
<li><a href="Default.aspx">Home</a></li>
<li><a href="Default2.aspx">Category</a></li>
<li><a href="Default3.aspx">Contect</a></li>
<li><a href="Default4.aspx">About</a></li>
</ul>
</div>
<div class="contect">
<asp:ContentPlaceHolder id="contentbody" runat="server">

</asp:ContentPlaceHolder>
</div>
<div class="clear"></div>
<div class="footer">
<h2>Mayank Solanki</h2>
</div>
</div>
</form>
</body>
</html>

 When you add 4 .aspx page first checked “Select master page”
And also check “place code in separated” checkboxes in add
new item window...

Mayank Jayeshbhai Solanki


~ 52 ~
 Next Add Stylesheet file from Add new item.
 Opeon add new item popup window and select “stylesheet” and
press add.then “StyleSheet.css” file was Created.

 Write CSS code in StyleSheet.css file

Code :

body,div,p,ui,li
{
padding:0;
margin:0;
}
body {
background-color:rgb(237,237,237);
font-family:"Arial",sans-serif;
font-size:12px;
}
.wrapper {
width:1000px;
margin:auto;
}
.contect {
width:100%;
background-color:rgb(254,254,254);
border:1px solid rgb(224,224,224);
float:left;

Mayank Jayeshbhai Solanki


~ 53 ~
margin-top:1px;
margin-bottom:1px;
min-height:520px;
}
.manu {
background-color: rgb(10,110,170);
width:100%;
margin:0px 0px 10px;
padding:0px;
height:40px;
color:rgb(243,243,243);
}
.navigation_first_item {
border-left:0px;
}
.navitem_s {
float:left;
border-right:1px solid rgb(10,85,125);
border-left:1px solid rgb(67,153,200);
height:40px;
background-color:rgb(14,79,114);
}
.manu ul li {
float: left;
display: block;
list-style: none;
border-right: 1px solid rgb(10,85,125);
border-left: 1px solid rgb(67,153,200);
}
.manu ul li a {
font-size:13px;
color:#fff;
font-weight:bold;
padding:8px 20px;
text-decoration:none;
line-height:40px;
}
.manu ul li:hover {
background-color:rgb(14,79,114);
border-right:1px solid rgb(14,89,130);
}
.clear {
clear:both;
}
.footer {
height:40px;
background-color:rgb(10,110,178);
color:rgb(225,225,225);
}
.footer h2 {
padding:15px;
text-align:center;}

Mayank Jayeshbhai Solanki

Potrebbero piacerti anche