- ASP.NET WP - Publish
- ASP.NET WP - Security
- ASP.NET WP - Caching
- Add Social Networking to the Website
- ASP.NET WP - Add Search
- ASP.NET WP - Add Email
- ASP.NET WP - Working with Videos
- ASP.NET WP - Working with Images
- ASP.NET WP - Working with Files
- ASP.NET WP - Charts
- ASP.NET WP - WebGrid
- ASP.NET WP - Delete Database Data
- ASP.NET WP - Edit Database Data
- ASP.NET WP - Add Data to Database
- ASP.NET WP - Database
- ASP.NET WP - Page Object Model
- ASP.NET WP - Working with Forms
- ASP.NET WP - Layouts
- ASP.NET WP - Programming Concepts
- ASP.NET WP - Global Pages
- Project Folder Structure
- ASP.NET WP - View Engines
- ASP.NET WP - Getting Started
- ASP.NET WP - Environment Setup
- ASP.NET WP - Overview
- ASP.NET WP - Home
ASP.NET WP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ASP.NET WP - Add Data to Database
In this chapter, we will be covering how to create a page that lets users add data to the Customers table in the database.
In this example, you will also understand when the record is inserted, then the page displays the updated table using the ListCustomers.cshtml page that we have created in the previous chapter.
In this page, we also add vapdation to make sure that the data which the user enters is vapd for the database. For example, user has entered data for all the required columns.
How to add Data to the Customer Table in the Database?
Let’s add a new CSHTML file to your website.
![Data in Table](/asp.net_wp/images/data_in_table.jpg)
Enter InsertCustomer.cshtml in the Name field and cpck OK.
Now create a new web page in which the user can insert data in the Customers table, so replace InsertCustomer.cshtml file with the following code.
@{ Vapdation.RequireField("FirstName", "First Name is required."); Vapdation.RequireField("LastName", "Last Name is required."); Vapdation.RequireField("Address", "Address is required."); var db = Database.Open("WebPagesCustomers"); var FirstName = Request.Form["FirstName"]; var LastName = Request.Form["LastName"]; var Address = Request.Form["Address"]; if (IsPost && Vapdation.IsVapd()) { // Define the insert query. The values to assign to the // columns in the Customers table are defined as parameters // with the VALUES keyword. if(ModelState.IsVapd) { var insertQuery = "INSERT INTO Customers (FirstName, LastName, Address) " + "VALUES (@0, @1, @2)"; db.Execute(insertQuery, FirstName, LastName, Address); // Display the page that psts products. Response.Redirect("~/ListCustomers"); } } } <!DOCTYPE html> <html> <head> <title>Add Customer</title> <style type = "text/css"> label { float:left; width: 8em; text-apgn: right; margin-right: 0.5em; } fieldset { padding: 1em; border: 1px sopd; width: 50em; } legend { padding: 2px 4px; border: 1px sopd; font-weight:bold; } .vapdation-summary-errors { font-weight:bold; color:red; font-size: 11pt; } </style> </head> <body> <h1>Add New Customer</h1> @Html.VapdationSummary("Errors with your submission:") <form method = "post" action = ""> <fieldset> <legend>Add Customer</legend> <span> <label>First Name:</label> <input name = "FirstName" type = "text" size = "50" value = "@FirstName"/> </span> <span> <label>Last Name:</label> <input name = "LastName" type = "text" size = "50" value = "@LastName" /> </span> <span> <label>Address:</label> <input name = "Address" type = "text" size = "50" value = "@Address" /> </span> <span> <label> </label> <input type = "submit" value = "Insert" class = "submit" /> </span> </fieldset> </form> </body> </html>
Now let’s run the apppcation and specify the following url − http://localhost:36905/InsertCustomer and you will see the following web page.
![Insert Customer](/asp.net_wp/images/insert_customer.jpg)
In the above screenshot, you can see that we have added vapdation, so you cpck the insert button without entering any data or miss any of the above mentioned field then you will see that it displays the error message as shown in the following screenshot.
![Error Message](/asp.net_wp/images/error_message.jpg)
Now let’s enter some data in all the fields.
![Enter Data](/asp.net_wp/images/enter_data.jpg)
Now cpck on Insert and you will see the updated pst of customers as shown in the following screenshot.
![Updated List](/asp.net_wp/images/updated_pst.jpg)