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 ??
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 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");
}
}
Move the Redirect
outside of the loop so you finish processing all rows.
Incidentally, it is better practice to redirect this way:
Response.Redirect("~/DoctorAddEditAttendance.aspx", false);
Context.ApplicationInstance.CompleteRequest();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.