Insert, Delete, Update in GridView in ASP.Net using C#
SP.NET GridView is a very common and useful control. Here, I’m explaining how to work with GridView control in ASP.NET, like how to insert, delete and update record in GridView control. Follow bellow steps to know how to work with GridView in ASP.NET.
Step 1:- Create Database in SqlServer and create a table within this database. For demonstration here I have created database named “Demo” and table named “GridView”
Note: Here “id” column is auto increment and primary key.
Step 2:- Add connection string in your application’s web.config file and change name and connectionString according your SQL Server configuration as following.
Step 3:- Drag & drop GridView control from Toolbox on “.aspx” page and write down the following line of code within <asp:GridView> section or simple copy the following line of code and put where you want to display GridVIew.
Step 4:- Create one class within App_Code folder. Here I have given this class named is GlobalClass and write following line of code.
Step 5:- Create one folder, named Images, where we have stored employees photos see in following image.
Step 6:- Write down following line of code in “.cs” page which related to your .aspx page (e.g. if your .aspx page name is default.aspx then your .csfile is default.aspx.cs).
Step 7:- Now, save and build you application and execute. Your application output display as following.
It’s your application first screen. Because there is not data in table so it displace message “No record available”.
Step 8:- For inserting data, click on button “Add New Record”. Now fill the data. Every controls have validation for prohibited wrong input.
After filling data, your GridView looking as following. Here I have inserted three records.
Note: - Id are generated automatic for every new record.
Step 9:- For updating records click button “Edit” and change TextBox Data and you can select new photo of employee. For example in place of age“28”, updated with “30” and in place of salary “50000”, updated with “55000”, then click button “Update”. Through button “Cancel” you return previous stage.
Step 10:- For record deletion, click on button “Delete”. Before deleting record, confirmation alert message are popup. Here I have deleted 3rdrecord (e.g. Andrew Deniel). After deleting record your GridView looks as following.