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

EXP 9 AND 10 ASP,ASP NET 2.0

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)
55 views

EXP 9 AND 10 ASP,ASP NET 2.0

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/ 12

EX.NO: 9 WEB APPLICATION IN ASP USING ADO.

NET
DATE:
AIM:
Program to demonstrate the web application in ASP using ADO.
ALGORITHM:
Step 1: Start the process.
Step 2: Create a ASP.NET Web Application.
Step 3: In the program add -> new item -> web form.
Step 4: Create a grid view for the code.
Step 5: Create database and Tables.
Step 6: Create a new SQL database in SQL Server.
Step 7: Create one or more tables in SQL Server.
Step 8: Create one or more tables in the database to store data.
Step 9: Add a Connection string to your web.config file to connect to the database.
Step 10: The data will be displayed in the web form. Stop the process.

PROGRAM:
Webform.aspx.cs
using System;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Insert data into the customers table
protected void btnInsert_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=DHYANI_PARI;Initial
Catalog=Students;Integrated Security=True;"))
{
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[customers] ([name],
[ph_no], [address]) VALUES (@Name, @Phone, @Address)", con);
// Add parameters to avoid SQL Injection
cmd.Parameters.AddWithValue("@Name", txtname.Text.Trim());
cmd.Parameters.AddWithValue("@Phone", txtphone.Text.Trim());
cmd.Parameters.AddWithValue("@Address", txtadd.Text.Trim());
try
{
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Response.Write("Data inserted successfully");
}
else
{
Response.Write("Insertion failed");
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
finally
{
con.Close();
}

// Clear the textboxes


txtname.Text = "";
txtphone.Text = "";
txtadd.Text = "";
}
}
// Delete data from the customers table
protected void btnDelete_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=DHYANI_PARI;Initial
Catalog=Students;Integrated Security=True;"))
{
SqlCommand cmd = new SqlCommand("DELETE FROM [dbo].[customers] WHERE
[name] = @Name", con);
// Add parameter to avoid SQL Injection
cmd.Parameters.AddWithValue("@Name", txtname.Text.Trim());
try
{
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Response.Write("Data deleted successfully");
}
else
{
Response.Write("No matching records found");
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
finally
{
con.Close();
}
}
// Clear the textboxes
txtname.Text = "";
}
}
}
Webform.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Customer Form</title>
<style type="text/css">
.autostyle1 {
height: 29px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtname" runat="server" /></td>
</tr>
<tr>
<td>Phone</td>
<td><asp:TextBox ID="txtphone" runat="server" /></td>
</tr>
<tr>
<td>Address</td>
<td><asp:TextBox ID="txtadd" runat="server" /></td>
</tr>
<tr>
<td>
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</td>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click"
/>
</td>
</tr>
</table>
</form>
</body>
</html>
OUTPUT:
COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)

RESULT:
Thus the above program called web application ado.net in asp has been executed successfully.
EX.NO: 10 CREATING A CUSTOM DATA-BOUND ASP.NET WEB
DATE: CONTROL FOR ASP.NET 2.0

AIM:
A program to demonstrate ASP.NET with web controls.
ALGORITHM:
Step 1 : Start the program
Step 2 : open the ASP.NET web application.
Step 3 : Right click on solution Explorer -> add new item -> web form server control & click Add.
Step 4 : Write the logic for customized controls in that and build it.
Step 5 : Create a new empty ASP.NET web application -> Solution explorer -> right click -> add new
item -> web forms and click add.
Step 6 : In toolbox, create a custom specification and choose the items of the previous web
application .dll.
Step 7 : Drag and drop the control in the web form.
Step 8 : Run the web form and perform the task.
Step 9 : Stop the process.
PROGRAM:
Webform1.aspx.cs:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : Page // Ensure it inherits from System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : CompositeControl
{
Label uname;
Label pswd; // Corrected variable name
Label label1;
TextBox textbox1;
TextBox textbox2;
Button button1;
LiteralControl lit;
protected override void CreateChildControls()
{
uname = new Label();
uname.Text = "Enter Username:";
uname.Width = Unit.Pixel(150);

pswd = new Label(); // Corrected variable name


pswd.Text = "Enter Password:";
pswd.Width = Unit.Pixel(150);

textbox1 = new TextBox();


textbox1.ID = "textbox1";
textbox1.Width = Unit.Pixel(150);

textbox2 = new TextBox();


textbox2.ID = "textbox2";
textbox2.Width = Unit.Pixel(150);
textbox2.TextMode = TextBoxMode.Password; // Hide password input

button1 = new Button();


button1.ID = "btnSubmit";
button1.Text = "Submit";
button1.Width = Unit.Pixel(100);
button1.Click += new EventHandler(btnSubmit_Click);
lit = new LiteralControl("<br/>");
label1 = new Label();
this.Controls.Add(uname);
this.Controls.Add(textbox1);
this.Controls.Add(lit);
this.Controls.Add(pswd);
this.Controls.Add(textbox2);
this.Controls.Add(lit);
this.Controls.Add(button1);
this.Controls.Add(lit);
this.Controls.Add(label1);
this.Controls.Add(lit);
}
protected override void Render(HtmlTextWriter writer)
{
uname.RenderControl(writer);
textbox1.RenderControl(writer);
lit.RenderControl(writer);

pswd.RenderControl(writer);
textbox2.RenderControl(writer);
lit.RenderControl(writer);

button1.RenderControl(writer);
lit.RenderControl(writer);
label1.RenderControl(writer);
lit.RenderControl(writer);
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (textbox1.Text == "Karthikeyan" && textbox2.Text == "23mcr019")
{
label1.Text = "Welcome";
label1.ForeColor = System.Drawing.Color.Blue;
}
else
{
label1.Text = "Wrong username or password";
label1.ForeColor = System.Drawing.Color.Red;
}
}
}
}
Webform.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication1.WebForm1" %>
<%@ Register TagPrefix="cc1" Namespace="WebApplication1" Assembly="WebApplication1" %>
<!-- Register the custom control -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Web Controls</title>
<style type="text/css">
.autostyle1 {
height: 29px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Web Controls</h2>
<cc1:WebCustomControl1 ID="CustomControl" runat="server" /> <!-- Updated with correct
tag prefix -->
</div>
</form>
</body>
</html>
OUTPUT:

saran

SRI

SRI

SRI

COE (30)
RECORD (20)
VIVA (10)
TOTAL (60)

RESULT:
Thus the above program demonstrate ASP.NET with controls has been executed successfully.

You might also like