Here we will learn crud operation in GridView using stored procedure in ASP.NET.

Before implementing this application first we will design the database table name is 'CollageInfo' and then stored Procedure name is CurdOperationInGridView.

Create Database Table and Stored procedure like following if you need,
  1. CREATE TABLE CollageInfo
  2. (
  3. Collageid int identity PRIMARY KEY ,
  4. Collagename nvarchar(50),
  5. CollageRank double
  6. )
  7. GO
  8. CREATE PROCEDURE CrudOperationsInGridView
  9. @Collageid int = 0,
  10. @Collagename varchar(50)=null,
  11. @CollageRank int=0,
  12. @status varchar(50)
  13. AS
  14. BEGIN
  15. SET NOCOUNT ON;
  16. --- Insert New Records For GridView
  17. IF @status='INSERT'
  18. BEGIN
  19. INSERT INTO CollageInfo(Collagename,CollageRank) VALUES(@Collagename,@CollageRank)
  20. END
  21. --- Select Records in Table For GridView
  22. IF @status='SELECT'
  23. BEGIN
  24. SELECT Collageid,Collagename,CollageRank FROM CollageInfo
  25. END
  26. --- Update Records in Table For GridView
  27. IF @status='UPDATE'
  28. BEGIN
  29. UPDATE CollageInfo SET @Collagename=@Collagename,CollageRank=@CollageRank WHERE Collageid=@Collageid
  30. END
  31. --- Delete Records from Table For GridView
  32. IF @status='DELETE'
  33. BEGIN
  34. DELETE FROM CollageInfo where Collageid=@Collageid
  35. END
  36. SET NOCOUNT OFF
  37. END
