title: ASP.Net How to Update a database from a gridview C#
body: <p>How would you update a SQL database from a gridview? (after a record has been updated)</p>
  
  <p>Using - SharePoint -> Application page - I know, SharePoint...."Making the basic things more basic and the hard things Impossible" (There's not even a SQLDatasource control on the side, you have to add it yourself if you want it - don't know if they don't want you to add one to the project or something...)</p>
  
  <p><a href="https://stackoverflow.com/questions/8934850/binding-gridview-to-sqldatasource-programmatically">I have data being displayed in the gridview</a>, and enabled the </p>
  
  <pre><code>  AutoGenerateEditButton="True" 
  </code></pre>
  
  <p>in the gridview. And the gridview's datasource is a dataset I populated from my database.</p>
  
  <p>I Take it that without a SQLDatasource thats bound to your gridview, you will have to connect to the database again and update the database manually. 
  But I cant seem to find any way to do this other than the SQLDataSource that is bound to the gridview that does the updating automatically.</p>
  
  <p>Ways to do this but Requires the SQLDatasource bound to the gridview</p>
  
  <p><a href="http://www.encodedna.com/2013/01/asp.net-gridview-edit-delete.htm" rel="nofollow noreferrer">Method Nr1</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12192236/update-in-asp-net-gridview-when-binding-with-sqldatasource">Method Nr2</a></p>
  
  <p>How will you go about updating the database after editing the gridview?</p>
  
  <p>--Update Solution-- Added a few modifications. Thanks</p>
  
  <pre><code>  protected void Page_Load(object sender, EventArgs e)
          {
              int employeerId = 1;
              //Add all the employees to the list to view them.
              string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + employeerId;
  
              DataSet ds = dbConn.returnSqlDataset(query);
              grdViewDetails.DataSource = ds;
              grdViewDetails.DataBind();
              int x  = grdViewDetails.Columns.Count;
  
              //Hide the columns.
              grdViewDetails.DataBind();
              if (grdViewDetails.Columns.Count &gt; 0)
              {
                  grdViewDetails.Columns[2].Visible = false;
                  grdViewDetails.Columns[3].Visible = false;
              }
              else
              {
                  grdViewDetails.HeaderRow.Cells[0].Visible = false;
                  foreach (GridViewRow gvr in grdViewDetails.Rows)
                  {
                      gvr.Cells[2].Visible = false;
                      gvr.Cells[3].Visible = false;
                  }
              }
          }
  
  
  protected void grdViewDetails_RowEditing1(object sender, GridViewEditEventArgs e)
      {
          string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + 1;
          grdViewDetails.EditIndex = e.NewEditIndex;
          //e.newedit index:- will be provide index of row for which edit button is selected
          grdViewDetails.DataSource = dbConn.returnSqlDataset(query);
          grdViewDetails.DataBind();
      }
  
  
  
      protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + 1;
  
          foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells)
          {
              if (cell.Controls[0] is TextBox)
              {
                  TextBox textbox = (TextBox)cell.Controls[0];
                  string value = textbox.Text;
              }
              else
              {
                  if (cell.Controls[0] is CheckBox)
                  {
                      CheckBox chkBoxWeek = (CheckBox)cell.Controls[0];
                      Boolean checkStatus = chkBoxWeek.Checked;
                  }
              }
          }
  </code></pre>