ASP DOT NET

Thursday, July 5, 2018

Q. How to developed calculator using asp.net c# step by step?

Step 1.Open Visual studio and Create one aspx file name as Calculator.

<form id="form1" runat="server">
        <div class="calculator-holder">
            <table class="calculator" id="calc">
                <tr>
                    <td colspan="4" class="calc_td_result">
                        <input type="text" readonly="readonly" runat="server" name="calc_result" id="calc_result" class="calc_result" />
                    </td>
                </tr>
                <tr>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonCE" runat="server" CssClass="calc_btn" Text="CE" OnClick="ButtonCE_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonReturn" runat="server" CssClass="calc_btn" Text="&larr;" OnClick="ButtonReturn_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonPercentage" runat="server" CssClass="calc_btn" Text="%" OnClick="ButtonPercentage_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonPlus" runat="server" CssClass="calc_btn" Text="+" OnClick="ButtonPlus_Click" />
                    </td>
                </tr>
                <tr>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button7" runat="server" CssClass="calc_btn" Text="7" OnClick="Button7_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button8" runat="server" CssClass="calc_btn" Text="8" OnClick="Button8_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button9" runat="server" CssClass="calc_btn" Text="9" OnClick="Button9_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonMinus" runat="server" CssClass="calc_btn" Text="-" OnClick="ButtonMinus_Click" />
                    </td>
                </tr>
                <tr>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button4" runat="server" CssClass="calc_btn" Text="4" OnClick="Button4_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button5" runat="server" CssClass="calc_btn" Text="5" OnClick="Button5_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button6" runat="server" CssClass="calc_btn" Text="6" OnClick="Button6_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonMultiply" runat="server" CssClass="calc_btn" Text="x" OnClick="ButtonMultiply_Click" />
                    </td>
                </tr>
                <tr>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button1" runat="server" CssClass="calc_btn" Text="1" OnClick="Button1_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button2" runat="server" CssClass="calc_btn" Text="2" OnClick="Button2_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button3" runat="server" CssClass="calc_btn" Text="3" OnClick="Button3_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonDivide" runat="server" CssClass="calc_btn" Text="&divide;" OnClick="ButtonDivide_Click" />
                    </td>
                </tr>
                <tr>
                    <td class="calc_td_btn">
                        <asp:Button ID="Button0" runat="server" CssClass="calc_btn" Text="0" OnClick="Button0_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonPlusMinus" runat="server" CssClass="calc_btn" Text="&plusmn;" OnClick="ButtonPlusMinus_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonComa" runat="server" CssClass="calc_btn" Text="," OnClick="ButtonComa_Click" />
                    </td>
                    <td class="calc_td_btn">
                        <asp:Button ID="ButtonEquals" runat="server" CssClass="calc_btn" Text="=" OnClick="ButtonEquals_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </form>

Step 2. Design some Style Sheet using css file

<style>
        .calculator-holder {
            position: relative;
            top: 50%;
            transform: translateY(+50%);
        }

        .calculator {
            width: 300px;
            height: 300px;
            background-color: #eeeeee;
            border: 2px solid #CCCCCC;
            margin: 0 auto 0 auto;
            padding-left: 5px;
            padding-bottom: 5px;
        }

            .calculator td {
                height: 16.66%;
            }

        .calc_td_result {
            text-align: center;
        }

        .calc_result {
            width: 90%;
            text-align: right;
        }

        .calc_td_calculs {
            text-align: center;
        }

        .calc_calculs {
            width: 90%;
            text-align: left;
        }

        .calc_td_btn {
            width: 25%;
            height: 100%;
        }

        .calc_btn {
            width: 90%;
            height: 90%;
            font-size: 20px;
        }
    </style>

Step 3. Now create aspx.cs file and write the bellow code.

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

public partial class Calculator : System.Web.UI.Page
{
    Calculate _Calculate;
    protected void Page_Load(object sender, EventArgs e)
    {
        _Calculate = new Calculate();
    }

