Save Form Data to Database using Model in ASP.NET CORE

In this chapter you will learn

  1. How to create Database in ASP.NET Core?
  2. Store Connection String in appsettings.json file
  3. How to retrieve connection string from appsettings.json file?
  4. Make model class to save data to database

Till now, you have learned how to make model class in asp.net core and how to connect it with controller. In this chapter you will learn whole package including creating model and saving data to database. This chapter is going to be quite interesting because once you complete this chapter you will be able to apply your own logic to database and project. So, study this chapter thoroughly to understand basic concept of working with database in ASP.NET Core.


Here, I am creating a New Project UserProfile, that will ask user details and save into database.


Step 1. Create New ASP.NET CORE project UserProfile with No Authentication. If you have problem in creating new project you can see this link.
Create Your First ASP.NET Core Project

Design View Page

Step 2. Add a View Page Profile.cshtml under Home folder and add the following code.

Create Database and Table

Step 3. Create UserProfile Database to save these details. Go to View Server Explorer. Right Click on Data Connections and select Create New SQL Server Database.

 

Step 4. Enter .\SQLEXPRESS as Server Name and UserProfile as Database Name as picture below.

Step 5. Expand UserProfile Database and then right click on Tables Add New Table. Design your table as picture below or paste the following script to design your table automatically. Then click on Update button to save your table. If you are designing your table yourself then don’t forget to set Identity Specification = True for Id column. You can set it in properties windows for Id column.

Step 6. Refresh your Server Explorer to see your Profile table under your database.
  • Get Connection String

    Step 7. Go to Server Explorer, Right click on your database and select Properties. Here you can see Connection String.

     



    Data Source=.\SQLEXPRESS;Initial Catalog=UserProfile;Integrated Security=True;Pooling=False


     

    Step 8. Copy Connection Strings and Open appsettings.json file. Add your Connection Strings using following method.

     

    Now, I will make a class in models folder that will access this connection strings when needed.

    Creating Models Folder and Class

    Step 9. Create a Models folder and add a class file GetConString.cs in it and paste the following code. If you have difficulties in creating Models folder then you can see this link.

    Creating Models Folder and Adding a Model class

    This class file will return Connection String that will be used to connecting with your database.

    Installing Microsoft.EntityFrameworkCore.SqlServer is Necessary

    You must install Microsoft.EntityFrameworkCore.SqlServer to work with Sql Data Client. It is very easy to install. Go to Tools NuGet Package Manager Package Manager Console. Here, type the following command and Hit Enter. Package will be automatically installed and restored on your project within couple of seconds.

    Install-Package Microsoft.EntityFrameworkCore.SqlServer

    Step 10. Create one more class file UserDataModel.cs in models that will handle input/output and saving details on database. Open UserDataModel.cs and paste the following code in it.

    Step 11. Now open HomeController.cs and add the following highlighted code.


    Step 12. Linking Profile Page to Home Page. Open Shared _Layout.cshtml and find the following block of codes and add the highlighted line as below:

     

     

    Step 13. It’s time to execute your program. Press Ctrl + F5 or F5 to launch the website. Click on Profile Page Link to open your Profile Page. Now Enter some value in the field and press Submit button.

    Step 14. Refresh your database in server explorer then right click on Profile table and choose Show Table Data. You will see here that data has been saved here successfully.

    Summary

    This chapter explains complete guide of MVC. Here, you learned how to design a form in View, How to write code in controller, how to create model class file and save data to database. It is just a demo and you will learn more in next chapter.


    6 thoughts on “Save Form Data to Database using Model in ASP.NET CORE”

    1. These are great easily to follow lessons that build on each nicely.
      However I encountered error at the end of the UserProfile session, Everything worked fine until press the submit button to save the data, (name, age, city).
      Full page of error code, this seemed significate:
      Microsoft.AspNetCore.Server.Kestrel.Http.MessageBody.ForContentLength.ReadAsyncImplementation(ArraySegment`1 buffer, CancellationToken cancellationToken)
      I was able earlier to create the database and table and enter data into it.
      Any suggestions on where to start looking for problem?

      1. Hi. I tried this tutorial again on a new install of Visual Studio and SQL Express and it all worked fine. Looking forward to the rest of the lessons in this series.

    Leave a Reply

    Your email address will not be published. Required fields are marked *