title: Insert data from a GridView into database on a click of a Button
body: <p>I am trying to insert every row in the GridView into a table on a click of button.  My problem with the code below is that it is only inserting the first row and repeating the same record in the rest of the gridview.  For example, if i have on the first row:</p>
  
  <pre><code>2    ABC    ATL
  </code></pre>
  
  <p>it will repeat this for the rest of the columns.  How can i capture every row and insert it into my database?  thanks. 
  Here is my code:</p>
  
  <pre><code>protected void Insert(object sender, EventArgs e)
  {
      foreach (GridViewRow row in GridView1.Rows)
      {
          if (row.RowType == DataControlRowType.DataRow)
          {
              Label ID = GridView1.Rows[0].FindControl("lblID") as Label;
              Label NAME = GridView1.Rows[0].FindControl("lblQue") as Label;
              TextBox LOC = GridView1.Rows[0].FindControl("txtPM") as TextBox;
  
              SqlConnection con = new SqlConnection(strConnString);
              SqlCommand cmd = new SqlCommand();
              cmd.CommandType = CommandType.Text;
              cmd.CommandText =
                  "insert into myTable(ID, NAME, LOC ) values(@ID, @NAME,  @LOC) " +
                  // "where ID=@ID;" + 
                  "SELECT ID, NAME, LOC FROM MYTABLE";
  
              cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
              cmd.Parameters.Add("@NAME", SqlDbType.VarChar).Value = NAME.Text;
              cmd.Parameters.Add("@LOC", SqlDbType.VarChar).Value = LOC.Text;
  
              GridView1.EditIndex = -1;
              GridView1.DataSource = GetData(cmd);
              GridView1.DataBind();
          }
      }
  }
  </code></pre>