Once we Create stored procedure in database then add an aspx page and write the following code on .aspx Page.
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head id="Head1" runat="server">
  3. <title>GridView Crud Operations using Stored Procedure in ASP.Net</title>
  4. <style type="text/css">
  5. .GridviewDesign {
  6. font-size: 100%;
  7. font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
  8. color: #303933;
  9. }
  10. .headerstyleForGrid {
  11. color: #FFFFFF;
  12. border-right-color: #abb079;
  13. border-bottom-color: #abb079;
  14. background-color: #df5015;
  15. padding: 0.5em 0.5em 0.5em 0.5em;
  16. text-align: center;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <form id="form1" runat="server">
  22. <div class="GridviewDesign">
  23. <asp:GridView runat="server" ID="gvDetailsForCurdOperation" ShowFooter="true" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" DataKeyNames="Collageid,Collagename" OnPageIndexChanging="gvDetailsForCurdOperation_PageIndexChanging" OnRowCancelingEdit="gvDetailsForCurdOperation_RowCancelingEdit" OnRowEditing="gvDetailsForCurdOperation_RowEditing" OnRowUpdating="gvDetailsForCurdOperation_RowUpdating" OnRowDeleting="gvDetailsForCurdOperation_RowDeleting" OnRowCommand="gvDetailsForCurdOperation_RowCommand">
  24. <HeaderStyle CssClass="headerstyleForGrid" />
  25. <Columns>
  26. <asp:BoundField DataField="Collageid" HeaderText="Collage Id" ReadOnly="true" />
  27. <asp:TemplateField HeaderText="Collage Name">
  28. <ItemTemplate>
  29. <asp:Label ID="lblCollagename" runat="server" Text='<%# Eval("Collagename")%>' />
  30. </ItemTemplate>
  31. <EditItemTemplate>
  32. <asp:TextBox ID="txtCollagename" runat="server" Text='<%# Eval("Collagename")%>' />
  33. </EditItemTemplate>
  34. <FooterTemplate>
  35. <asp:TextBox ID="txtpname" runat="server" />
  36. </FooterTemplate>
  37. </asp:TemplateField>
  38. <asp:TemplateField HeaderText="Collage Rank">
  39. <ItemTemplate>
  40. <asp:Label ID="lblCollageRank" runat="server" Text='<%# Eval("CollageRank")%>'></asp:Label>
  41. </ItemTemplate>
  42. <EditItemTemplate>
  43. <asp:TextBox ID="txtCollageRank" runat="server" Text='<%# Eval("CollageRank")%>' />
  44. </EditItemTemplate>
  45. <FooterTemplate>
  46. <asp:TextBox ID="txtCollageRank" runat="server" />
  47. <asp:Button ID="btnAddNewItem" CommandName="AddNew" runat="server" Text="Add" />
  48. </FooterTemplate>
  49. </asp:TemplateField>
  50. <asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
  51. </Columns>
  52. </asp:GridView>
  53. <asp:Label ID="lblresult" runat="server"></asp:Label>
  54. </div>
  55. </form>
  56. </body>
  57. </html>
After completion code for .aspx page write the following code on .aspx.cs page,
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack) {
  4. BindGridview();
  5. }
  6. }
  7. protected void BindGridview()
  8. {
  9. DataSet ds = new DataSet();
  10. using(SqlConnection con = new SqlConnection("Data Source=Munesh;Integrated Security=true;Initial Catalog=GridCurdOperation"))
  11. {
  12. con.Open();
  13. SqlCommand cmd = new SqlCommand("CrudOperationsInGridView", con);
  14. cmd.CommandType = CommandType.StoredProcedure;
  15. cmd.Parameters.AddWithValue("@status", "SELECT");
  16. SqlDataAdapter da = new SqlDataAdapter(cmd);
  17. da.Fill(ds);
  18. con.Close();
  19. if (ds.Tables[0].Rows.Count > 0) {
  20. gvDetailsForCurdOperation.DataSource = ds;
  21. gvDetailsForCurdOperation.DataBind();
  22. } else {
  23. ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
  24. gvDetailsForCurdOperation.DataSource = ds;
  25. gvDetailsForCurdOperation.DataBind();
  26. int columncount = gvDetailsForCurdOperation.Rows[0].Cells.Count;
  27. gvDetailsForCurdOperation.Rows[0].Cells.Clear();
  28. gvDetailsForCurdOperation.Rows[0].Cells.Add(new TableCell());
  29. gvDetailsForCurdOperation.Rows[0].Cells[0].ColumnSpan = columncount;
  30. gvDetailsForCurdOperation.Rows[0].Cells[0].Text = "No Records Found";
  31. }
  32. }
  33. }
  34. protected void gvDetailsForCurdOperation_RowCommand(object sender, GridViewCommandEventArgs e) {
  35. if (e.CommandName.Equals("AddNew")) {
  36. TextBox txtname = (TextBox) gvDetailsForCurdOperation.FooterRow.FindControl("txtpname");
  37. TextBox txtprice = (TextBox) gvDetailsForCurdOperation.FooterRow.FindControl("txtprice");
  38. crudoperationsForGrid("INSERT", txtname.Text, txtprice.Text, 0);
  39. }
  40. }
  41. protected void gvDetailsForCurdOperation_RowEditing(object sender, GridViewEditEventArgs e) {
  42. gvDetailsForCurdOperation.EditIndex = e.NewEditIndex;
  43. BindGridview();
  44. }
  45. protected void gvDetailsForCurdOperation_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
  46. gvDetailsForCurdOperation.EditIndex = -1;
  47. BindGridview();
  48. }
  49. protected void gvDetailsForCurdOperation_PageIndexChanging(object sender, GridViewPageEventArgs e) {
  50. gvDetailsForCurdOperation.PageIndex = e.NewPageIndex;
  51. BindGridview();
  52. }
  53. protected void gvDetailsForCurdOperation_RowUpdating(object sender, GridViewUpdateEventArgs e) {
  54. int Collageid = Convert.ToInt32(gvDetailsForCurdOperation.DataKeys[e.RowIndex].Values["Collageid"].ToString());
  55. TextBox txtname = (TextBox) gvDetailsForCurdOperation.Rows[e.RowIndex].FindControl("txtCollagename");
  56. TextBox txtCollageRank = (TextBox) gvDetailsForCurdOperation.Rows[e.RowIndex].FindControl("txtCollageRank");
  57. crudoperationsForGrid("UPDATE", txtname.Text, txtCollageRank.Text, Collageid);
  58. }
  59. protected void gvDetailsForCurdOperation_RowDeleting(object sender, GridViewDeleteEventArgs e) {
  60. int Collageid = Convert.ToInt32(gvDetailsForCurdOperation.DataKeys[e.RowIndex].Values["Collageid"].ToString());
  61. string Collagename = gvDetailsForCurdOperation.DataKeys[e.RowIndex].Values["Collagename"].ToString();
  62. crudoperationsForGrid("DELETE", Collagename, "", Collageid);
  63. }
  64. protected void crudoperationsForGrid(string status, string Collagename, string price, int Collageid) {
  65. using(SqlConnection con = new SqlConnection("Data Source=Munesh;Integrated Security=true;Initial Catalog=GridCurdOperation")) {
  66. con.Open();
  67. SqlCommand cmd = new SqlCommand("CrudOperationsInGridView", con);
  68. cmd.CommandType = CommandType.StoredProcedure;
  69. if (status == "INSERT") {
  70. cmd.Parameters.AddWithValue("@status", status);
  71. cmd.Parameters.AddWithValue("@Collagename", Collagename);
  72. cmd.Parameters.AddWithValue("@CollageRank", price);
  73. } else if (status == "UPDATE") {
  74. cmd.Parameters.AddWithValue("@status", status);
  75. cmd.Parameters.AddWithValue("@Collagename", Collagename);
  76. cmd.Parameters.AddWithValue("@CollageRank", price);
  77. cmd.Parameters.AddWithValue("@Collageid", Collageid);
  78. } else if (status == "DELETE") {
  79. cmd.Parameters.AddWithValue("@status", status);
  80. cmd.Parameters.AddWithValue("@Collageid", Collageid);
  81. }
  82. cmd.ExecuteNonQuery();
  83. lblresult.ForeColor = Color.Green;
  84. lblresult.Text = Collagename + " details " + status.ToLower() + "d successfully";
  85. gvDetailsForCurdOperation.EditIndex = -1;
  86. BindGridview();
  87. }
  88. }
Now run your application and see the output and perform CRUD operation for GridView.