You are on page 1of 13

Ex.

No: 4 Create a web application to


 Insert 3 records inside the SQL database table having following fields( DeptId,
DeptName, EmpName, Salary).
 Update the salary for any one employee and increment it to 15% of the present
salary.
 Perform delete operation on 1 row of the database table

Aim:

Alogorithm:
Webpage Design:
Program:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


Inherits="_Default" %>

<!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>Untitled Page</title>
<style type="text/css">
.style1
{
font-size: xx-large;
text-align: center;
color: #3333FF;
}
.style2
{
width: 600px;
font-size: x-large;
}
.style4
{
text-align: center;
}
.style5
{
width: 136px;
text-align: center;
}
.style6
{
width: 240px;
text-align: center;
}
.style7
{
width: 100%;
font-size: x-large;
}
.style8
{
height: 24px;
text-align: left;
}
.style9
{
height: 24px;
text-align: center;
width: 405px;
}
.style10
{
width: 100%;
font-size: x-large;
}
.style11
{
width: 428px;
}
.style13
{
background-color: #FFFF66;
}
#form1
{
background-color: #CCFFFF;
}
.style14
{
color: #000000;
background-color: #66FFFF;
}
.style15
{
text-align: center;
font-size: x-large;
font-weight: bold;
}
.style16
{
background-color: #00FFFF;
}
</style>
</head>
<body>
<p class="style1">
Web Application for Database Manipulation</p>
<form id="form1" runat="server">
<table class="style2" align="center">
<tr>
<td class="style15" colspan="4">
<span class="style14">Employee Information</span></td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label1" runat="server" Text="Deptartment ID"></asp:Label>
</td>
<td class="style5">
<asp:Label ID="Label2" runat="server" Text="Deptartment
Name"></asp:Label>
</td>
<td class="style6">
Employee Name</td>
<td style="text-align: center">
Salary</td>
</tr>
<tr>
<td class="style4">
<asp:TextBox ID="txtDeptID1" runat="server"></asp:TextBox>
</td>
<td class="style5">
<asp:TextBox ID="txtDeptName1" runat="server"></asp:TextBox>
</td>
<td class="style6">
<asp:TextBox ID="txtEmpName1" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtSalary1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
<asp:TextBox ID="txtDeptID2" runat="server"></asp:TextBox>
</td>
<td class="style5">
<asp:TextBox ID="txtDeptName2" runat="server"></asp:TextBox>
</td>
<td class="style6">
<asp:TextBox ID="txtEmpName2" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtSalary2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
<asp:TextBox ID="txtDeptID3" runat="server"></asp:TextBox>
</td>
<td class="style5">
<asp:TextBox ID="txtDeptName3" runat="server"></asp:TextBox>
</td>
<td class="style6">
<asp:TextBox ID="txtEmpName3" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtSalary3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4" colspan="4">
<asp:Button ID="btnInsert" runat="server"
style="text-align: center; font-size: x-large" Text="Insert Employee Data"
onclick="BtnInsert_Click" />
</td>
</tr>
</table>
<br />
<table class="style7" align="center">
<tr>
<td colspan="2">
<table class="style2" align="center">
<tr>
<td class="style15" >
<span class="style16">Increment Salary for Empoyee</span></td>
</tr>
</table>
</td>
</tr>
<tr align="center">
<td class="style9" style="text-align:right">
<asp:Label ID="Label3" runat="server" Text="Enter Employee
Name"></asp:Label>
</td>
<td class="style8">
<asp:TextBox ID="txtEmpName4" runat="server" style="text-align: left"
Width="214px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="btnUpdate" runat="server"
style="text-align: center; font-size: x-large" Text="Do Increment"
onclick="btnUpdate_Click" />
</td>
</tr>
</table>
<span class="style13">
<br />
</span>
<table class="style10" align="center">
<tr>
<td class="style15" colspan="2">
<span class="style16">Delete Empoyee details</span></td>
</tr>
<tr>
<td class="style11" style="text-align: right">
<asp:Label ID="Label4" runat="server" Text="Enter Employee
Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpName5" runat="server" style="margin-left:
0px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="btnDelete" runat="server" style="font-size: x-large"
Text="Do Delete" onclick="btnDelete_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
Output: Record Insersion

Updating Record information


Default.aspx.cs
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
con=new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial
Catalog=MCA2A;Integrated Security=True");
con.Open();
}
protected void BtnInsert_Click(object sender, EventArgs e)
{
string qryString;
SqlCommand cmd;
qryString = "insert into dept values(" + txtDeptID1.Text + ",'" + txtDeptName1.Text + "','"
+ txtEmpName1.Text + "'," + txtSalary1.Text + ")";
cmd = new SqlCommand(qryString, con);
cmd.ExecuteNonQuery();

qryString = "insert into dept values(" + txtDeptID2.Text + ",'" + txtDeptName2.Text + "','"


+ txtEmpName2.Text + "'," + txtSalary2.Text + ")";
cmd = new SqlCommand(qryString, con);
cmd.ExecuteNonQuery();
qryString = "insert into dept values(" + txtDeptID3.Text + ",'" + txtDeptName3.Text + "','" +
txtEmpName3.Text + "'," + txtSalary3.Text + ")";
Deleting a Recrod
cmd = new SqlCommand(qryString, con);
int rowsAffected=cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Response.Write("<script language=javascript>alert('New Empoyees
Inserted');</script>");
btnInsert.Enabled = false;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
string qryString = "Update dept set salary =salary * 1.15 where EmpName='"+
txtEmpName4.Text + "'";
SqlCommand cmd = new SqlCommand(qryString, con);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Response.Write("<script language=javascript>alert('Salary Updated');</script>");
btnUpdate.Enabled = false;
}
else
Response.Write("<script language=javascript>alert('No Such a Employee');</script>");
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string qryString = "Delete dept where EmpName='" + txtEmpName5.Text + "'";
SqlCommand cmd = new SqlCommand(qryString, con);
int rowsAffected=cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Response.Write("<script language=javascript>alert('Record Deleted');</script>");
btnDelete.Enabled = false;
}
else
Response.Write("<script language=javascript>alert('No Such a Employee');</script>");
}
}
Result:

You might also like