    protected void Button0_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "0";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "1";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "2";
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "3";
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "4";
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "5";
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "6";
    }

    protected void Button7_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "7";
    }

    protected void Button8_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "8";
    }

    protected void Button9_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + "9";
    }

    protected void ButtonComa_Click(object sender, EventArgs e)
    {
        calc_result.Value = calc_result.Value + ",";
    }

    protected void ButtonPlusMinus_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            calc_result.Value = "-" + calc_result.Value;
        }
    }

    protected void ButtonPlus_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            ViewState["Value1"] = calc_result.Value;
            ViewState["Operation"] = "Addition";
            calc_result.Value = string.Empty;
        }
    }

    protected void ButtonMinus_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            ViewState["Value1"] = calc_result.Value;
            ViewState["Operation"] = "Subtraction";
            calc_result.Value = string.Empty;
        }
    }

    protected void ButtonMultiply_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            ViewState["Value1"] = calc_result.Value;
            ViewState["Operation"] = "Multiplication";
            calc_result.Value = string.Empty;
        }
    }

    protected void ButtonDivide_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            ViewState["Value1"] = calc_result.Value;
            ViewState["Operation"] = "Division";
            calc_result.Value = string.Empty;
        }
    }

    protected void ButtonPercentage_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            ViewState["Value1"] = calc_result.Value;
            ViewState["Operation"] = "Percentage";
            calc_result.Value = string.Empty;
        }
    }

    protected void ButtonCE_Click(object sender, EventArgs e)
    {
        if ((string)ViewState["Operation"] != null)
        {
            ViewState["Operation"] = null;
        }
        else if ((string)ViewState["Value1"] != null)
        {
            ViewState["Value1"] = null;
        }
    }

    protected void ButtonReturn_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            string CharactersInTextBox = calc_result.Value;
            string FinalCharactersInTextBox = string.Empty;

            for (int i = 0; i < CharactersInTextBox.Length - 1; i++)
            {
                FinalCharactersInTextBox = FinalCharactersInTextBox + CharactersInTextBox[i];
            }

            calc_result.Value = FinalCharactersInTextBox;
        }
    }

    protected void ButtonEquals_Click(object sender, EventArgs e)
    {
        if (calc_result.Value == string.Empty)
        {
            Response.Write("<script>alert('No Value is given.')</script>");
        }
        else
        {
            ViewState["Value2"] = calc_result.Value;
            calc_result.Value = string.Empty;

            try
            {
                if ((string)ViewState["Operation"] == "Addition")
                {
                    calc_result.Value = _Calculate.Add(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                }
                else if ((string)ViewState["Operation"] == "Subtraction")
                {
                    calc_result.Value = _Calculate.Subtract(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                }
                else if ((string)ViewState["Operation"] == "Multiplication")
                {
                    calc_result.Value = _Calculate.Multiply(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                }
                else if ((string)ViewState["Operation"] == "Division")
                {
                    calc_result.Value = _Calculate.Divide(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                }
                else if ((string)ViewState["Operation"] == "Percentage")
                {
                    calc_result.Value = _Calculate.Percentage(Convert.ToInt32(ViewState["Value1"]), Convert.ToInt32(ViewState["Value2"])).ToString();
                }
                else Response.Write("<script>alert('No Operation was recorded.')</script>");
            }
            catch (FormatException)
            {
                Response.Write("<script>alert('Bad Input Format.')</script>");
            }
        }
    }
}

Step 4. Create one class name as Calculate and inside the class write the following code.

public class Calculate
{
        public int Add(int Value1, int Value2)
        {
            return Value1 + Value2;
        }
        public int Subtract(int Value1, int Value2)
        {
            return Value1 - Value2;
        }
        public int Multiply(int Value1, int Value2)
        {
            return Value1 * Value2;
        }
        public double Divide(int Value1, int Value2)
        {
            return Value1 / Value2;
        }
        public string Percentage(int Value1, int Value2)
        {
            Value1 = Value1 * 100;

            return Divide(Value1, Value2) + "%";
        }  
}

Step 5. Now run the application and get the bellow output.




Tuesday, July 3, 2018

Q. How to Bind Data to a DropDownList inside GridView using asp.net c#?

Step 1 Create Table 

CREATE TABLE dbo.Qualification
    (QualificationCode int NOT NULL, 
    Qualification VARCHAR(20) NULL,
CONSTRAINT PK_Master_Qualification PRIMARY KEY CLUSTERED
(QualificationCode ASC)) ON[PRIMARY]


INSERT INTO Qualification (QualificationCode, Qualification) 
    VALUES (1, 'GRADUATE')
INSERT INTO Qualification (QualificationCode, Qualification) 
    VALUES (2, 'ADVANCE PHYSICS')
INSERT INTO Qualification (QualificationCode, Qualification) 
    VALUES (3, 'DIPLOMA IN FINANCE')
INSERT INTO Qualification (QualificationCode, Qualification) 
    VALUES (4, 'MATHEMATICS')
INSERT INTO Qualification (QualificationCode, Qualification) 
    VALUES (5, 'ACCOUNTS')
INSERT INTO Qualification (QualificationCode, Qualification) 
    VALUES (6, 'MANAGEMENT')
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

CREATE TABLE [dbo].[Employees](
[EmployeeID] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nvarchar](20) NOT NULL,
[FirstName] [nvarchar](10) NOT NULL,
[Title] [nvarchar](30) NULL,
[TitleOfCourtesy] [nvarchar](25) NULL,
[BirthDate] [datetime] NULL,
[HireDate] [datetime] NULL,
[Address] [nvarchar](60) NULL,
[City] [nvarchar](15) NULL,
[Region] [nvarchar](15) NULL,
[PostalCode] [nvarchar](10) NULL,
[Country] [nvarchar](15) NULL,
[HomePhone] [nvarchar](24) NULL,
[Extension] [nvarchar](4) NULL,
[Photo] [image] NULL,
[Notes] [ntext] NULL,
[ReportsTo] [int] NULL,
[PhotoPath] [nvarchar](255) NULL,
[Qualification] [varchar](50) NULL,
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
(
[EmployeeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

INSERT [dbo].[Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country], [HomePhone], [Extension], [Photo], [Notes], [ReportsTo], [PhotoPath], [Qualification]) VALUES (1, N'Davolio', N'Nancy', N'Sales Representative', N'Ms.', CAST(N'1948-12-08 00:00:00.000' AS DateTime), 


Now Add aspx page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title>Bind Data to a DropDownList in GridView</title>
   
    <style type="text/css">
        .dropdown { 
            font:12px/0.8 Arial; 
            border:solid 1px #6FA602; 
            border-radius:4px; 
            -moz-border-radius:4px; 
            -webkit-border-radius:4px; 
            cursor:pointer; 
            width:auto;
        }
        .gridv th,td { padding:5px }
    </style>
</head>
<body>
    <form id="form1" runat="server">
     <div style="font:12px/0.8 Arial">
        
            <asp:GridView ID="GridView" runat="server" 
                AutoGenerateColumns="False" 
                AutoGenerateEditButton="True" 
                OnRowDataBound="GridView_RowDataBound" 
                OnRowEditing="GridView_RowEditing" 
                OnRowCancelingEdit="GridView_RowCancelingEdit"
                CssClass="gridv">
                
                <Columns>
                    <asp:TemplateField HeaderText="Employee ID"> 
                        <ItemTemplate>
                            <asp:Label ID="lblEmpID" runat="server" Text='<% #Eval("EmployeeID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText ="Employee Name">
                        <ItemTemplate >
                            <asp:Label ID="lblEmpName" runat ="server" Text ='<%#Eval("FirstName")%>' ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Qualification">
                        <EditItemTemplate>
                            <asp:DropDownList id="ddlQualification" CssClass="dropdown" runat="server">
                                </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblqual" runat="server" Text='<% #Bind("Qualification") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                
                <HeaderStyle
                    BackColor="#989898" 
                    BorderColor="Gray" 
                    Font-Bold="True" 
                    ForeColor="White" 
                    Height="20px" />
                    
                <RowStyle HorizontalAlign="Center" Height="20px" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>


aspx.cs

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

using System.Data;                  // FOR "DataTable"
using System.Data.SqlClient;
using System.Configuration;

public partial class Default7 : System.Web.UI.Page
{
    SqlConnection myConn = default(SqlConnection);
    SqlCommand sqComm = default(SqlCommand);
    System.Data.DataSet ds = new System.Data.DataSet();
    System.Data.SqlClient.SqlDataAdapter SqlAdapter;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (setConn())
        {
            PopulateDS();       // FILL DATASET WITH MASTER DATA.
            ShowEmpDetails();   // SHOW EMPLOYEE DETAILS IN THE GRIDVIEW.
        }
    }
    private bool setConn()
    {
        // SET DATABASE CONNECTION.
        try
        {
            myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
            myConn.Open();

            sqComm = new SqlCommand();
            sqComm.Connection = myConn;
        }
        catch (Exception ex) { return false; }
        return true;
    }
    // CANCEL ROW EDITING.
    protected void GridView_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        GridView.EditIndex = -1;
        ShowEmpDetails();
    }
    protected void GridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            // BIND THE "DROPDOWNLIST" WITH THE DATASET FILLED WITH "QUALIFICATION" DETAILS.
            DropDownList ddlQual = new DropDownList();
            ddlQual = (DropDownList)e.Row.FindControl("ddlQualification");

            if (ddlQual != null)
            {
                ddlQual.DataSource = ds.Tables["qual"];
                ddlQual.DataTextField = ds.Tables["qual"].Columns["Qualification"].ColumnName.ToString();
                ddlQual.DataValueField = ds.Tables["qual"].Columns["QualificationCode"].ColumnName.ToString();
                ddlQual.DataBind();

                // ASSIGN THE SELECTED ROW VALUE ("QUALIFICATION CODE") TO THE DROPDOWNLIST SELECTED VALUE.
                ((DropDownList)e.Row.FindControl("ddlQualification")).SelectedValue =
                    DataBinder.Eval(e.Row.DataItem, "QualificationCode").ToString();
            }
        }
    }
    protected void GridView_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        GridView.EditIndex = e.NewEditIndex;
        ShowEmpDetails();
    }
    private void ShowEmpDetails()
    {
        string sQuery = "SELECT EmpDet.EmployeeID, EmpDet.FirstName, EmpDet.Qualification, Qual.QualificationCode FROM dbo.Employees EmpDet LEFT OUTER JOIN Qualification Qual ON EmpDet.Qualification = Qual.Qualification";        
        SqlDataReader sdrEmp = GetDataReader(sQuery);
        try 
        {
            if (sdrEmp.HasRows) {
            DataTable dt = new DataTable();
            dt.Load(sdrEmp);
            GridView.DataSource = dt;
                GridView.DataBind();        // BIND DATABASE TABLE WITH THE GRIDVIEW.
            }
        } 
        catch (Exception ex) { }
        finally 
        {
            sdrEmp.Close();
            sdrEmp = null;
        }
    }
    // MASTER DATA IN A DATASET.
    private void PopulateDS()
    {
        ds.Clear();
        SqlAdapter = new System.Data.SqlClient.SqlDataAdapter("SELECT QualificationCode, Qualification FROM dbo.Qualification", myConn);
        SqlAdapter.Fill(ds, "qual");
        SqlAdapter.Dispose();
    }
    private SqlDataReader GetDataReader(string sQuery)
    {
        SqlDataReader functionReturnValue = default(SqlDataReader);
        sqComm.CommandText = sQuery;
        sqComm.ExecuteNonQuery();
        functionReturnValue = sqComm.ExecuteReader();
        sqComm.Dispose();
        return functionReturnValue;
    }
}






