CRUD Application With Grid View And SQL Data Source In ASP.NET Web Forms

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CRUD_WITH_GRIDVIEW.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" ShowFooter="True">

                <Columns>

                    <asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">

                        <EditItemTemplate>

                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label>

                        </EditItemTemplate>

                        <ItemTemplate>

                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>

                        </ItemTemplate>

                        <FooterTemplate>

                            <asp:LinkButton OnClick="LinkButton1_Click" ValidationGroup="Insert" ID="LinkButton1" runat="server">Insert</asp:LinkButton>

                        </FooterTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="name" SortExpression="name">

                        <EditItemTemplate>

                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>

                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ForeColor="Red" Text="*" SetFocusOnError="true" Display="Dynamic" ErrorMessage="Name is a required field"></asp:RequiredFieldValidator>

                        </EditItemTemplate>

                        <ItemTemplate>

                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>

                        </ItemTemplate>

                        <FooterTemplate>

                            <asp:TextBox ID="FooterName" runat="server"></asp:TextBox>

                            <asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator2" runat="server" ControlToValidate="FooterName" ForeColor="Red" Text="*" SetFocusOnError="true" Display="Dynamic" ErrorMessage="Name is a required field"></asp:RequiredFieldValidator>

                        </FooterTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="gender" SortExpression="gender">

                        <EditItemTemplate>

                            <asp:DropDownList ID="DropDownList1" runat="server" Text='<%# Bind("gender") %>'>

                                <asp:ListItem>Select Gender</asp:ListItem>

                                <asp:ListItem>Male</asp:ListItem>

                                <asp:ListItem>Female</asp:ListItem>

                            </asp:DropDownList>

                            <asp:RequiredFieldValidator InitialValue="Select Gender" ID="RequiredFieldValidator3" runat="server" ControlToValidate="DropDownList1" ForeColor="Red" Text="*" SetFocusOnError="true" Display="Dynamic" ErrorMessage="Gender is a required field"></asp:RequiredFieldValidator>

                        </EditItemTemplate>

                        <ItemTemplate>

                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("gender") %>'></asp:Label>

                        </ItemTemplate>

                        <FooterTemplate>

                            <asp:DropDownList ID="FooterDdlGender" runat="server">

                                <asp:ListItem>Select Gender</asp:ListItem>

                                <asp:ListItem>Male</asp:ListItem>

                                <asp:ListItem>Female</asp:ListItem>

                            </asp:DropDownList>

                            <asp:RequiredFieldValidator ValidationGroup="Insert" InitialValue="Select Gender" ID="RequiredFieldValidator4" runat="server" ControlToValidate="FooterDdlGender" ForeColor="Red" Text="*" SetFocusOnError="true" Display="Dynamic" ErrorMessage="Gender is a required field"></asp:RequiredFieldValidator>

                        </FooterTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="class" SortExpression="class">

                        <EditItemTemplate>

                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("class") %>'></asp:TextBox>

                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TextBox3" ForeColor="Red" Text="*" SetFocusOnError="true" Display="Dynamic" ErrorMessage="Class is a required field"></asp:RequiredFieldValidator>

                        </EditItemTemplate>

                        <ItemTemplate>

                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("class") %>'></asp:Label>

                        </ItemTemplate>

                        <FooterTemplate>

                            <asp:TextBox ID="FooterClass" runat="server"></asp:TextBox>

                            <asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator6" runat="server" ControlToValidate="FooterClass" ForeColor="Red" Text="*" SetFocusOnError="true" Display="Dynamic" ErrorMessage="Class is a required field"></asp:RequiredFieldValidator>

                        </FooterTemplate>

                    </asp:TemplateField>

                </Columns>

                <FooterStyle BackColor="#CCCCCC" />

                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />

                <RowStyle BackColor="White" />

                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

                <SortedAscendingCellStyle BackColor="#F1F1F1" />

                <SortedAscendingHeaderStyle BackColor="#808080" />

                <SortedDescendingCellStyle BackColor="#CAC9C9" />

                <SortedDescendingHeaderStyle BackColor="#383838" />

            </asp:GridView>

            <asp:ValidationSummary ValidationGroup="Insert" ID="ValidationSummary1" runat="server" BackColor="#CCCCCC" ForeColor="Red" Font-Size="Larger"></asp:ValidationSummary>

            <asp:ValidationSummary ID="ValidationSummary2" runat="server" BackColor="#CCCCCC" ForeColor="Red" Font-Size="Larger"></asp:ValidationSummary>

            <br />

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Student] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Student] ([name], [gender], [class]) VALUES (@name, @gender, @class)" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Student]" UpdateCommand="UPDATE [Student] SET [name] = @name, [gender] = @gender, [class] = @class WHERE [Id] = @Id">

                <DeleteParameters>

                    <asp:Parameter Name="Id" Type="Int32" />

                </DeleteParameters>

                <InsertParameters>

                    <asp:Parameter Name="name" Type="String" />

                    <asp:Parameter Name="gender" Type="String" />

                    <asp:Parameter Name="class" Type="Int32" />

                </InsertParameters>

                <UpdateParameters>

                    <asp:Parameter Name="name" Type="String" />

                    <asp:Parameter Name="gender" Type="String" />

                    <asp:Parameter Name="class" Type="Int32" />

                    <asp:Parameter Name="Id" Type="Int32" />

                </UpdateParameters>

            </asp:SqlDataSource>

        </div>

    </form>

</body>

</html>

Above File is CRUD_WITH_GRIDVIEW\WebForm1.aspx


















using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRUD_WITH_GRIDVIEW
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            SqlDataSource1.InsertParameters["name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("FooterName")).Text;
            SqlDataSource1.InsertParameters["gender"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("FooterDdlGender")).SelectedValue;
            SqlDataSource1.InsertParameters["class"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("FooterClass")).Text;

            int a = SqlDataSource1.Insert();
            if (a > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Insertion Successfull !!')</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),"Scripts","<script>alert('Insertion Failed !!')</script>");
            }
        }
    }
}
Above File is CRUD_WITH_GRIDVIEW\WebForm1.aspx.cs

















<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Crud_Db.mdf;Integrated Security=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
</configuration>
Above File is Web.config










Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

Display Auto Increment Value In Text Field After Insert To Database ASP.NET