[go: up one dir, main page]

0% found this document useful (0 votes)
13 views41 pages

Basic Programs

The document provides instructions and code examples for creating a web application in ASP.NET that performs CRUD operations (Create, Read, Update, Delete) on a database. It includes HTML source code for the web form, SQL commands for database setup, and C# code for handling user interactions and database operations. The application allows users to insert, update, and delete company data, including fields such as ID, name, address, contact number, and date of birth.

Uploaded by

itssatyam Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views41 pages

Basic Programs

The document provides instructions and code examples for creating a web application in ASP.NET that performs CRUD operations (Create, Read, Update, Delete) on a database. It includes HTML source code for the web form, SQL commands for database setup, and C# code for handling user interactions and database operations. The application allows users to insert, update, and delete company data, including fields such as ID, name, address, contact number, and date of birth.

Uploaded by

itssatyam Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

1) Write a program to insert update delete data in database

2) Write a program to insert update delete data in database with the help of crud operation
3) Write a program to insert update delete data in detail view and grid-view
4) Write a program to perform login-logout in asp.net using database
1) Write a program to insert update delete data in database

Design the form accordingly

Html source code :


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="inupdel.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 124px;
}
.auto-style3 {
width: 124px;
height: 15px;
}
.auto-style4 {
height: 15px;
}
.auto-style5 {
width: 124px;
height: 26px;
}
.auto-style6 {
height: 26px;
}
</style>
</head>
<body>
<form id="form1" runat="server">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True"
Font-Size="Large" ForeColor="#FF3300" Text="ABC Company"></asp:Label>
<br />
<br />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style2">id</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">name</td>
<td>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style5">address</td>
<td class="auto-style6">
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">contact no</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">date of birth</td>
<td class="auto-style4">
<asp:TextBox ID="TextBox5" runat="server" Type="date"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>
<asp:Button ID="Button1" runat="server" Text="insert" />
&nbsp;
<asp:Button ID="Button2" runat="server" Text="update" />
&nbsp;
<asp:Button ID="Button3" runat="server" Text="delete" />
</td>
</tr>
</table>
<div>
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
In the source code of textbox which is associated with date write

Type="date"

Design database accordingly


create database sam;

use sam;

drop database sam;

use master;

create table company(id int primary key not null,name varchar(20) not null,address varchar(20) not
null,contactno varchar(20) not null,dob date not null);

select * from company;

drop table company;

insert into company values(1,'sujay','dombivli',9833521197,'2021-10-04');

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;
using System.Data.SqlClient;

namespace inupdel
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QFR0A2O\SQLEXPRESS;Initial Catalog=sam;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//code on insert
SqlCommand cmd = new SqlCommand("insert into company values('"+txtid.Text+ "','" +
txtname.Text + "','" + txtadd.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "')", con);
con.Open();//connection open
cmd.ExecuteNonQuery();//this is to execute the query

Response.Write("data inserted successfully");// message will be diaplsye din top left


corner of form
Label2.Text = "data inserted";
con.Close();//connection close

protected void Button2_Click(object sender, EventArgs e)


{
//code on update
SqlCommand cmd = new SqlCommand("update company set name='" + txtname.Text +
"',address='" + txtadd.Text + "',contactno='" + txtcontact.Text + "',dob='" + txtdob.Text + "' where
id='" + txtid.Text + "'", con);
con.Open();
cmd.ExecuteNonQuery();//this is to execute the query

Response.Write("data updated successfully");// message will be diaplsye din top left


corner of form
Label2.Text = "data updated";
con.Close();
}

protected void Button3_Click(object sender, EventArgs e)


{
//code on delete
SqlCommand cmd = new SqlCommand("DELETE FROM company WHERE id='" +
txtid.Text + "'", con);
con.Open();
cmd.ExecuteNonQuery();//this is to execute the query
Response.Write("data deleted successfully");
con.Close();

}
}
}
MEthod 2 with try and catch block
C# 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;
using System.Data.SqlClient;

