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

PR 10

Uploaded by

dhairyajoshi991
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)
13 views

PR 10

Uploaded by

dhairyajoshi991
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/ 10

ITM(SLS) BARODA UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY


Diploma Semester – 5(A)
ASP.Net

Practical-10
Aim:- Write an exercise, to allow the user to ADD, UPDATE, MODIFY his profile once
he has logged into the website using Bound and Unbound Controls.
Theory:-
The goal of this exercise is to allow a logged-in user to Add, Update, and Modify their
profile using Bound and Unbound controls in ASP.NET. This involves:
 Bound controls: Automatically bind data from the database to the user interface.
 Unbound controls: Require manual coding to transfer data between the control
and the database.
Code1:-Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master"


AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CRUD._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">


<style>
/* General styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0px;
}

main {
max-width: 800px;
margin: auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* Form styles */
.form-container {
margin-bottom: 30px;
}

.form-container label {
display: block;
margin: 10px 0 5px;
font-weight: bold;
}

Name:- Dhairya joshi Enrollment No:-22C11027 1


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net

.form-input {
width: calc(100% - 22px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 15px;
}

.btn {
background-color: #28a745;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}

.btn:hover {
background-color: #218838;
}

.btn-action {
background-color: #FF0000;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}

.btn-action:hover {
background-color: #0056b3;
}

.user-list {
margin-top: 20px;
}

/* Table styles */
.user-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}

.user-table th, .user-table td {

Name:- Dhairya joshi Enrollment No:-22C11027 2


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}

.user-table th {
background-color: #f2f2f2;
}

.hidden-label {
display: none; /* Hide the label for ID */
}
</style>

<main>
<!-- User Input Form -->
<div class="form-container">
<asp:Label ID="lblID" runat="server" CssClass="hidden-label"
Visible="false"></asp:Label> <!-- Hidden label for User ID -->
<label for="txtName">Name:</label>
<asp:TextBox ID="txtName" runat="server"
CssClass="form-input"></asp:TextBox><br />

<label for="txtEmail">Email:</label>
<asp:TextBox ID="txtEmail" runat="server"
CssClass="form-input"></asp:TextBox><br />

<label for="txtAddress">Address:</label>
<asp:TextBox ID="txtAddress" runat="server"
CssClass="form-input"></asp:TextBox><br />

<asp:Button ID="btnInsert" runat="server" Text="Insert/Update"


OnClick="InsertOrUpdateUser" CssClass="btn" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel"
OnClick="CancelOperation" CssClass="btn" Visible="false" />
</div>

<!-- User List -->


<div class="user-list">
<asp:Repeater ID="rptUsers" runat="server"
OnItemCommand="rptUsers_ItemCommand">
<HeaderTemplate>
<table class="user-table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Actions</th>

Name:- Dhairya joshi Enrollment No:-22C11027 3


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net
</tr>
</thead>
<tbody>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
<td><%# Eval("Email") %></td>
<td><%# Eval("Address") %></td>
<td>
<!-- Edit Button -->
<asp:LinkButton ID="btnEdit" runat="server"
CommandName="EditUser" CommandArgument='<%# Eval("ID") %>' Text="Edit"
CssClass="btn-action" />

<!-- Delete Button -->


<asp:LinkButton ID="btnDelete" runat="server"
CommandName="DeleteUser" CommandArgument='<%# Eval("ID") %>' Text="Delete"
CssClass="btn-action" OnClientClick="return confirm('Are you sure you want to delete
this user?');" />
</td>
</tr>
</ItemTemplate>

<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</main>
</asp:Content>

Code2:-Default.aspx.cs

using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Web.UI.WebControls;

namespace CRUD
{
public partial class _Default : System.Web.UI.Page
{
private string connectionString =
ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

Name:- Dhairya joshi Enrollment No:-22C11027 4


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net

protected void Page_Load(object sender, EventArgs e)


{
if (!IsPostBack)
{
BindUsers();
}
}

protected void BindUsers()


{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users1",
conn); // Updated table name
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
rptUsers.DataSource = dt;
rptUsers.DataBind();
}
}

protected void InsertOrUpdateUser(object sender, EventArgs e)


{
string name = txtName.Text;
string email = txtEmail.Text;
string address = txtAddress.Text;

try
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
MySqlCommand cmd;

if (!string.IsNullOrEmpty(lblID.Text)) // If editing an existing user


{
cmd = new MySqlCommand("UPDATE users1 SET Name = @Name,
Email = @Email, Address = @Address WHERE ID = @ID", conn); // Updated table
name
cmd.Parameters.AddWithValue("@ID", lblID.Text);
}
else // If inserting a new user
{
cmd = new MySqlCommand("INSERT INTO users1 (Name, Email,
Address) VALUES (@Name, @Email, @Address)", conn); // Updated table name
}

Name:- Dhairya joshi Enrollment No:-22C11027 5


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net

cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Email", email);
cmd.Parameters.AddWithValue("@Address", address);
cmd.ExecuteNonQuery();
}

ClearInputs();
BindUsers();
}
catch (Exception ex)
{
// Log the actual error message
Response.Write("Error: " + ex.Message);
}
}

protected void rptUsers_ItemCommand(object source,


RepeaterCommandEventArgs e)
{
if (e.CommandName == "EditUser")
{
EditUser(Convert.ToInt32(e.CommandArgument));
}
else if (e.CommandName == "DeleteUser")
{
DeleteUser(Convert.ToInt32(e.CommandArgument));
}
}

private void EditUser(int userID)


{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users1
WHERE ID = @ID", conn); // Updated table name
cmd.Parameters.AddWithValue("@ID", userID);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
lblID.Text = reader["ID"].ToString(); // Set ID for editing
txtName.Text = reader["Name"].ToString();
txtEmail.Text = reader["Email"].ToString();
txtAddress.Text = reader["Address"].ToString();
}
}
}

