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

Lab 6 - Update and delete data from the table through ASP .NET Core

This document provides a tutorial on managing data in SQL Server using ASP .Net Core, specifically focusing on updating and deleting data in a flower database. It outlines the steps to create an update page, implement the EditData and UpdateData functions, and add functionality to delete records from the database. The tutorial concludes with a preview of the next topic, which will cover migrating tables to Amazon RDS.

Uploaded by

Mark
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)
4 views

Lab 6 - Update and delete data from the table through ASP .NET Core

This document provides a tutorial on managing data in SQL Server using ASP .Net Core, specifically focusing on updating and deleting data in a flower database. It outlines the steps to create an update page, implement the EditData and UpdateData functions, and add functionality to delete records from the database. The tutorial concludes with a preview of the next topic, which will cover migrating tables to Amazon RDS.

Uploaded by

Mark
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/ 11

CT071-3-3-DDAC Managing data in SQL Server with ASP .

Net Core

Lab 3 – Managing data in SQL Server with ASP .Net Core: Part 2
Estimated usage time: 1 Hour

Lab 3.3. Update data in SQL Server through ASP .Net Core.
This tutorial will teach the student how to update data in the SQL database.

a. Create a page for updating data in existing Identity DB


(Estimation of Total Time Used: 30 minutes)

1. Continued from the Lab 3.1 project.


2. Go to Solution Explorer > Go to Views Folder > Go to Flowers Folder > Double clicks on the
Index.cshtml File.

Level 3 Asia Pacific University of Technology & Innovation Page 1 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

3. Copy the function name from the button asp-action = “” (as shown in the red highlighted below).

4. Now, go to Solution Explorer > go to Controllers Folder > Double clicks on the
FlowersController.cs File.

Level 3 Asia Pacific University of Technology & Innovation Page 2 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

5. Add the function with name of “EditData()” in the FlowersController.cs.

//load an update page for the user


public async Task<IActionResult> EditData(int ? FlowerID)
{
if(FlowerID == null)
{
return NotFound();
}
var flower = await _context.FlowerTable.FindAsync(FlowerID);

if(flower == null)
{
return BadRequest(FlowerID + " is not found in the table!");
}
return View(flower);
}

6. Now, highlight the EditData() function and right click on the highlighted function. Select
Add View and generate a new page for this function.

Level 3 Asia Pacific University of Technology & Innovation Page 3 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

7. In the Add New Scaffolded Item dialog, select Razor View and press button Add.

8. In the Add Razor View dialog, directly create on the Add button.

9. The EditData.cshtml page will be added to a folder named Flowers.

Level 3 Asia Pacific University of Technology & Innovation Page 4 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

10. Now, in the EditData.cshtml, add the below sentence to display the current existing data
in the page.
@model MVC_APU_FlowerShop2023.Models.Flower

<center>
<h1>Edit Current Flower Information:</h1>
<br /><hr /><br />
<form method="post" asp-action="UpdateData" asp-controller="Flowers">
<table border='1'>
<tr>
<th><label asp-for="FlowerID"></label></th>
<td><input asp-for="FlowerID" readonly /></td>
</tr>
<tr>
<th><label asp-for="FlowerName"></label></th>
<td><input asp-for="FlowerName"/></td>
<td><span asp-validation-for="FlowerName"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerType"></label></th>
<td><input asp-for="FlowerType" /></td>
<td><span asp-validation-for="FlowerType"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerPrice"></label></th>
<td><input asp-for="FlowerPrice" /></td>
<td><span asp-validation-for="FlowerPrice"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerProducedDate"></label></th>
<td><input asp-for="FlowerProducedDate" /></td>
<td><span asp-validation-for="FlowerProducedDate"></span></td>
</tr>
<tr>
<td colspan="3" style="text-align:center">
<button type="submit" class="btn-info">Update Flower
Information</button>
</td>
</tr>
</table>
</form>
</center>

Level 3 Asia Pacific University of Technology & Innovation Page 5 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

Level 3 Asia Pacific University of Technology & Innovation Page 6 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

11. Now, click on the Start Without Debugging button, you will see the below page appear
after you clicked on the Edit button in the Index.cshtml page.

Level 3 Asia Pacific University of Technology & Innovation Page 7 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

12. To update the latest data in the Flower table, we need to write a function named
ProcessUpdateData in the controller file to process the update action.

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateData(Flower flower)
{
try
{
if (ModelState.IsValid)
{
_context.FlowerTable.Update(flower);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Flowers");
}
return View("EditData", flower);
}
catch(Exception ex)
{
return BadRequest("Error: "+ ex.Message);
}
}

Level 3 Asia Pacific University of Technology & Innovation Page 8 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

13. To test the function, we can change the current flower information and see if it changes in
the database.

Level 3 Asia Pacific University of Technology & Innovation Page 9 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

Lab 3.4. Delete Data from the tables of SQL Server with ASP .Net Core.
This tutorial will teach the student how to delete data from a table in the SQL database.

b. Delete Data from the Flower Table


(Estimation of Total Time Used: 30 minutes)

1. Now, go back to FlowersController.cs again.

2. Add a new DeleteData() function by following the below code.


//delete data from the page
public async Task<IActionResult> DeleteData(int ? FlowerID)
{
if(FlowerID ==null)
{
return NotFound();
}
var flower = await _context.FlowerTable.FindAsync(FlowerID);
if(flower == null)
{
return BadRequest(FlowerID + " is not found in the list!");
}
_context.FlowerTable.Remove(flower);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Flowers");
}

Level 3 Asia Pacific University of Technology & Innovation Page 10 of 11


CT071-3-3-DDAC Managing data in SQL Server with ASP .Net Core

3. Now, click on the Start Without Debugging button and restart the website again.
4. To test the function, we can click on the Delete button and see if the selected flower
information is deleted from the database.

Summary:
In this tutorial, we have learnt how to edit data in database through the existing ASP .Net Core
project. Besides, in the next tutorial, we have also learnt how to delete record from the table
through the existing ASP.NET Core project.

In the next tutorial, we will learn how to migrate the tables from local database to Amazon
Relational Database Services through Visual Studio.

Level 3 Asia Pacific University of Technology & Innovation Page 11 of 11

You might also like