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

AWP Practical 7

This document provides code examples for three database-related practical exercises in a web programming course. Exercise A creates an application that binds data from a database to a dropdown list and textbox on button click. Exercise B displays an author's phone number by querying a database based on their ID. Exercise C demonstrates inserting and deleting records from a database using ExecuteNonQuery, validating the changes.

Uploaded by

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

AWP Practical 7

This document provides code examples for three database-related practical exercises in a web programming course. Exercise A creates an application that binds data from a database to a dropdown list and textbox on button click. Exercise B displays an author's phone number by querying a database based on their ID. Exercise C demonstrates inserting and deleting records from a database using ExecuteNonQuery, validating the changes.

Uploaded by

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

T.Y.B.SC(I.T.

)-ADVANCED WEB PROGRAMMING-PAPER(III)


IT-7239
2018-2019

Practical 7
Working with Database

A) Create a web application bind data in a multiline textbox by querying in


another textbox.
Program-

7a.aspx-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="7a.aspx.cs" Inherits="_7a" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="VALUE" />
<br /><br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataTextField="name"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>

Design-

Page 1|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

7a.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _7a : System.Web.UI.Page
{
String str;
SqlConnection cn = new SqlConnection("Data Source=VICKY-PC;Initial
Catalog=studentdetails;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(DropDownList1.SelectedItem);
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from stud_dtls", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "name";
DropDownList1.DataBind();
}
}

Output-

After clicking on button and selecting a value from dropdownlist-

Page 2|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

B) Create a web application for to display the phone no of an author using


database.
Program-

7b.aspx-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="7b.aspx.cs" Inherits="_7b" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<asp:Label ID="Label1" runat="server" Text="Enter Author's ID-"></asp:Label>


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Author's Phone Number-"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Page 3|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

</div>
</form>
</body>
</html>
Design-

7b.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _7b : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection("Data Source=vicky-PC;Initial
Catalog=aut_det;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from author_details where id=" +
TextBox1.Text + "", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox2.Text = Convert.ToString(dr["phoneno"]);
}

}
}

Page 4|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

Output-

Entering ID and clicking on Button-

C) Create a web application for inserting and deleting record from a


database. (Using Execute-Non Query).
Program-
7c.aspx-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="7c.aspx.cs" Inherits="_7c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<asp:Label ID="Label1" runat="server" Text="ID-"></asp:Label>


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Name-"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
Page 5|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" />
&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Delete" />
<br />
<br />
<asp:Label ID="Label3" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

Design-

7c.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _7c : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=VICKY-PC;Initial
Catalog=studentdetails;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into stud_dtls values('" + TextBox1.Text +
"','" + TextBox2.Text + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
Label3.Text = "Data inserted successfully";
Page 6|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=VICKY-PC;Initial
Catalog=studentdetails;Integrated Security=True");
SqlCommand cmd = new SqlCommand("delete from stud_dtls where id=" + TextBox1.Text
+ "", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Label3.Text = "Data deleted successfully";
}
}
Output-

Entering data and clicking on Insert button-

Database After inserting data-

Page 7|8
T.Y.B.SC(I.T.)-ADVANCED WEB PROGRAMMING-PAPER(III)
IT-7239
2018-2019

Entering ID and deleting a data-

Database after deleting a record-

Page 8|8

You might also like