Really, I'll take a gander at including information first. To add another book to the database, we have to give a title, ISBN number, depiction and we have to include a writer and classification. The book will gain a BookId esteem consequently as a consequence of making the BookId field in the database an Identity segment. The initial 3 qualities require textboxes inside a structure:
<form action="" method="post">
<div class="row">
<span class="label"><label for="title">Title:</label></span>
<input type="text" name="title" id="title" size="50" />
</div>
<div class="row">
<span class="label"><label for="isbn">ISBN:</label></span>
<input type="text" name="isbn" id="isbn" size="20" />
</div>
<div class="row">
<span class="label"><label for="description">Description:</label></span>
<textarea cols="50" rows="8" name="description" id="description"></textarea>
</div>
Things are progressing pretty well. The creator and classification are not all that direct. A determination of writers and classifications as of now exist in the database in their own different tables. The main piece of tthe writer or classification record that is put away in the book record is the Primary Key quality for the writer or class, as a Foreign Key worth. This relates the suitable writer or classification record the book record. In this way, when adding a writer to a book, for instance, everything we need is the ID of the writer. Notwithstanding, introducing a rundown of ID qualities to the client is very little utilize. How are they going to know which Id fits in with which creator? The answer is to utilize a html <select> component, likewise referred to in ASP.NET circles as a dropdown list. A select component contains one or more <option> components.
Every choice has a quality ascribe (which is utilized to hold the ID), and it incorporates some content which characterizes the choices accessible to the client in an all the more cordial way. On account of creators, a mix of first name and last name will likely be the most supportive approach to do this. Thus, in the code zone at the highest point of the page, we have to characterize a Database item, and after that question it for the information for the select rundown:
var db = Database.Open("Books");
var authors = db.Query("SELECT AuthorId, FirstName + ' ' + LastName AS AuthorName FROM Authors");
While the SQL looks somewhat occupied, it just returns two qualities - the AuthorId for each one creator, and the first name and last name linked with a space between, and conveyed under the pseudonym "AuthorName". The thing to do next is to apply these to the select rundown
<div class="row">
<span class="label"><label for="authorId">Author:</label></span>
<select name="authorId" id="authorId">
<option value="">-- Select Author --</option>
@{
foreach(var author in authors){
<option value="@author.AuthorId">@author.AuthorName</option>
}
}
</select>
</div>
The primary alternative is the default one, and is given no quality whatsoever. After that, every alternative tag is given the ID having a place with the matching creator in the rundown. At the point when the structure is submitted, just the ID quality is posted back (which is all we are after). The same procedure is emulated for applying a select rundown of classifications:
<div class="row">
<span class="label"><label for="categoryId">Category:</label></span>
<select name="categoryId" id="categoryId">
<option value="">-- Select Category --</option>
@{
foreach(var category in categories){
<option value="@category.CategoryId">@category.Category</option>
}
}
</select>
</div>
Finally the form is finished off:
<div class="row">
<span class="label"> </span>
<input type="submit" name="action" id="action" value="Add" />
</div>
</form>
At the point when the structure is posted back, the qualities entered by the client are assembled and embedded into the database:
if (IsPost){
var sql = "INSERT INTO Books (Title, ISBN, Description, AuthorId, CategoryId) VALUES (@0, @1, @2, @3, @4)";
db.Execute(sql, Request["title"], Request["isbn"], Request["description"], Request["authorId"], Request["categoryId"]);
}
Notice the qualities @0, @1 and so forth in the SQL? They are parameter placeholders, and protet against SQL infusion, which is an endeavor to pass vindictive code to the database. At the point when the Database.Execute() strategy is called, the SQL is passed in initially, emulated by the wellspring of the parameter values. For this situation, the greater part of the qualities originate from the Request.Form gathering (despite the fact that the shorthand form Request[index] is utilized. Things are referenced by their list, which is the name of the structure field. The qualities are passed into the strategy in the same request that their parameters show up in the SQL.
Altering a current book requires a just about indistinguishable structure. Be that as it may, the structure needs to know which book is being altered. This point of interest is passed in the URL to the EditBook.cshtml page. Connections are made in the posting page (Default.cshtml):
foreach(var push in db.Query(sql, Request["CategoryID"])){
foreach(var row in db.Query(sql, Request["CategoryID"])){
<h2>@row.Title</h2>
<p><strong>Author:</strong> @row.FirstName @row.LastName<br />
<strong>ISBN:</strong> @row.ISBN <br/>
<strong>Description:</strong> @row.Description <br />
<strong>Category: </strong> @row.Category</p>
<a href="@Href("~/EditBook", row.BookId)">Edit</a>
}
The last line of code demonstrates the Href() assistant being utilized to connection to EditBook.cshtml utilizing steering, so the record postfix is not required, and the Id of the book is attached like this: EditBook/5, or EditBook/7 and so on. This worth is drawn from the URL utilizing the UrlData() assistant and utilized as a parameter for the SQL that gets the pointed out book's points of interest, alongside the information for the writers and classifications drop down records:
var db = Database.Open("Books");
var Id = UrlData[0].AsInt();
var sql = "SELECT Title, ISBN, Description, AuthorId, CategoryId FROM Books WHERE BookId = @0";
var book = db.QuerySingle(sql, Id);
var categories = db.Query("SELECT CategoryId, Category FROM Categories");
var authors = db.Query("SELECT AuthorId, FirstName + ' ' + LastName AS AuthorName FROM Authors");
The structure itself presents the book to be altered:
<form action="" method="post">
<div class="row">
<span class="label"><label for="title">Title:</label></span>
<input type="text" name="title" id="title" value="@book.Title" size="50" />
</div>
<div class="row">
<span class="label"><label for="isbn">ISBN:</label></span>
<input type="text" name="isbn" id="isbn" value="@book.ISBN" size="20" />
</div>
<div class="row">
<span class="label"><label for="description">Description:</label></span>
<textarea cols="50" rows="8" name="description" id="description">@book.Description</textarea>
</div>
<div class="row">
<span class="label"><label for="authorId">Author:</label></span>
<select name="authorId" id="authorId">
@{
foreach(var author in authors){
if(author.AuthorId == book.AuthorId){
<option value="@author.AuthorId" selected="selected">@author.AuthorName</option>
} else {
<option value="@author.AuthorId">@author.AuthorName</option>
}
}
}
</select>
</div>
<div class="row">
<span class="label"><label for="categoryId">Category:</label></span>
<select name="categoryId" id="categoryId">
@{
foreach(var category in categories){
if(category.CategoryId == book.CategoryId){
<option value="@category.CategoryId" selected="selected">@category.Category</option>
} else {
<option value="@category.CategoryId">@category.Category</option>
}
}
}
</select>
</div>
<div class="row">
<span class="label"> </span>
<input type="submit" name="action" id="action" value="Edit" />
</div>
</form>
Inside the code for both the writer and classification dropdowns, the Id of the book to be altered is contrasted and every Id of the writer or class, and if there is a match, selected="selected" is added as a credit to the alternative. When WebMatrix is discharged completely, there may be aides for drop down records, yet this is more or less great Beta 1. Presently we have a structure that gets the chose book and presentations it for altering, yet nothing happens if the client clicks submit. The code square needs to be changed at the highest point of the record to overhaul the chose book with the new values, and after that redisplay it, so it now resembles this:
var db = Database.Open("Books");
var Id = UrlData[0].AsInt();
var sql = "";
if (IsPost)
{
sql = "UPDATE Books SET Title = @0, ISBN = @1, Description = @2, AuthorId = @3, CategoryId = @4 WHERE BookId = @5";
db.Execute(sql, Request["title"], Request["isbn"], Request["description"], Request["authorId"], Request["categoryId"], Id);
}
sql = "SELECT Title, ISBN, Description, AuthorId, CategoryId FROM Books WHERE BookId = @0";
var book = db.QuerySingle(sql, Id);
var categories = db.Query("SELECT CategoryId, Category FROM Categories");
var authors = db.Query("SELECT AuthorId, FirstName + ' ' + LastName AS AuthorName FROM Authors");
Another code test has been connected if the page IsPost(), and in that, the qualities are gotten from the Request accumulation and passed into the SQL to upgrade the database. At that point the code proceeds as in the recent past, recovering the chose book for redisplay with the redesigns.
Best ASP.NET Hosting Recommendation
ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.