• How to Retrieve the Data Saved in SQL Database in ASP.NET MVC

    by  • January 12, 2016 • Microsoft ASP.NET MVC • 0 Comments

    In the previous article we have saved the data submitted by the customer in a SQL database. In this article, we will retrieve the data that is saved in SQL Database. Now the customer has placed the order of five books that we has saved in our SQL Database as shown in the figure below.

    Database

    Now we want to display these book orders  inWe create a new controller named as ‘BooksController’. In this controller we add Index action method that will return a View. Lets look at the BooksController.cs code file given below

    BookController.cs

    In the above code,  we just create an instance of our database by the code line private store db = new store(). Then we are creating a list of books that are stored in the database and storing it in a books variable. The Index action method return a View Index. So we create book Index View, the code is given below

    Index.cshtml

    In the above code at the top first line, we are using our Book model with IEnumerable interface. The IEnumerable interface gives us the collection of books. After this, we form a Html table and use Html Helper Methods @HTML.Displayfor() which is just like a title in Html but the helper method just render the display template and match the property of model. @HTML.DisplayNamefor() is used to display the name defined in your model. We also use foreach loop to iterate through the set of data. Actually IEnumerable interface facilitate the use of foreach loop to iterate through items.

    Now We will get the following given output when we run the application.

    Output

     

    So in this way, we will retrieve the data saved in our SQL database.




    Namecheap.com

    Leave a Reply