PR 10
PR 10
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
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;
}
.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 {
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 />
<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" />
<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;
try
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
MySqlCommand cmd;
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);
}
}
Code3:-Web.config
<connectionStrings>
<add name="MyDatabase"
connectionString="Server=localhost;Database=mydatabase;User=root;Password=18
46;"
providerName="MySql.Data.MySqlClient" />
Output:-
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.