Stored Procedure
Firstly, create a Stored Procedure for performing CRUD operation on database from ASP.NET page. In the following Stored procedure I have mentioned the entire query in one stored procedure like Select, Add, Update and Delete. All the operation is based on Event or status which will send from ASP.NET page.
Stored Procedure is based on Table "Employee".
| Column Name | DataType |
| Id | int |
| FirstName | varchar(50) |
| LastName | varchar(50) |
| PhoneNumber | nvarchar(15) |
| EmailAddress | nvarchar(50) |
| Salary | decimal(18, 2) |
Below find the stored procedure for above table. I have used if-else condition to perform all operations in single stored procedure. Based on Event which is sent by ASP.NET page the query will be executed.
- CREATE PROC [dbo].[usp_GridViewExample]
- (
- @EmpId int=0,@FirstName varchar(50)=Null,@LastName varchar(50)=Null,@PhoneNumber nvarchar(15)=Null,
- @EmailAddress nvarchar(50)=Null,@Salary decimal=Null,@Event varchar(10)
- )
- AS
- BEGIN
- IF(@Event='Select')
- BEGIN
- SELECT * FROM Employee ORDER BY FirstName ASC;
- END
- ELSE IF(@Event='Add')
- BEGIN
- INSERT INTO Employee (FirstName,LastName,PhoneNumber,EmailAddress,Salary,CreatedDate) VALUES(@FirstName,@LastName,@PhoneNumber,@EmailAddress,@Salary,GETDATE());
- END
- ELSE IF(@Event='Update')
- BEGIN
- UPDATE Employee SET FirstName=@FirstName,LastName=@LastName,PhoneNumber=@PhoneNumber,EmailAddress=@EmailAddress,Salary=@Salary where Id=@EmpId;
- END
- ELSE
- BEGIN
- DELETE FROM Employee WHERE Id=@EmpId;
- END
- END
This is a UI of application. In this I have created Add Employee Panel where we can add new employee and below this I have added a gridview where we can do Edit and Delete as well as you can see all the records,
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="GridViewDemo.GridViewDemo" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style>
- td {
- align=center;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div align="center">
- <div style="border: 1px Solid #0094ff; width: 70%;">
- <table>
- <tr>
- <td colspan="3" align="center">
- <h2>Employee Management System</h2> </td>
- </tr>
- <tr>
- <td>First Name </td>
- <td>
- <asp:TextBox runat="server" ID="txtFirstName" ValidationGroup="add"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator runat="server" ID="reqFirstName" ControlToValidate="txtFirstName" ForeColor="Red" ErrorMessage="First Name Requierd" EnableClientScript="true" ValidationGroup="add"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>Last Name </td>
- <td>
- <asp:TextBox runat="server" ID="txtLastName" ValidationGroup="add"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator runat="server" ID="reqLastName" ControlToValidate="txtLastName" ForeColor="Red" ErrorMessage="Last Name Required" EnableClientScript="true" ValidationGroup="add"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>Phone Number </td>
- <td>
- <asp:TextBox runat="server" ID="txtPhoneNumber"></asp:TextBox>
- </td>
- <td></td>
- </tr>
- <tr>
- <td>Emial Address </td>
- <td>
- <asp:TextBox runat="server" ID="txtEmailAddress" ValidationGroup="add"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator runat="server" ID="reqEmailAddress" ControlToValidate="txtEmailAddress" ForeColor="Red" ErrorMessage="Email Address Required" EnableClientScript="true" ValidationGroup="add"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>Salary </td>
- <td>
- <asp:TextBox runat="server" ID="txtSalary" ValidationGroup="add"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator runat="server" ID="reqSalary" ControlToValidate="txtSalary" ForeColor="Red" ErrorMessage="Salary Required" EnableClientScript="true" ValidationGroup="add"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td colspan="3" align="center">
- <asp:Button runat="server" ID="btnAddEmployee" Text="Add" OnClick="btnAddEmployee_Click" Style="height: 26px" Width="37px" ValidationGroup="add" /> </td>
- </tr>
- <tr>
- <td colspan="3" align="center">
- <br />
- <asp:Label runat="server" ID="lblMessage"></asp:Label>
- <br />
- <br /> </td>
- </tr>
- <tr>
- <td colspan="3">
- <asp:GridView ID="grvEmployee" runat="server" AllowPaging="true" AutoGenerateColumns="false" Width="900px" HeaderStyle-ForeColor="blue" OnPageIndexChanging="grvEmployee_PageIndexChanging" OnRowCancelingEdit="grvEmployee_RowCancelingEdit" OnRowDeleting="grvEmployee_RowDeleting" OnRowEditing="grvEmployee_RowEditing" OnRowUpdating="grvEmployee_RowUpdating">
- <Columns>
- <asp:TemplateField HeaderText="EmpId">
- <ItemTemplate>
- <asp:Label runat="server" ID="lblEmpId" Text='<%#Eval("id") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="FirstName">
- <ItemTemplate>
- <asp:Label runat="server" ID="lblFirstName" Text='<%#Eval("FirstName") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox runat="server" ID="txtFirstName" Text='<%#Eval("FirstName") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="LastName">
- <ItemTemplate>
- <asp:Label runat="server" ID="lblLastName" Text='<%#Eval("LastName") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox runat="server" ID="txtLastName" Text='<%#Eval("LastName") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Phone No.">
- <ItemTemplate>
- <asp:Label runat="server" ID="lblPhoneNumber" Text='<%#Eval("PhoneNumber") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox runat="server" ID="txtPhoneNumber" Text='<%#Eval("PhoneNumber") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Email">
- <ItemTemplate>
- <asp:Label runat="server" ID="lblEmailAddress" Text='<%#Eval("EmailAddress") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox runat="server" ID="txtEmailAddress" Text='<%#Eval("EmailAddress") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Salary">
- <ItemTemplate>
- <asp:Label runat="server" ID="lblSalary" Text='<%#Eval("Salary") %>'></asp:Label>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox runat="server" ID="txtSalary" Text='<%#Eval("Salary") %>'></asp:TextBox>
- </EditItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Manage">
- <ItemTemplate>
- <asp:LinkButton runat="server" ID="btnEdit" Text="Edit" CommandName="Edit" />
- <br /> <span onclick="return confirm('Are you sure you want to delete this record?')">
- <asp:LinkButton runat="server" ID="btnDelete" Text="Delete" CommandName="Delete" />
- </span> </ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton runat="server" ID="btnUpdate" Text="Update" CommandName="Update" />
- <br />
- <asp:LinkButton runat="server" ID="btnCancel" Text="Cancel" CommandName="Cancel" /> </EditItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </div> <a href="http://www.nextprogramming.com/2014/09/stored-procedure-in-sql-server-advantages-of-stored-procedure-and-how-to-use-stored-procedure">Click Here</a> </form>
- </body>
- </html>

Nag JPosted Aug 4, 2019, 9:20 AM
You made mistake while creating stored procedure that inside event add you have mentioned CreatedDate column but you have not mentioned that column while creating employee table and for id you have to mention identity (1,1)---> Constraint
Nag JPosted Aug 4, 2019, 9:16 AM
You made a mistake while creating stored procedure
Santhakumar MunuswamyPosted Dec 22, 2015, 8:30 AM
Can you upload source code it will be helpful for the beginner
Santhakumar MunuswamyPosted Dec 22, 2015, 8:29 AM
Thanks for nice article
Suman VermaPosted Dec 22, 2015, 8:19 AM
nice one
Raja TPosted Dec 22, 2015, 3:02 AM
Nice, thanks for sharing
Gowtham KPosted Dec 22, 2015, 2:22 AM
Good One
Humayun Kabir MamunPosted Dec 22, 2015, 2:15 AM
Nice...
Aishwarya DPosted Dec 22, 2015, 1:51 AM
'
Aishwarya DPosted Dec 22, 2015, 1:50 AM
Nice share
Banketeshvar NarayanPosted Dec 22, 2015, 1:45 AM
Nice Share