namespace samplewithwebform
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QFR0A2O\SQLEXPRESS;Initial Catalog=sam;Integrated Security=True");
SqlTransaction transaction;
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{

// SqlCommand cmd = new SqlCommand("insert into company values('" + txtid.Text +


"','" + txtname.Text + "','" + txtadd.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "')", con);

//SqlCommand cmd = new SqlCommand("insert into


company([id],[name],[address],[contactno],[dob]) OUTPUT
inserted.id,inserted.name,inserted.address,inserted.contactno,inserted.dob into recorda
values('" + txtid.Text + "','" + txtname.Text + "','" + txtadd.Text + "','" + txtcontact.Text + "','" +
txtdob.Text + "')", con);
//above is a way where we can insert data using output method
con.Open();
transaction = con.BeginTransaction();
try
{
new SqlCommand("insert into company values('" + txtid.Text + "','" + txtname.Text +
"','" + txtadd.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "')", con, transaction)
.ExecuteNonQuery();
new SqlCommand("insert into recorda values('" + txtid.Text + "','" + txtname.Text + "','"
+ txtadd.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "') ", con, transaction)
.ExecuteNonQuery();
// new SqlCommand("INSERT Qwery3 ", con, transaction)
// .ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException sqlError)
{
transaction.Rollback();
}

//cmd.ExecuteNonQuery();//this is to execute the query

Response.Write("data inserted successfully");// message will be diaplsye din top left


corner of form
Label2.Text = "data inserted";
con.Close();//connection close
}
/* SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QFR0A2O\SQLEXPRESS;Initial Catalog=sam;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
// SqlCommand cmd = new SqlCommand("insert into company values('" + txtid.Text +
"','" + txtname.Text + "','" + txtadd.Text + "','" + txtcontact.Text + "','" + txtdob.Text + "')", con);

SqlCommand cmd = new SqlCommand("insert into


company([id],[name],[address],[contactno],[dob]) OUTPUT
inserted.id,inserted.name,inserted.address,inserted.contactno,inserted.dob into recorda
values('" + txtid.Text + "','" + txtname.Text + "','" + txtadd.Text + "','" + txtcontact.Text + "','" +
txtdob.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();//this is to execute the query

Response.Write("data inserted successfully");// message will be diaplsye din top left


corner of form
Label2.Text = "data inserted";
con.Close();//connection close
}*/

protected void Button2_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("update company set name='" + txtname.Text +
"',address='" + txtadd.Text + "',contactno='" + txtcontact.Text + "',dob='" + txtdob.Text + "' where
id='" + txtid.Text + "'", con);
con.Open();
cmd.ExecuteNonQuery();//this is to execute the query

Response.Write("data updated successfully");// message will be diaplsye din top left


corner of form
Label2.Text = "data updated";
con.Close();
}

protected void Button3_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("DELETE FROM company WHERE id='" +
txtid.Text + "'", con);
con.Open();
cmd.ExecuteNonQuery();//this is to execute the query
Response.Write("data deleted successfully");
con.Close();

}
}
}

Output:

2) Write a program to insert update delete data in database with the help of crud
operation
Design the form accordingly

Html code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication22.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 124px;
}
.auto-style3 {
width: 124px;
height: 15px;
}
.auto-style4 {
height: 15px;
}
.auto-style5 {
width: 124px;
height: 26px;
}
.auto-style6 {
height: 26px;
}
</style>
</head>
<body>
<form id="form1" runat="server">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True"
Font-Size="Large" ForeColor="#FF3300" Text="ABC Company"></asp:Label>
<br />
<br />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style2">id</td>
<td>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">name</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style5">address</td>
<td class="auto-style6">
<asp:TextBox ID="txtadd" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">gender</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="auto-style3">date of birth</td>
<td class="auto-style4">
<asp:TextBox ID="txtdob" runat="server" Type="date"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">shift</td>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" Text="day" />
&nbsp;
<asp:RadioButton ID="RadioButton2" runat="server" Text="night" />
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="insert" />
&nbsp;
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click"
Text="update" />
&nbsp;
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click"
Text="delete" />
</td>
</tr>
</table>
<div>
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>

