title: How to Update a GridView when boundfield is used
body: <p>I have a <code>GridView</code> and it is bound to a database. I am finding difficulties in updating the <code>GridView</code>  and corresponding table in the database.</p>
  
  <p>My asp code for <code>GridView</code> after binding to SQLdatasource is :</p>
  
  <pre><code>&lt;asp:GridView ID="GridView2" runat="server" OnRowEditing="GridView2_RowEditing"
      OnRowUpdating="GridView2_RowUpdating" CellPadding="4" ForeColor="#333333" OnRowCancelingEdit="GridView2_RowCancelingEdit"
      OnRowDataBound="GridView2_RowDataBound" AutoGenerateColumns="False" 
      DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" DataKeyNames="Locations"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="Locations" HeaderText="Locations" 
              SortExpression="Locations" ReadOnly="true"/&gt;
          &lt;asp:BoundField DataField="Lamp_pro4" HeaderText="Lamp_pro4" 
              SortExpression="Lamp_pro4" /&gt;
          &lt;asp:BoundField DataField="Lamp_pro5" HeaderText="Lamp_pro5" 
              SortExpression="Lamp_pro5" /&gt;
          &lt;asp:BoundField DataField="AC_Profile5" HeaderText="AC_Profile5" 
              SortExpression="AC_Profile5" /&gt;
      &lt;/Columns&gt;
  
  &lt;/asp:GridView&gt;
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:TouchPadConnectionString %&gt;" 
      SelectCommand="SELECT * FROM [Quantity]"&gt;
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>My datakey is Locations and its readonly.</p>
  
  <p>The .cs code for updating is:</p>
  
  <pre><code>protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          SqlConnection con = new SqlConnection("Data Source=ARCHANA-PC\\ARCHANA;Initial Catalog=TouchPad;Integrated Security=True");
          string LocName = GridView2.DataKeys[e.RowIndex].Values["Locations"].ToString();
          TextBox txt1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro4");
          TextBox txt2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("Lamp_pro5");
          TextBox txt3 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("AC_Profile5");
          string updStmt = "UPDATE Quantity set Lamp_pro4=@Lamp_pro4,Lamp_pro5=@Lamp_pro5,AC_Profile5=@AC_Profile5 where Locations=@locName";
  
          con.Open();
          SqlCommand updCmd = new SqlCommand(updStmt, con);
  
          updCmd.Parameters.AddWithValue("@locName", LocName);
          updCmd.Parameters.AddWithValue("@Lamp_pro4", txt1.Text);
          updCmd.Parameters.AddWithValue("@Lamp_pro5", txt2.Text);
          updCmd.Parameters.AddWithValue("@AC_Profile5", txt3.Text);
          updCmd.ExecuteNonQuery();
          GridView2.DataBind();
  
      }
  </code></pre>