Razor - Save Form to Database

In this chapter you will learn
  • How to save form data to database?
  • How to fill form with database?
  • How to insert, update and delete using Form?

How to save Form Data to Database?

In the previous chapter you learned how to get user input and validate form in razor. In this chapter you will learn how to save those values to database. We have already created a database in previous chapter Create and Connect to a Database. We will use this database in this chapter.
 

How to Save Form Data to Database?

You can save form data to database as follow:

@{
    var sqldb = Database.Open("Database Tutorial");    
}
 
<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
 @{
      //Validating Input
      Validation.RequireField("txtBook","Book Name is Required.");
      Validation.RequireField("txtAuthor", "AuthorName is Required.");
      Validation.Add("txtAuthor", Validator.StringLength(40,5,"Author Name must be Minimum 5 chars long"));
    
    if(IsPost)
    {
        //Getting TextBox value in string
        string BookName=Request.Form["txtBook"];
        string AuthorName=Request.Form["txtAuthor"];
 
        if(Validation.IsValid())
        {
            //Insert a Row
            if(Request.Form["btnInsert"] != null)
            {
                var sqlinsert = "INSERT INTO Library (BookName,Author) Values(@0,@1)";           
                sqldb.Execute(sqlinsert,BookName,AuthorName);
                <div style="padding:20px; background-color:aqua">@BookName - @AuthorName is Saved in Database</div><br /><br />
            }           
           else if(Request.Form["btnUpdate"] != null)
           {
           //Update a Row
           var sqlupdate = "UPDATE Library SET BookName=@0 where Author=@1";
           sqldb.Execute(sqlupdate,BookName,AuthorName);
           <div style="padding:20px; background-color:aqua">@BookName - @AuthorName is Updated in Database</div><br /><br />
           }
           else if(Request.Form["btnDelete"] != null)
           {
           //Delete a Row
           var sqldelete = "DELETE FROM Library where BookName=@0";
           sqldb.Execute(sqldelete,BookName);
           <div style="padding:20px; background-color:aqua">@BookName is Deleted</div><br /><br />
           }
        }
    }
 }
 
        <form method="post">        
            <table>            
            <tr>
            <td><label for="lblBook">Book Name : </label></td>
            <td><input type="text" name="txtBook" value="@Request.Form["txtBook"]" /></td>
            </tr>
            <tr><td colspan="2">@Html.ValidationMessage("txtBook")</td></tr>
            
            <tr>
            <td><label for="lblAuthor">Author Name : </label></td>
            <td><input type="text" name="txtAuthor" value="@Request.Form["txtAuthor"]" /></td>
            <tr><td colspan="2">@Html.ValidationMessage("txtAuthor")</td></tr>
            </tr>
                
             <tr>
             <td colspan="2">
             <input type="submit" name="btnInsert" value="Insert" />
             <input type="submit" name="btnUpdate" value="Update" />
             <input type="submit" name="btnDelete" value="Delete" />
             </td>
             </tr>  
            </table>
        </form>
    </body>
</html>

Output
RazorBasic - E. BalaKrishna is Saved in Database
Book Name :
Author Name :

Summary

In this chapter you learned how to get user input using form, validate them and save to database. You also learned to update and delete data using HTML form. This is very basic knowledge of working with database and you will be experienced more when you will start working on real time project.
 
 

Share your thought