Name:- Dhairya joshi Enrollment No:-22C11027 6


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net
btnCancel.Visible = true; // Show cancel button while editing
}

private void DeleteUser(int userID)


{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("DELETE FROM users1
WHERE ID = @ID", conn); // Updated table name
cmd.Parameters.AddWithValue("@ID", userID);
cmd.ExecuteNonQuery();
}

BindUsers(); // Refresh user list after deletion


}

protected void CancelOperation(object sender, EventArgs e)


{
ClearInputs();
btnCancel.Visible = false;
}

private void ClearInputs()


{
lblID.Text = "";
txtName.Text = "";
txtEmail.Text = "";
txtAddress.Text = "";
}
}
}

Code3:-Web.config

<?xml version="1.0" encoding="utf-8"?>


<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>

<connectionStrings>
<add name="MyDatabase"

connectionString="Server=localhost;Database=mydatabase;User=root;Password=18
46;"
providerName="MySql.Data.MySqlClient" />

Name:- Dhairya joshi Enrollment No:-22C11027 7


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add
assembly="Microsoft.AspNet.Web.Optimization.WebForms"
namespace="Microsoft.AspNet.Web.Optimization.WebForms"
tagPrefix="webopt" />
</controls>
</pages>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime"
publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2"
newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity
name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0"
newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json"
publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0"
newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease"
publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930"
newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity
name="System.Runtime.CompilerServices.Unsafe"
publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0"
newVersion="6.0.0.0" />
</dependentAssembly>

Name:- Dhairya joshi Enrollment No:-22C11027 8


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net
<dependentAssembly>
<assemblyIdentity name="System.Memory"
publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2"
newVersion="4.0.1.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvide
r, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4"
compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript"
extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4"
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\
&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>

Output:-

Name:- Dhairya joshi Enrollment No:-22C11027 9


ITM(SLS) BARODA UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE, ENGINEERING AND TECHNOLOGY
Diploma Semester – 5(A)
ASP.Net

Output:-

Output:-

Conclusion:-in this practical,we learn that how add,edit or delete the content in the
existing website with the help of CRUD operation.

Name:- Dhairya joshi Enrollment No:-22C11027 10

You might also like