Introduction
The FormView control is used to display a single record at a time from a data source. When you use the FormView control, you create templates to display and edit data-bound values. The templates contain controls, binding expressions, and formatting that define the look and functionality of the form. In this article, I will demonstrate how to dynamically bind FormView control in ASP.NET from the database.
Step 1 - Create database table in SQL server 2014
- CREATE TABLE [dbo].[StudentsResult](
- [RollNumber] [int] IDENTITY(10000,1) NOT NULL,
- [StudentName] [nvarchar](50) NULL,
- [Hindi] [int] NULL,
- [English] [int] NULL,
- [Physics] [int] NULL,
- [Chemistry] [int] NULL,
- [Biology] [int] NULL,
- [Mathematics] [int] NULL,
- [Total_Marks] [int] NULL,
- [Percentage] [float] NULL,
- CONSTRAINT [PK_StudentsResult] PRIMARY KEY CLUSTERED
- (
- [RollNumber] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
- CREATE PROCEDURE [dbo].[spGetStudentResult]
- AS
- BEGIN
- SELECT*FROM [dbo].[StudentsResult]
- END
Screenshot database table
Step 2
Open Visual Studio 2015 click on New Project and create an empty web application project.
Screenshot for creating new project 1
After clicking on New Project one window will appear select Web from left panel choose ASP.NET Web Application to give a meaningful name of your project then click on OK. As shown in below screenshot.
Screenshot for creating new project 2
After clicking on OK one more window will appear to choose Empty check on Web Forms checkbox and click on OK. As shown below screenshot.
Screenshot for creating new project 3
Step 3
Double click on webconfig file and database connections. Write the following line of code.
- <connectionStrings>
- <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>
- </connectionStrings>
Step 4 - Add web form in your project
Right click on project, select add, choose Web Form and click on it.
Screenshot adding web form 1
After clicking on the web form a window will appear. Assign a name to web form, such as FormView, or any meaningful name. Click OK, the web form will be added in the project.
Screenshot adding web form-2
Step 5
Add script and styles of bootstrap in the head section of the web form.