In the source code of textbox which is associated with date write

Type="date"
Design database accordingly

create database sam;


use sam;
drop database sam;
use master;

create table company(id int primary key not null,name varchar(20) not null,address
varchar(20) not null,gender varchar(20) not null,dob date not null,shift varchar(20) not
null);

select * from company;

drop table company;

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;
using System.Data.SqlClient;

namespace samplewithwebform
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QFR0A2O\SQLEXPRESS;Initial Catalog=sam;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{
//code on submit button

con.Open();
SqlCommand cmd = new SqlCommand("proceinsert '" + txtid.Text + "','" + txtname.Text
+ "','" + txtadd.Text + "','" + DropDownList1.SelectedValue + "','" + txtdob.Text + "','" +
RadioButtonList1.SelectedValue + "' ", con);

cmd.ExecuteNonQuery();
con.Close();
clear();
Label2.Text = "submitted successfully";

}
void clear()
{
txtid.Text = txtname.Text = txtadd.Text = DropDownList1.Text = txtdob.Text =
RadioButtonList1.Text = "";

Label2.Text = "";

protected void Button2_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("proceupdate '" + txtid.Text + "','" + txtname.Text
+ "','" + txtadd.Text + "','" + DropDownList1.SelectedValue + "','" + txtdob.Text + "','" +
RadioButtonList1.SelectedValue + "' ", con);
cmd.ExecuteNonQuery();
con.Close();
Label2.Text = "updated successfully";

protected void Button3_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand com = new SqlCommand("procedelete '" + txtid.Text + "'", con);
com.ExecuteNonQuery();
con.Close();
Label2.Text = "Deleted Sucessfully ";

}
}
}

Procedures for insert update and delete

For inserting the records

create proc proceinsert


@id int,
@name varchar(20),
@address varchar(20),
@gender varchar(20),
@dob date,
@shift varchar(20)
as

begin
insert into company(id,name,address,gender,dob,shift)
values(@id,@name,@address,@gender,@dob,@shift)
end

drop proc proceinsert;


Method 2
In above procedure we can add data in 2 tables simultaneously if we want

create proc proceinsert


@id int,
@name varchar(20),
@address varchar(20),
@gender varchar(20),
@dob date,
@shift varchar(20)
as

begin
insert into company(id,name,address,gender,dob,shift)
values(@id,@name,@address,@gender,@dob,@shift)
end

begin
insert into secondss(id,name,address)
values(@id,@name,@address)
End
// also create a table named secondss with above parameters we can add data in 2 tables
simultaneously
//create table secondss(id varchar(20),name varchar(20),address varchar(20));
Output for inserting 2 records
For updating the records

create proc proceupdate


@id int,
@name varchar(20),
@address varchar(20),
@gender varchar(20),
@dob date,
@shift varchar(20)
as
begin
update company set name=@name,address=@address,gender=@gender,
dob=@dob,shift=@shift
where id=@id
End
drop proc proceupdate;

For deleting the records

create proc procedelete


@id int
as
begin
delete company where id=@id
end
drop proc procedelete;

Output:
3) Write a program to insert update delete data in detail view and grid-view

Design the form accordingly

Above one is detail view and below one is gridview

HTML Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="samplewithwebform.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True"
Font-Size="Large" ForeColor="#CC3300" Text="Registration form for students"></asp:Label>
<div>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="Medium"
ForeColor="Red" Text="insert update delete using grid view in asp.net"></asp:Label>
<br />
<br />
<br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
</asp:DetailsView>
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>

Design the table studentdata accordingly always have a primary key in the table or else you
cannot edit in advanced options

create database sam;

use sam;

drop database sam;

use master;

create table studentdata(rollno int primary key not null,sname varchar(20) not null,fathername
varchar(20) not null,mothername varchar(20) not null);

select * from studentdata;

drop table studentdata;


Configure the sqldatasource1 to database in it click on advance

And select generate and ok


Above screenshot will be generated by clicking on top

right corner of gridview then autoformat and select the skin and apply ok .

