简体   繁体   English

将gridview的所有行保存到数据库asp.net C#

[英]save all rows of gridview to database asp.net c#

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 ?? 我有一个带有两列的gridview,其中之一是带有下拉列表的模板字段。我想通过gridview外部的保存按钮将所有行保存到数据库,但是当我单击它时,它仅保存gridview的第一行。 。如何将整个gridview保存到数据库?

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. Redirect循环外,以完成对所有行的处理。

Incidentally, it is better practice to redirect this way: 顺便说一句,最好的方法是重定向这种方式:

Response.Redirect("~/DoctorAddEditAttendance.aspx", false);
Context.ApplicationInstance.CompleteRequest();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM