title: save all rows of gridview to database asp.net c#
body: <p>I have a gridview with two columns one of them is template field with dropdown list.. I want to save all rows to the database with a save button outside the gridview, but when I click it is saves only the first row of the gridview.. How Can I save the entire gridview to the database ??</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
          {
  
              for (int i = 0; i &lt; GridView2.Rows.Count; i++)
              {
  
                      GridViewRow row = GridView2.Rows[i];
  
                      string coursecode = DropDownList1.SelectedValue.ToString();
                      string weekno = DropDownList2.SelectedValue.ToString();
                      string day = DropDownList3.SelectedValue;
                      string stid = row.Cells[0].Text;
                      DropDownList ddlstatus = (DropDownList)row.Cells[1].FindControl("DropDownList9");
                      string status = ddlstatus.SelectedValue.ToString();
  
                      double percentage;
                      if (status == "A")
                      {
                          if (day == "Saturday")
                          {
                              percentage = 6.66;
                          }
                          else
                          {
                              percentage = 3.33;
                          }
                      }
                      else
                      {
                          percentage = 0;
                      }
  
  
                      String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                      SqlConnection con = new SqlConnection(strConnString);
                      String query = "insert into Attendance values (@CourseCode, @St_ID, @WeekNo, @Day,@Status, @Percentage)";
                      SqlCommand cmd = new SqlCommand(query, con);
                      cmd.Parameters.AddWithValue("@CourseCode", coursecode);
                      cmd.Parameters.AddWithValue("@WeekNo", weekno);
                      cmd.Parameters.AddWithValue("@Day", day);
                      cmd.Parameters.AddWithValue("@St_ID", stid);
                      cmd.Parameters.AddWithValue("@Status", status);
                      cmd.Parameters.AddWithValue("@Percentage", percentage);
                      con.Open();
                      cmd.ExecuteNonQuery();
                      con.Close();
                      Response.Redirect("~/DoctorAddEditAttendance.aspx");
                  }
              }
  </code></pre>