then select the datasource of gridview to sqldatasource1


In above screenshot click on the checkbox of this 2 options enable editing and enable deleting

Now go to edit columns

By using arrow the command field in which edit and delete link buttons are there we will bring
them down so that they will go the the right side
Also by selecting roll no we can change the text i.e. heading text and then in item style we can
change the horizontal alignment to centre this is not compulsory we do if we want to

Also in the case of details view change the skin in autoformat

And change the datasource of it to sql datasource1

In detail view

Select the checkbox of enable inserting


Now select the detailed view and then go in propertiesà in behaviour in default mode select
insert in place of ready only

Now select the detailsview and in properties go in events

And in that double click on iteminserted


In it write the code

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)

Label1.Text = "data has been successfully inserted";

GridView1.DataBind();

Then select grid view and in events and double click on row updated

In it write the code

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)

Label1.Text = "data has been updated inserted";

GridView1.DataBind();

}
And then in grid view only go in events and rowdeleted doble click it

In it write the code

protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)


{

Label1.Text = "data has been deleted inserted";

GridView1.DataBind();

Output:
4) Write a program to perform login-logout in asp.net using database

Design the form accordingly

HTML Code:
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 29%;
height: 121px;
margin-left: 36px;
}
.auto-style3 {
width: 110px;
}
.auto-style4 {
width: 110px;
height: 23px;
}
.auto-style5 {
height: 23px;
}
</style>
</head>
<body style="width: 931px">
<form id="form1" runat="server">
<div>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True"
Font-Size="X-Large" ForeColor="#66FF99" Text="ABC Company"></asp:Label>
<br />
<br />
<br />
<table class="auto-style1">
<tr>
<td class="auto-style4">Username</td>
<td class="auto-style5">
<asp:TextBox ID="TextBox1" runat="server"
Height="32px"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td class="auto-style3">Password</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"
Height="32px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">&nbsp;</td>
<td>
<asp:Button ID="Button1" runat="server" Height="39px" Text="Login"
Width="73px" />
<br />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>

Select the text box of password and in it’s properties change the textmode to password
Design the database accordingly and insert 1 record which will be useful for login

use master;
create database logu;
use logu;
create table login(username varchar(20) not null,password varchar(20) not null);
select * from login;
insert into login values('sujay','pass123');

CODE on webform.aspx i.e login page

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

namespace samplewithwebform
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QFR0A2O\SQLEXPRESS;Initial Catalog=logu;Integrated
Security=True");
protected void Page_Load(object sender, EventArgs e)
{

protected void Button1_Click(object sender, EventArgs e)


{

String myquery = "select * from login";


SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
String uname;
String pass;
uname = ds.Tables[0].Rows[0]["username"].ToString();
pass = ds.Tables[0].Rows[0]["password"].ToString();
con.Close();
if (uname == TextBox1.Text && pass == TextBox2.Text)
{
Session["username"] = uname;
Response.Redirect("AdminHome.aspx");
}
else
{
Label2.Text = "Invalid Username or Password - Relogin with Correct Username
Password";

}
}
}
}
Design the AdminHome.aspx accordingly

HTML Code:

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


Inherits="samplewithwebform.AdminHome" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="ABC Company"></asp:Label>
<br />
<br />
<br />
welcome you have successfully login in our landing page<br />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="logout"
/>
</div>
</form>
</body>
</html>

C# code on AdminHome.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace samplewithwebform
{
public partial class AdminHome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
Response.Redirect("WebForm1.aspx");
}
else
{
Label2.Text = "Hello " + Session["username"].ToString();
}

protected void Button1_Click(object sender, EventArgs e)


{
Session.Abandon();
Response.Redirect("Signout.aspx");
}
}
}
HTML Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Signout.aspx.cs"
Inherits="samplewithwebform.Signout" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
You Have Successfully Logged Out<br />
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="WebForm1.aspx">Click here to go to login page</asp:HyperLink>
<br />
</div>
</form>
</body>
</html>

In hyper link set the navigate url property to WebForm1.aspx

You might also like