ASP. NET Tutorial 8 Create a Login websiteCreating Master Page and apply it to existing aspx pages Duration 1043. ProgrammingKnowledge 186,823 views. CRUD Operations In ASP. NET MVC 5 Using ADO. NET Background. After awesome response of an published by me in the year 2. Insert, Update, Delete In Grid. View Using ASP. Net C. It now has more than 1. K views, therefore to help beginners I decided to rewrite the article i with stepbystep approach using ASP. NET MVC, since it is a hot topic in the market today. I have written this article focusing on beginners so they can understand the basics of MVC. Please read my previous article using the following links to understand the basics about MVC Step 1 Create an MVC Application. Now let us start with a stepbystep approach from the creation of simple MVC application as in the following Start, then All Programs and select Microsoft Visual Studio 2. File, then New and click Project. then select ASP. NET Web Application Template, then provide the Project a name as you wish and click on OK. Learn Step by Step or going for. NET interviews, start from here. Learn, Share, Build. Each month, over 50 million developers come to Stack Overflow to learn, share their knowledge, and build their careers. Join the worlds.
After clicking, the following window will appear As shown in the preceding screenshot, click on Empty template and check MVC option, then click OK. This will create an empty MVC web application whose Solution Explorer will look like the following Step 2 Create Model Class. Now let us create the model class named Emp. Model. cs by right clicking on model folder as in the following screenshot Note It is not mandatory that Model class should be in Model folder, it is just for better readability you can create this class anywhere in the solution explorer. This can be done by creating different folder name or without folder name or in a separate class library. Emp. Model. cs class code snippet public class Emp. Model DisplayName Id public int Empid get set RequiredError. Message First name is required. public string Name get set RequiredError. Message City is required. public string City get set RequiredError. Message Address is required. public string Address get set In the above model class we have added some validation on properties with the help of Data. Annotations. Step 3 Create Controller. Now let us add the MVC 5 controller as in the following screenshot After clicking on Add button it will show the following window. Now specify the Controller name as Employee with suffix Controller as in the following screenshot Note The controller name must be having suffix as Controller after specifying the name of controller. After clicking on Add button controller is created with by default code that support CRUD operations and later on we can configure it as per our requirements. Step 4 Create Table and Stored procedures. Now before creating the views let us create the table name Employee in database according to our model fields to store the details I hope you have created the same table structure as shown above. Now create the stored procedures to insert, update, view and delete the details as in the following code snippet To Insert Records Create procedure dbo. Add. New. Emp. Details Name varchar 5. City varchar 5. 0, Address varchar 5. Insert into Employee valuesName,City,Address End To View Added Records. Create Procedure dbo. Get. Employees as begin select from Employee End To Update Records. Create procedure dbo. Update. Emp. Details Emp. Id int, Name varchar 5. City varchar 5. 0, Address varchar 5. Update Employee set NameName, CityCity, AddressAddress where IdEmp. Id End To Delete Records Create procedure dbo. Delete. Emp. By. Id Emp. Id int as begin Delete from Employee where IdEmp. Id End Step 5 Create Repository class. Now create Repository folder and Add Emp. Repository. cs class for database related operations, after adding the solution explorer will look like the following screenshot. Now create methods in Emp. Repository. cs to handle the CRUD operation as in the following screenshot Emp. Repository. cspublic class Emp. Repository private Sql. Connection con private void connection string constr Configuration. Manager. Connection. Stringsgetconn. To. String con new Sql. Connectionconstr public bool Add. EmployeeEmp. Model obj connection Sql. Command com new Sql. CommandAdd. New. Emp. Details, con com. Command. Type Command. Type. Stored. Procedure com. Parameters. Add. With. ValueName, obj. Name com. Parameters. Add. With. ValueCity, obj. City com. Parameters. Add. With. ValueAddress, obj. Address con. Open int i com. Execute. Non. Query con. Close if i 1 return true else return false public Listlt Emp. Model Get. All. Employees connection Listlt Emp. Model Emp. List new Listlt Emp. Model Sql. Command com new Sql. CommandGet. Employees, con com. Command. Type Command. Type. Stored. Procedure Sql. Data. Adapter da new Sql. Data. Adaptercom Data. Table dt new Data. Table con. Open da. Filldt con. Close foreach Data. Row dr in dt. Rows Emp. List. Add new Emp. Model Empid Convert. To. Int. 32drId, Name Convert. To. String drName, City Convert. To. String drCity, Address Convert. To. StringdrAddress return Emp. List public bool Update. EmployeeEmp. Model obj connection Sql. Command com new Sql. CommandUpdate. Emp. Details, con com. Command. Type Command. Type. Stored. Procedure com. Parameters. Add. With. ValueEmp. Id, obj. Empid com. Parameters. Add. With. ValueName, obj. Name com. Parameters. Add. With. ValueCity, obj. City com. Parameters. Add. With. ValueAddress, obj. Address con. Open int i com. Execute. Non. Query con. Close if i 1 return true else return false public bool Delete. Employeeint Id connection Sql. Command com new Sql. CommandDelete. Emp. By. Id, con com. Command. Type Command. Type. Stored. Procedure com. Parameters. Add. With. ValueEmp. Id, Id con. Open int i com. Execute. Non. Query con. Close if i 1 return true else return false Step 6 Create Methods into the Employee. Controller. cs file. Now open the Employee. Controller. cs and create the following action methods public class Employee. Controller Controller public Action. Result Get. All. Emp. Details Emp. Repository Emp. Repo new Emp. Repository Model. State. Clear return ViewEmp. Repo. Get. All. Employees public Action. Result Add. Employee return View Http. Post public Action. Result Add. EmployeeEmp. Model Emp try if Model. State. Is. Valid Emp. Repository Emp. Repo new Emp. Repository if Emp. Repo. Add. EmployeeEmp View. Getting Started with ASP. NET MVC 5by Rick Anderson. Note. An updated version of this tutorial is available here using the latest version of Visual Studio. The new tutorial uses ASP. NET Core MVC, which provides many improvements over this tutorial. This tutorial will teach you the basics of building an ASP. NET MVC 5 web app using Visual Studio 2. This tutorial was written by Scott Guthrie twitterscottgu, Scott Hanselman twitter shanselman, and Rick Anderson Rick. And. MSFT You need an Azure account to deploy this app to Azure You can open an Azure account for free You get credits you can use to try out paid Azure services, and even after theyre used up you can keep the account and use free Azure services. You can activate MSDN subscriber benefits Your MSDN subscription gives you credits every month that you can use for paid Azure services. Getting Started. Start by installing and running Visual Studio Express 2. Web or Visual Studio 2. Visual Studio is an IDE, or integrated development environment. Just like you use Microsoft Word to write documents, youll use an IDE to create applications. In Visual Studio theres a toolbar along the top showing various options available to you. Theres also a menu that provides another way to perform tasks in the IDE. For example, instead of selecting New Project from the Start page, you can use the menu and select File New Project. Creating Your First Application. Click New Project, then select Visual C on the left, then Web and then select ASP. NET Web Application. Name your project Mvc. Movie and then click OK. In the New ASP. NET Project dialog, click MVC and then click OK. Visual Studio used a default template for the ASP. NET MVC project you just created, so you have a working application right now without doing anythingThis is a simple Hello World project, and its a good place to start your application. Click F5 to start debugging. F5 causes Visual Studio to start IIS Express and run your web app. Visual Studio then launches a browser and opens the applications home page. Notice that the address bar of the browser says localhost port and not something like example. Thats because localhost always points to your own local computer, which in this case is running the application you just built. When Visual Studio runs a web project, a random port is used for the web server. In the image below, the port number is 1. When you run the application, youll see a different port number. Right out of the box this default template gives you Home, Contact and About pages. The image above doesnt show the Home, About and Contact links. Depending on the size of your browser window, you might need to click the navigation icon to see these links. The application also provides support to register and log in. The next step is to change how this application works and learn a little bit about ASP. NET MVC. Close the ASP. NET MVC application and lets change some code. For a list of current tutorials, see MVC recommended articles. See this App Running on Azure. Would you like to see the finished site running as a live web app You can deploy a complete version of the app to your Azure account by simply clicking the following button. You need an Azure account to deploy this solution to Azure. If you do not already have an account, you have the following options Next.