In this article I will explain how to insert new records to SQL Server database using ASP.Net GridView’s Empty Data Template.
The below screenshot displays the structure of the database table that will store the customer records
Below is the HTML Markup of the ASP.Net GridView control
Above you will notice that the ASP.Net GridView is displaying 3 columns, Customer Name, Company Name and City. I have added textboxes and button to add new records in the <FooterTemplate> and <EmptyDataTemplate>.
Binding the GridView
Below is the code to bind the data from the SQL Server database to the ASP.Net GridView control
The below screenshot displays GridView when there is no data in the database, you will notice that it is displaying the<EmptyDataTemplate> with three textboxes and a button.
Add new record to database using GridView
Below is the code that gets called when the Add button is clicked. This code snippet fires an insert query in the database and inserts the record in the SQL Server database table
Above after the record is inserted in the database, the page is redirected to itself so that GridView is loaded with the newly inserted record. The below screenshot displays the GridView with the newly added record and now it is displaying the <FooterTemplate> with three textboxes and add button.
You can download the sample source code in VB.Net and C# along with the Database script using the download link provided belowAdd Records to ASP.Net GridView using Empty Data Template