CRUD operations are fundamental for any web application that interacts with a database. In ASP.NET Web Pages, you can easily implement these operations using ADO.NET or SQL Helper. Below, we will explore how to perform Create, Read, Update, and Delete operations step by step.
Prerequisites
- Ensure you have a SQL Server database set up.
- Have a table created in your database (e.g.,
Users
with columnsID
,Name
, andEmail
).
1. Create Operation
The Create operation allows you to insert new records into the database. Below is an example of how to create a new user.
@{
var connectionString = "Server=your_server_name;Database=your_database_name;User Id=your_username;Password=your_password;";
if (IsPost)
{
var name = Request.Form["Name"];
var email = Request.Form["Email"];
using (var connection = new SqlConnection(connectionString))
{
var query = "INSERT INTO Users (Name, Email) VALUES (@Name, @Email)";
using (var command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@Email", email);
connection.Open();
command.ExecuteNonQuery();
}
}
<p>User created successfully!</p>
}
}
<form method="post">
<input type="text" name="Name" placeholder="Name" required />
<input type="email" name="Email" placeholder="Email" required />
<button type="submit">Create User</button>
</form>
2. Read Operation
The Read operation allows you to retrieve records from the database. Below is an example of how to read and display all users.
@{
using (var connection = new SqlConnection(connectionString))
{
var query = "SELECT * FROM Users";
using (var command = new SqlCommand(query, connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
<p>@reader["Name"] - @reader["Email"]</p>
}
}
}
}
}
3. Update Operation
The Update operation allows you to modify existing records in the database. Below is an example of how to update a user's information.
@{
var userId = Request.QueryString["id"];
if (IsPost)
{
var name = Request.Form["Name"];
var email = Request.Form["Email"];
using (var connection = new SqlConnection(connectionString))
{
var query = "UPDATE Users SET Name = @Name, Email = @Email WHERE ID = @ID";
using (var command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@Email", email);
command.Parameters.AddWithValue("@ID", userId);
connection.Open();
command.ExecuteNonQuery();
}
}
<p>User updated successfully!</p>
}
else
{
// Fetch existing user data for the form
var existingUser = new { Name = "", Email = "" };
using (var connection = new SqlConnection(connectionString))
{
var query = "SELECT * FROM Users WHERE ID = @ID";
using (var command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@ID", userId);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
existingUser .Name = reader["Name"].ToString();
existingUser .Email = reader["Email"].ToString();
}
}
}
}
}
}
<form method="post">
<input type="text" name="Name" value="@existingUser .Name" placeholder="Name" required />
<input type="email" name="Email" value="@existingUser .Email" placeholder="Email" required />
<button type="submit">Update User</button>
</form>
4. Delete Operation
The Delete operation allows you to remove records from the database. Below is an example of how to delete a user.
@{
var userId = Request.QueryString["id"];
if (IsPost)
{
using (var connection = new SqlConnection(connectionString))
{
var query = "DELETE FROM Users WHERE ID = @ID";
using (var command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@ID", userId);
connection.Open();
command.ExecuteNonQuery();
}
}
<p>User deleted successfully!</p>
}
}
<form method="post">
<p>Are you sure you want to delete this user?</p>
<button type="submit">Delete User</button>
</form>
Conclusion
Performing CRUD operations in ASP.NET Web Pages is straightforward and can be accomplished using ADO.NET. By following the examples above, you can create, read, update, and delete records in your database efficiently. This functionality is essential for building dynamic web applications that require user interaction with data.