Introduction
We've done the Form Creation in previous articles. So here this article explains to create a form with which we can insert more records to the database when using ASP.NET Web Pages (Razor).
In that context, we'll learn to create a form, add the data, validate the data and link to another page and we'll also use the code to execute various methods.
So, let's proceed with the following sections:
- Creating Forms
- Insert Data into Database
- Working with Validations
- Page Linking
Creating Forms
Step 1: Now at first create a CSHTML named CreateCricketer.
Step 2: Replace your code with the following code:
- @{
- }
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <title>Create Cricketer</title>
- </head>
- <body>
- <h1>Create New</h1>
- <form method="post">
- <fieldset>
- <legend>Cricketer Information</legend>
- <table>
- <tr>
- <td style="width: 100px"><label for="Name">Name:</label></td>
- <td><input type="text" name="Name" value="@Request.Form["Name"]" /></td>
- </tr>
- <tr>
- <td><label for="Team">Team:</label></td>
- <td><input type="text" name="Team" value="@Request.Form["Team"]" /></td>
- </tr>
- <tr>
- <td><label for="Grade">Grade:</label></td>
- <td><input type="text" name="Grade" value="@Request.Form["Grade"]" /></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="BtnSubmit" value="Save" /></td>
- </tr>
- </table>
- </fieldset>
- </form>
- </body>
- </html>
In the code above, we've created a form element with a post method. There are various elements used here to insert the records into the database. Each element has the name property to get the user's input and pass it.
Step 3: Launch the page into the website.
Step 4: It's time to get the form values. Now in the code block add the following code:
- @{
- var Cric_Name="";
- var Cric_Team="";
- var Cric_Grade="";
- if(IsPost){
- Cric_Name= Request.Form["Name"];
- Cric_Team= Request.Form["Team"];
- Cric_Grade= Request.Form["Grade"];
- }
- }
In the code above the variables are declared to store the values from the text boxes. The If(IsPost) method is used to ensure that the variables are set only when the user clicks on the "Save" button.
Insert Data into Database
Now we have the form to submit the records to the database. We have only to provide code to do this job. So, let's proceed with the following procedure.
Step 1: Modify the If() method like below:
- if(IsPost){
- Cric_Name= Request.Form["Name"];
- Cric_Team= Request.Form["Team"];
- Cric_Grade= Request.Form["Grade"];
- var Cric_Db= Database.Open("Cricketer Site");
- var CommandText = "Insert into Cricketers(Name,Team,Grade) values(@0,@1,@2)";
- Cric_Db.Execute(CommandText, Cric_Name, Cric_Team, Cric_Grade);
- Response.Redirect("~/Cricketers");
- }
In the code above, we need to provide the Insert query to insert the record into the database. At first we open the database and next we use the Insert Command to insert the record. At last the Response method is used to view the inserted record.
Note: It is important that you use the placeholders in the SQL Statement otherwise if you concatenate user input into a SQL Statement you open yourself to a SQL injection attack.
Step 2: Now run the page and insert the record.
When you save your new record, you can search for your record in the table as shown below:
Working with Validations
We do not provide the validations yet. Use the following procedure to check it.
Step 1: Add another record and enter the name and leave the other entries.
Step 2: Your record is saved because you didn't provide any validations.
Now we need to provide the validation in the forms to validate the form using the Validation helper. The validation helper validates the field if the entered record does not meet the requirements.
Step 3: Now modify your code like below:
- @{
- Validation.RequireField("Name", "Cannot be bank!!");
- Validation.RequireField("Team", "Cannot be bank!!");
- Validation.RequireField("Grade", "Cannot be bank!!");
- var Cric_Name="";
- var Cric_Team="";
- var Cric_Grade="";
- if(IsPost && Validation.IsValid()){
- Cric_Name= Request.Form["Name"];
- Cric_Team= Request.Form["Team"];
- Cric_Grade= Request.Form["Grade"];
- var Cric_Db= Database.Open("Cricketer Site");
- var CommandText = "Insert into Cricketers(Name,Team,Grade) values(@0,@1,@2)";
- Cric_Db.Execute(CommandText, Cric_Name, Cric_Team, Cric_Grade);
- Response.Redirect("~/Cricketers");
- }
- }
Show Validation Message
So far we've applied the validation and now we need to show the validation message if the user has entered an invalid record. Use the following procedure to do that.
Step 1: Modify your code like below:
- <h1>Create New</h1>
- @Html.ValidationSummary()
- <form method="post">
- <fieldset>
- <legend>Cricketer Information</legend>
- <table>
- <tr>
- <td style="width: 100px"><label for="Name">Name:</label></td>
- <td><input type="text" name="Name" value="@Request.Form["Name"]" /></td>
- <td>@Html.ValidationMessage("Name")</td>
- </tr>
- <tr>
- <td><label for="Team">Team:</label></td>
- <td><input type="text" name="Team" value="@Request.Form["Team"]" /></td>
- <td>@Html.ValidationMessage("Team")</td>
- </tr>
- <tr>
- <td><label for="Grade">Grade:</label></td>
- <td><input type="text" name="Grade" value="@Request.Form["Grade"]" /></td>
- <td>@Html.ValidationMessage("Grade")</td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="BtnSubmit" value="Save" /></td>
- </tr>
- </table>
- </fieldset>
- </form>
Step 2: Now run the page and enter the record as shown below:
You can see that the validation message is displayed after leaving a texbox.
Page Linking
Now we apply our last step in which we add a "Create New" link in our main page to redirect to the "Create Cricketer" page.
To do this open the Cricketers.cshtml page and add the following code before the body
- <p>
- <a href="~/CreateCricketer">Create New</a>
- </p>
- </body>
Summary
In this article we created a form for creating some input elements in the form to insert the record into the database. We can also validate the form by the validation helper in the ASP.NET Web Pages 2. In the next part we'll update our database. Thanks for reading.