TY-BSCIT SUBJECT: Advanced Web Programming
Practical No. 7
A. Create a web application to display Databinding using dropdownlist
control.
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data
Source=COMP115;Initial Catalog=TYIT;Integrated Security =
True";
//stringconnStr =
ConfigurationManager.ConnectionStrings["connStr"].ConnectionSt
ring;
//SqlCommand cmd = new SqlCommand("Select
Id,city from City", conn);
SqlCommand cmd = new SqlCommand("select
ID,CITY from CITY11", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
DropDownList1.DataSource = reader;
DropDownList1.DataTextField = "City";
DropDownList1.DataBind();
reader.Close();
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs
e)
{
Label1.Text = "You Have Selected : " +
DropDownList1.SelectedValue;
}
}
}
SEM-V Roll no.: 711
TY-BSCIT SUBJECT: Advanced Web Programming
Output:
SEM-V Roll no.: 711
TY-BSCIT SUBJECT: Advanced Web Programming
B. Create a web application for to display the phone no of an author
using database
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data
Source=COMP115;Initial Catalog=TYIT;Integrated
Security=True;TrustServerCertificate=True";
//stringconnStr =
ConfigurationManager.ConnectionStrings["connStr"].ConnectionSt
ring;
//ConfigurationManager.ConnectionStrings["connStr"].Connection
String;
//SqlCommand cmd = new SqlCommand("Select
Id,city from City", conn);
SqlCommand cmd = new SqlCommand("select * from
BOOKCITY1", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
//DropDownList1.DataSource = reader;
//DropDownList1.DataTextField = "book";
//DropDownList1.DataBind();
DropDownList1.DataTextField = "book"; //
shown to user
DropDownList1.DataValueField = "id"; //
used in queries
DropDownList1.DataSource = reader;
DropDownList1.DataBind();
reader.Close();
conn.Close();
}
}
protected void
DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
protected void Button1_Click(object sender, EventArgs
e)
{
SEM-V Roll no.: 711
TY-BSCIT SUBJECT: Advanced Web Programming
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data
Source=COMP115;Initial Catalog=TYIT;Integrated
Security=True;TrustServerCertificate=True";
//string selectSQL;
//selectSQL = "select author from Book ";
//selectSQL += "WHERE id =" +
DropDownList1.SelectedValue;
//SqlCommand cmd = new SqlCommand(selectSQL,
conn);
string selectSQL = "SELECT AUTHOR FROM BOOKCITY1
WHERE ID = @ID";
SqlCommand cmd = new SqlCommand(selectSQL, conn);
cmd.Parameters.AddWithValue("@ID",
DropDownList1.SelectedValue);
cmd.Connection = conn;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Label1.Text = reader["AUTHOR"].ToString();
}
reader.Close();
conn.Close();
}
}
}
Output:
SEM-V Roll no.: 711
TY-BSCIT SUBJECT: Advanced Web Programming
C. Create a web application for inserting and deleting record
from a database. (Using Execute-Non Query).
Code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection.Emit;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace p7c
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=JCC-
GIGABYTE;Initial Catalog=TYIT;Integrated Security = True;
TrustServerCertificate = True";
string InsertQuery = "insert into Bank
values(@addr,@city,@bname,@state,@ZIP)";
SqlCommand cmd = new SqlCommand(InsertQuery, conn);
cmd.Parameters.AddWithValue("@addr", TextBox1.Text);
cmd.Parameters.AddWithValue("@city", TextBox2.Text);
cmd.Parameters.AddWithValue("@bname", TextBox3.Text);
cmd.Parameters.AddWithValue("@state", TextBox4.Text);
cmd.Parameters.AddWithValue("@ZIP", TextBox5.Text);
conn.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Record Inserted Successfuly.";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
SEM-V Roll no.: 711
TY-BSCIT SUBJECT: Advanced Web Programming
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=JCC-
GIGABYTE;Initial Catalog=TYIT;Integrated Security = True;
TrustServerCertificate = True";
string DQuery = "delete from Bank where bname=@bname";
SqlCommand cmd = new SqlCommand(DQuery, conn);
cmd.Parameters.AddWithValue("@bname", TextBox3.Text);
conn.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Record Deleted permanently.";
conn.Close();
}
}
}
OUTPUT:
SEM-V Roll no.: 711
TY-BSCIT SUBJECT: Advanced Web Programming
SEM-V Roll no.: 711