ASP.NET Web Form is still used by a large number of developers for rapid application development. This tutorial is a CRUD (Create, Retrieve, Update, and Delete) Web Application with the "search for a record" functionality. The records are displayed in a ListView Control which involves model binding. Data Pager is used for pagination of the records.
The data-bound controls (such as the ListView, the Repeater, the GridView, the DetailsView, and the FormView) are Strongly-Typed Data-Bound Controls.
Note - Required Libraries are deleted from the bin folder of the uploaded project as the file size became bigger than upload limit 10 MB.
Software required for this tutorial is -
- Microsoft .NET Framework 4.5.1or any higher version.
- Microsoft Visual Studio 2013 or Visual Studio Express 2013 for Web or any higher version of Microsoft Visual Studio above 2012
- Entity Framework 6.0
Lunch Visual Studio and create a new ASP.NET project. Name it as List View Search.
Go to File >> New Project.
The project looks like below.
Default.aspx Page
Default.aspx.cs Page
Web.Config File
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- For more information on how to configure your ASP.NET application, please visit
- http:
- -->
- <configuration>
- <configSections>
- <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
- <!-- For more information on Entity Framework configuration, visit http:
- </configSections>
- <system.web>
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" />
- <pages>
- <namespaces>
- <add namespace="System.Web.Optimization" />
- </namespaces>
- <controls>
- <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
- </controls>
- </pages>
- </system.web>
- <runtime>
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <dependentAssembly>
- <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
- <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
- </dependentAssembly>
- </assemblyBinding>
- </runtime>
- <connectionStrings>
- <add name="TutorialEntities" connectionString="metadata=res://*/Models.EmployeeModel.csdl|res://*/Models.EmployeeModel.ssdl|res://*/Models.EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Tutorial.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
- </connectionStrings>
- <entityFramework>
- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
- <parameters>
- <parameter value="v11.0" />
- </parameters>
- </defaultConnectionFactory>
- <providers>
- <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
- </providers>
- </entityFramework>
- </configuration>
Creating Database File Tutorial.mdf
Right-click on the App_Data folder and choose Add >> New Item >> New SQL Server Database.
Name it as Tutorial.mdf.
Now, we will be creating a table in the Microsoft database file, Tutorial.mdf, and manually inserting a few dummy records.
The newly created database file Tutorial.mdf is displayed inside the App_Data folder.
Right-click on Tutorial.mdf file and click "Open".
Right-click on Tables >> Add New Table.
Name the table as Employee and add six columns.
SQL of the table is mentioned below.
- CREATE TABLE [dbo].[Employee] (
- [Id] INT NOT NULL,
- [FirstName] NVARCHAR (30) NULL,
- [LastName] NVARCHAR (25) NULL,
- [Salary] INT NULL,
- [ContactNo] NVARCHAR (30) NULL,
- [Date Of Birth] NVARCHAR(20) NULL,
- PRIMARY KEY CLUSTERED ([Id] ASC)
- );
Click the "Update" button in the left corner and update the database in newly opened window.
A new table, Employee, is created. Refresh the Table folder in the Server Explorer window. The Employee table is there.
To add a record to the Employee table, right-click on it and select "Show Table Data".
There is no record. So, let us add dummy records.
Here is the SQL for records to insert.
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (1, N'Stacy', N'Stewart', 5000, N'89765432987', N'1996-11-03')
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (2, N'Kevin', N'Sweetman', 6000, N'67987654387', N'1995-02-04')
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (3, N'Steve', N'Perry', 7500, N'76987432167', N'1993-02-05')
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (4, N'Mike', N'Ponting', 8300, N'87098654327', N'1995-02-01')
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (5, N'Rick', N'Rabjohn', 74000, N'54765432876', N'2000-05-06')
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (6, N'Sibu', N'Patra', 6500, N'879054321', N'2000-08-23')
- INSERT INTO [dbo].[Employee] ([Id], [FirstName], [LastName], [Salary], [ContactNo], [Date Of Birth]) VALUES (7, N'Sibu', N'Patra', 6500, N'879054321', N'2000-08-23')
Creating a Model to insert into Employee Database (Entity Framework Database First)
Create a new folder by right-clicking the project node >> Add >> NewFolde. Name it as "Models".
Right-click on the "Models" folder and from the opened menu list, click Add >> New Item >> ADO.NET Entity Data Model. Now, name it as EmpModel.edmex.
Click on "New Connection" >> browse, and select database file Tutorial.mdf.
Click "Test Connection".
Click Next >> Employee Table, as displayed below. Then, click "Finish".
Click OK. Again, click OK and then, select the "Yes To All" button.
The web application ‘Employee Manager’ is shown below.
Add a record to it.
Here is how it will be searching for the FirstName or LastName containing the letter ‘S’.
Edit the records like below.
Delete the Records like below.
That's It. We have successfully built the CRUD Application in ASP.NET. Suggestions for modification are welcome.