title: ASP.NET Edit and Delete issues in GridView
body: <p>I have created GridView with Insert, Edit, and Delete function. The Insert work great but not Delete and Edit. When I click Delete, it suppose to be automatic delete row but it never happen because it still there. Second issue same for Edit, I click Edit, then I change something there then click update, nothing changed after clicked and there data still the same in GridView. I am sure I am missing tiny things... Please help!</p>
  
  <p>Here the C# code behind HTML,</p>
  
  <pre><code>protected void GridViewServer_RowEditing(object sender, GridViewEditEventArgs e)
      {
          GridViewServer.EditIndex = e.NewEditIndex;
          BindGridView();          
  
      }
  
      protected void GridViewServer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
          GridViewServer.EditIndex = -1;
          BindGridView();           
      }
  
      protected void GridViewServer_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {           
          GridViewRow _row = GridViewServer.Rows[e.RowIndex];
          Label servername = (Label)_row.FindControl("lblServerName");
  
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
          SqlCommand cmd = new SqlCommand("delete from tblServer where ServerName = '" + servername + "'", conn);
          cmd.CommandType = CommandType.Text;
          conn.Open();
          cmd.ExecuteNonQuery();
  
          GridViewServer.EditIndex = -1;
          BindGridView();
          conn.Close();
  
      }
  
      protected void GridViewServer_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
  
  
          GridViewRow _row = GridViewServer.Rows[e.RowIndex];
          Label servername = (Label)_row.FindControl("lblServerName");
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
          SqlCommand cmd = new SqlCommand("update tblServer set ClusterName = @ClusterName, Architecture = @Architecture, Database_Type = @DataType, Restriction = @Restriction, Business_Continuity_Plan = @BCP, Tenant_Code = @TenantCode where ClusterName =  '" + servername + "'", conn);
          cmd.CommandType = CommandType.Text;
          //cmd.Parameters.AddWithValue("@ServerName", ((Label)_row.FindControl("lblServerName")).Text);
          cmd.Parameters.AddWithValue("@ClusterName", ((DropDownList)_row.FindControl("DropDownClusterName")).Text);
          cmd.Parameters.AddWithValue("@Architecture", ((DropDownList)_row.FindControl("DropDownArchitecture")).Text);
          cmd.Parameters.AddWithValue("@DataType", ((DropDownList)_row.FindControl("DropDownDataType")).Text);
          cmd.Parameters.AddWithValue("@Restriction", ((DropDownList)_row.FindControl("DropDownRestriction")).Text);
          cmd.Parameters.AddWithValue("@BCP", ((TextBox)_row.FindControl("txtBCP")).Text);
          cmd.Parameters.AddWithValue("@TenantCode", ((TextBox)_row.FindControl("txtTenantCode")).Text);
  
          conn.Open();
          cmd.ExecuteNonQuery();
  
          GridViewServer.EditIndex = -1;
          BindGridView();
          conn.Close();
  
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public void BindGridView()
      {
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);
  
          conn.Open();
  
          string sqlquery = ("SELECT * FROM tblServer");
  
          SqlCommand command = new SqlCommand(sqlquery, conn);
  
          SqlDataAdapter adp = new SqlDataAdapter(command);
  
          DataSet ds = new DataSet();
  
          adp.Fill(ds);
  
          GridViewServer.DataSource = ds.Tables[0];
  
          GridViewServer.DataBind();
      }
  </code></pre>
  
  <p>The GridView HTML code if you need to see,</p>
  
  <pre><code>&lt;asp:GridView ID="GridViewServer" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="ServerName" GridLines="None" ShowFooter="True" onrowcancelingedit="GridViewServer_RowCancelingEdit" 
          onrowdeleting="GridViewServer_RowDeleting" onrowediting="GridViewServer_RowEditing" 
          onrowupdating="GridViewServer_RowUpdating"&gt;
          &lt;Columns&gt;               
              &lt;asp:TemplateField HeaderText="ServerName" SortExpression="ServerName"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblServerName" runat="server" Text='&lt;%# Eval("ServerName") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:Label ID="txtServerName" runat="server" Text='&lt;%# Eval("ServerName") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox ID="txtServerName" runat="server"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="rfvServerName" runat="server" ControlToValidate="txtServerName" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="ClusterName" SortExpression="ClusterName"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblClusterName" runat="server" Text='&lt;%# Eval("ClusterName") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownClusterName" runat="server" AutoPostBack="True" DataSourceID="ClusterName" DataTextField="ClusterName" DataValueField="ClusterName"&gt;
                      &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownClusterName" runat="server" DataSourceID="ClusterName" DataTextField="ClusterName" DataValueField="ClusterName"&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="ClusterName" runat="server" ConnectionString="&lt;%$ ConnectionStrings:Database_Shared_NotebookConnectionString %&gt;" SelectCommand="SELECT [ClusterName] FROM [tblCluster]"&gt;&lt;/asp:SqlDataSource&gt;                       
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Architecture" SortExpression="Architecture"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblArchitecture" runat="server" Text='&lt;%# Eval("Architecture") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownArchitecture" runat="server"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSN EAP&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSN NEA&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSNConnect.com&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSNDMZ.COM&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Dow EAP&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Dow NEA&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownArchitecture" runat="server"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSN EAP&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSN NEA&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSNConnect.com&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;BSNDMZ.COM&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Dow EAP&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Dow NEA&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                       &lt;asp:RequiredFieldValidator ID="rfvArchitecture" runat="server" ControlToValidate="DropDownArchitecture" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                   &lt;/FooterTemplate&gt;
               &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Database_Type" SortExpression="Database_Type"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblDatabaseType" runat="server" Text='&lt;%# Eval("Database_Type") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownDataType" runat="server" &gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Oracle&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;SQL Server&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                       &lt;asp:DropDownList ID="DropDownDataType" runat="server" AutoPostBack="True"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Oracle&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;SQL Server&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                       &lt;asp:RequiredFieldValidator ID="rfvDatabaseType" runat="server" ControlToValidate="DropDownDataType" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                   &lt;/FooterTemplate&gt;
               &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Life_Cycle_Stage" SortExpression="Life_Cycle_Stage"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblLCS" runat="server" Text='&lt;%# Eval("Life_Cycle_Stage") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownLCS" runat="server"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Dev&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Prod&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Test&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                       &lt;asp:DropDownList ID="DropDownLCS" runat="server"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Dev&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Prod&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Test&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                       &lt;asp:RequiredFieldValidator ID="rfvLCS" runat="server" ControlToValidate="DropDownLCS" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                   &lt;/FooterTemplate&gt;
               &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Restriction" SortExpression="Restriction"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblRestriction" runat="server" Text='&lt;%# Eval("Restriction") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="DropDownRestriction" runat="server"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Export Controlled&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;SOX Critical&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Standard&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                       &lt;asp:DropDownList ID="DropDownRestriction" runat="server"&gt;
                          &lt;asp:ListItem Selected="True"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Export Controlled&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;SOX Critical&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Standard&lt;/asp:ListItem&gt;
                       &lt;/asp:DropDownList&gt;
                       &lt;asp:RequiredFieldValidator ID="rfvRestriction" runat="server" ControlToValidate="DropDownRestriction" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                   &lt;/FooterTemplate&gt;
               &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Business_Continuity_Plan" SortExpression="Business_Continuity_Plan"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblBCP" runat="server" Text='&lt;%# Eval("Business_Continuity_Plan") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtBCP" runat="server" Text='&lt;%# Eval("Business_Continuity_Plan") %&gt;'&gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                       &lt;asp:TextBox ID="txtBCP" runat="server"&gt;&lt;/asp:TextBox&gt;
                       &lt;asp:RequiredFieldValidator ID="rfvBCP" runat="server" ControlToValidate="txtBCP" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                   &lt;/FooterTemplate&gt;
               &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Tenant_Code" SortExpression="Tenant_Code"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblTenantCode" runat="server" Text='&lt;%# Eval("Tenant_Code") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtTenantCode" runat="server" Text='&lt;%# Eval("Tenant_Code") %&gt;'&gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                       &lt;asp:TextBox ID="txtTenantCode" runat="server"&gt;&lt;/asp:TextBox&gt;
                       &lt;asp:RequiredFieldValidator ID="rfvTenantCode" runat="server" ControlToValidate="txtTenantCode" ErrorMessage="Required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                   &lt;/FooterTemplate&gt;
               &lt;/asp:TemplateField&gt;
              &lt;asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" /&gt;
              &lt;asp:TemplateField HeaderText="" SortExpression="Insert"&gt;                  
                  &lt;FooterTemplate&gt;
                      &lt;asp:LinkButton ID="lnkInsert" runat="server" Text ="Insert" OnClick="lnkInsert_Click"&gt;&lt;/asp:LinkButton&gt;
                      &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
          &lt;FooterStyle BackColor="White" ForeColor="#000066" /&gt;
          &lt;HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /&gt;
          &lt;PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /&gt;
          &lt;EditRowStyle BackColor="White" /&gt;
          &lt;RowStyle ForeColor="#000066" /&gt;
          &lt;SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /&gt;
          &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
          &lt;SortedAscendingHeaderStyle BackColor="#007DBB" /&gt;
          &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
          &lt;SortedDescendingHeaderStyle BackColor="#00547E" /&gt;
      &lt;/asp:GridView&gt;
  </code></pre>