Problem: How do I perform CRUD operation in web?
Solution: I will explain in detail steps how to write a simple web application that is capable of crud operation in SQL server.
- From Visual Studio create a MVC web application. I used Visual Studio 2017.
- Add entity framework core, sql server compact packages from NuGet browser if you want
- Get a SQL lite database (mdf file) and add an employee table with columns, Id (int), name, address and phone number as varchar.
- Copy it to the data folder in the application folder
- Add a class named employee to the modal class with properties same as the column names in the employee table
- Add a controller class named employees
- Go to the appsettings.json file, rename your copied database file to the same name as the database name in the connection string
- Go to the layout page and add the newly created employees CRUD operation index page to the web layout
- Build the project
- Launch the website by pressing F5
- Check if you are able to go the employees index page from the website
- If you get SQL connection string error, then double click on the mdf file in the data folder of the solution browser. This will open the database in the SQL explorer of visual studio. Right click and open the properties of the database, copy the connection string and replace the connection string of the appsettings.json file.
- Check that the employees index page is opened from the website.
- Click on the create link in the index page. And enter the details and try to save it. In case insertion of employee to the employee table fails due to primary key missing error, add the field to get the primary key from the create page of the employee.
- Now everything should work ......
For step by step demo in Visual Studio 2017 visit
https://youtu.be/PWQL15kWPWE
https://youtu.be/PWQL15kWPWE
Comments
Post a Comment