Sunday, July 1, 2018

Q. How to do update using grid view row?


Step 1. Create table
USE [MyDatabase]
GO
/****** Object:  Table [dbo].[tbl_Employee]    Script Date: 7/1/2018 12:38:32 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_Employee](
[ID] [int] NOT NULL,
[Name] [varchar](50) NULL,
[City] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[tbl_Employee] ([ID], [Name], [City]) VALUES (1, N'Firoz Ansari', N'Bettiah')
INSERT [dbo].[tbl_Employee] ([ID], [Name], [City]) VALUES (2, N'Tamanna', N'Bettiah')
INSERT [dbo].[tbl_Employee] ([ID], [Name], [City]) VALUES (3, N'Gafur Ansari', N'Chhawni')


Step 2.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div align="center">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update" />
                            <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="City">
                        <ItemTemplate>
                            <asp:Label ID="lbl_City" runat="server" Text='<%#Eval("City") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt_City" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle BackColor="#663300" ForeColor="#ffffff" />
                <RowStyle BackColor="#e7ceb6" />
            </asp:GridView>

        </div>
    </form>
</body>
</html>

Step 3.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls; 

public partial class Default6 : System.Web.UI.Page
{
    //Connection String from web.config File  
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection con;
    SqlDataAdapter adapt;
    DataTable dt;  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowData();
        }  
    }
    //ShowData method for Displaying Data in Gridview  
    protected void ShowData()
    {
        dt = new DataTable();
        con = new SqlConnection(cs);
        con.Open();
        adapt = new SqlDataAdapter("Select ID,Name,City from tbl_Employee", con);
        adapt.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        con.Close();
    }
    protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        //NewEditIndex property used to determine the index of the row being edited.  
        GridView1.EditIndex = e.NewEditIndex;
        ShowData();
    }
    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        //Finding the controls from Gridview for the row which is going to update  
        Label id = GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
        TextBox name = GridView1.Rows[e.RowIndex].FindControl("txt_Name") as TextBox;
        TextBox city = GridView1.Rows[e.RowIndex].FindControl("txt_City") as TextBox;
        con = new SqlConnection(cs);
        con.Open();
        //updating the record  
        SqlCommand cmd = new SqlCommand("Update tbl_Employee set Name='" + name.Text + "',City='" + city.Text + "' where ID=" + Convert.ToInt32(id.Text), con);
        cmd.ExecuteNonQuery();
        con.Close();
        //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
        GridView1.EditIndex = -1;
        //Call ShowData method for displaying updated data  
        ShowData();
    }
    protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
        GridView1.EditIndex = -1;
        ShowData();
    }  
}






How to to select duplicate rows from sql server?

 SELECT * FROM Recruitment WHERE Email IN (SELECT Email FROM Recruitment GROUP BY Email HAVING COUNT(*) > 1); WITH CTE AS (     SELECT   ...