title: Edit, delete and update gridview with validation and textbox on it
body: <p>I have four text-boxes, two buttons one grid-view and one check-box and i put require field validation on each text-box i have a database so that when i enter value in the text-boxes and press submit button data store in the database and show in gird as i use datatable. i Have edit, delete and update link in my grid-view now when i click on edit or delete link validation error message displayed and i won't make changes in my grid. can any one help me on this. </p>
  
  <p>Thanks in advance</p>
  
  <p>My aspx code:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;&lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body style="height: 191px"&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      &lt;table class="style1"&gt;
          &lt;tr&gt;
              &lt;td class="style3"&gt;
                  FirstName&lt;/td&gt;
              &lt;td&gt;
      &lt;asp:TextBox ID="tFirstName" runat="server"&gt;&lt;/asp:TextBox&gt;
      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                      ControlToValidate="tFirstName" ErrorMessage="Enter First Name"&gt;*&lt;/asp:RequiredFieldValidator&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style3"&gt;
                  Last name&lt;/td&gt;
              &lt;td&gt;
      &lt;asp:TextBox ID="tLastName" runat="server"&gt;&lt;/asp:TextBox&gt;
      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                      ControlToValidate="tLastName" ErrorMessage="Enter Last Name"&gt;*&lt;/asp:RequiredFieldValidator&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style3"&gt;
                  Age&lt;/td&gt;
              &lt;td&gt;
      &lt;asp:TextBox ID="tAge" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                      ControlToValidate="tAge" ErrorMessage="" dalega"&gt;*&lt;/asp:RequiredFieldValidator&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style7"&gt;
                  Is Active&lt;/td&gt;
              &lt;td class="style8"&gt;
                  &lt;asp:CheckBox ID="dropActive" runat="server" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style3"&gt;
                  Auto ID&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="autoID" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                  ControlToValidate="autoID" ErrorMessage="Marzi hai teri"&gt;*&lt;/asp:RequiredFieldValidator&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style6"&gt;
                  &lt;/td&gt;
              &lt;td class="style5"&gt;
         &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Sumit" ValidationGroup="abc"/&gt;
      &lt;input type="reset" /&gt;
                  &lt;br /&gt;   
                  &lt;asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="72px" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style4" colspan="2"&gt;
                   &lt;asp:Label ID="lblMessage" runat="server" ForeColor="Green" EnableViewState="false" /&gt;
    &lt;asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" Width="36%" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow" OnRowUpdating="UpdateRow" DataKeyNames="AutoId" OnRowDeleting="DeleteRow" AllowPaging="true" OnPageIndexChanging="ChangePage" AllowSorting="True"&gt;
  &lt;Columns&gt;
      &lt;asp:TemplateField HeaderText="Edit"&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" /&gt;
          &lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" /&gt;
              &lt;asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" /&gt;
          &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:BoundField HeaderText="AutoId" DataField="AutoId" ReadOnly="true" /&gt;
      &lt;asp:TemplateField HeaderText="First Name"&gt;
          &lt;ItemTemplate&gt;
              &lt;%# Eval("FirstNAme") %&gt;&lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:TextBox ID="txtFirstName" runat="server" Text='&lt;%# Eval("FirstName") %&gt;'/&gt;
          &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Last Name"&gt;
          &lt;ItemTemplate&gt;
              &lt;%# Eval("LastName") %&gt;&lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:TextBox ID="txtLastName" runat="server" Text='&lt;%# Eval("LastName") %&gt;'/&gt;
          &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Age"&gt;
          &lt;ItemTemplate&gt;
              &lt;%# Eval("Age") %&gt;&lt;/ItemTemplate&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:TextBox ID="txtAge" runat="server" Text='&lt;%# Eval("Age") %&gt;' Columns="3" /&gt;
             &lt;asp:RequiredFieldValidator ID="REw" runat="server" ControlToValidate="txtAge" Text="*" /&gt; 
          &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Is Active?"&gt;
          &lt;ItemTemplate&gt;
              &lt;%# Eval("Active").ToString().Equals("True") ? "Yes" : "No" %&gt;
          &lt;/ItemTemplate&gt;
          &lt;HeaderStyle HorizontalAlign="Left" /&gt;
          &lt;EditItemTemplate&gt;
              &lt;asp:DropDownList ID="dropActive" runat="server" SelectedValue='&lt;%# Eval("Active") %&gt;'&gt;
              &lt;asp:ListItem Text="Yes" Value="True" /&gt;
              &lt;asp:ListItem Text="No" Value="False" /&gt;
              &lt;/asp:DropDownList&gt;
          &lt;/EditItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="Delete?"&gt;
          &lt;ItemTemplate&gt;
              &lt;span onclick="return confirm('Are you sure to delete?')"&gt;
              &lt;asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete" /&gt;
              &lt;/span&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
  &lt;AlternatingRowStyle BackColor="White" /&gt;
  &lt;EditRowStyle BackColor="#efefef" /&gt;
  &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
  &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
  &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
  &lt;RowStyle BackColor="#EFF3FB" /&gt;
  &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
  &lt;SortedAscendingCellStyle BackColor="#F5F7FB" /&gt;
  &lt;SortedAscendingHeaderStyle BackColor="#6D95E1" /&gt;
  &lt;SortedDescendingCellStyle BackColor="#E9EBEF" /&gt;
  &lt;SortedDescendingHeaderStyle BackColor="#4870BE" /&gt;
   &lt;/asp:GridView&gt;
                      &lt;/table&gt;
                      &lt;/div&gt;
                      &lt;/form&gt;
                     &lt;/body&gt;
                     &lt;/html&gt;
  </code></pre>
  
  <p>code for aspx.cs:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          PopulateData();
      }
  }
  protected void EditRow(object sender, GridViewEditEventArgs e)
  {
      GridView1.EditIndex = e.NewEditIndex;
      this.PopulateData();
  }
  protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)
  {
      GridView1.EditIndex = -1;
      this.PopulateData();
  }
  protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
  {
      var autoID = GridView1.DataKeys[e.RowIndex].Value;
      using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=EMS;Integrated Security=True"))
      {
          string sql = "Delete from PersonalDetail" +" where AutoId = @AutoId";
          using (SqlCommand cmd = new SqlCommand(sql, conn))
          {
              cmd.Parameters.AddWithValue("@AutoId", autoID);
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
          }
      }
      lblMessage.Text ="Record has been deleted successfully !";
      lblMessage.ForeColor = System.Drawing.
      Color.Red;
      this.PopulateData();
  }
  protected void ChangePage(object sender, GridViewPageEventArgs e)
  {
      GridView1.PageIndex = e.NewPageIndex;
      this.PopulateData();
  }
  protected void UpdateRow(object sendedr, GridViewUpdateEventArgs e)
  {
      var autoID = GridView1.DataKeys[e.RowIndex].Value;
      GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
      TextBox tFirstName = row.FindControl("txtFirstName") as TextBox;
      TextBox tLastName = row.FindControl("txtLastName") as TextBox;
      TextBox tAge = row.FindControl("txtAge") as TextBox;
      DropDownList dropActive = row.FindControl("dropActive") as DropDownList;
      using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=EMS;Integrated Security=True"))
      {
          string sql = "Update PersonalDetail set FirstName = @FirstName,LastName=@LastName, Age= @Age, Active = @Active" + " where AutoId = @AutoId";
          using (SqlCommand cmd = new SqlCommand(sql, conn))
          {
              cmd.Parameters.AddWithValue("@FirstName", tFirstName.Text.Trim());
              cmd.Parameters.AddWithValue("@LastName", tLastName.Text.Trim());
              cmd.Parameters.AddWithValue("@Age", tAge.Text.Trim());
              cmd.Parameters.AddWithValue("@Active", dropActive.SelectedValue);
              cmd.Parameters.AddWithValue("@AutoId", autoID);
              conn.Open();
              cmd.ExecuteNonQuery();
              conn.Close();
          }
      }
      lblMessage.Text ="Record updated successfully !";
      GridView1.EditIndex = -1;
      this.PopulateData();
  }
  private void PopulateData()
  {
      DataTable table = new DataTable();
      using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=EMS;Integrated Security=True"))
      {
          string sql = "Select * from PersonalDetail";
          using (SqlCommand cmd = new SqlCommand(sql, conn))
          {
              using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
              {
                  ad.Fill(table);
              }
          }
      }
      GridView1.DataSource = table;
      GridView1.DataBind();
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=EMS;Integrated Security=True");
      con.Open();
  
      string FirstName = tFirstName.Text;
      string LastName = tLastName.Text;
      string Age = tAge.Text;
      string Active = dropActive.Text;
      string AutoId = autoID.Text;
  
      using (SqlCommand cmd = con.CreateCommand())
      {
          cmd.CommandText = "insert into PersonalDetail(FirstName,LastName,Age,Active,AutoId) values('" + FirstName + "','" + LastName + "','" + Age + "','" + Active + "','" + AutoId + "')";
          cmd.Parameters.AddWithValue("@FirstName", tFirstName.Text.Trim());
          cmd.Parameters.AddWithValue("@LastName", tLastName.Text.Trim());
          cmd.Parameters.AddWithValue("@Age", tAge.Text.Trim());
          cmd.Parameters.AddWithValue("@Active", dropActive.Text.Trim());
  
          cmd.Parameters.AddWithValue("@AutoId", autoID.Text.Trim());
          cmd.ExecuteNonQuery();
      }
      con.Close();
      tFirstName.Text = string.Empty;
      tLastName.Text = string.Empty;
      tAge.Text = string.Empty;
      dropActive.Text = string.Empty;
      tFirstName.Focus();
  }
  </code></pre>