索引超出gridview行的范围

时间:2015-07-23 11:22:16

标签: asp.net c#-4.0

我试图阻止之前点击的值,但是我得到错误数组超出绑定索引错误请帮我解决这些问题.lblWelcomeName.Text是facultyname,显示在aspx页面的标签中。所有其他值am从数据库中读取。

protected void createbtnclick_Click(object sender, EventArgs e)
{
     using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    var datedif = new List<DateTime>();
                    var subject1 = new List<string>();
                    var subject2 = new List<string>();
                    var subject3 = new List<string>();
                    var subject4 = new List<string>();
                    var subject5 = new List<string>();
                    var hour1 = new List<bool>();
                    var hour2 = new List<bool>();
                    var hour3 = new List<bool>();
                    var hour4 = new List<bool>();
                    var hour5 = new List<bool>();
                    var faculty1 = new List<string>();
                    var faculty2 = new List<string>();
                    var faculty3 = new List<string>();
                    var faculty4 = new List<string>();
                    var faculty5 = new List<string>();
                    SqlCommand cmd = new SqlCommand("select datedif from test", con2);
                    con2.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        datedif.Add(Convert.ToDateTime(rdr["datedif"]));
                    }
                    con2.Close();
                    DateTime[] datelist = datedif.ToArray();
                    int datearray = datedif.ToArray().Length;
                    for (int i = 0; i < datearray; i++)
                    {
                        SqlCommand cmd1 = new SqlCommand("select subject1,subject2,subject3,subject4,subject5,faculty1,faculty2,faculty3,faculty4,faculty5,hour1,hour2,hour3,hour4,hour5 from test where datedif='" + datelist[i] + "'", con2);
                        con2.Open();
                        SqlDataReader rdr1 = cmd1.ExecuteReader();
                        while (rdr1.Read())
                        {
                            subject1.Add(rdr1["subject1"].ToString());
                            subject2.Add(rdr1["subject2"].ToString());
                            subject3.Add(rdr1["subject3"].ToString());
                            subject4.Add(rdr1["subject4"].ToString());
                            subject5.Add(rdr1["subject5"].ToString());
                            subject1.Add(rdr1["subject1"].ToString());

                            faculty1.Add(rdr1["faculty1"].ToString());
                            faculty2.Add(rdr1["faculty2"].ToString());
                            faculty3.Add(rdr1["faculty3"].ToString());
                            faculty4.Add(rdr1["faculty4"].ToString());
                            faculty5.Add(rdr1["faculty5"].ToString());

                            hour1.Add(Convert.ToBoolean(rdr1["hour1"].ToString()));
                            hour2.Add(Convert.ToBoolean(rdr1["hour2"].ToString()));
                            hour3.Add(Convert.ToBoolean(rdr1["hour3"].ToString()));
                            hour4.Add(Convert.ToBoolean(rdr1["hour4"].ToString()));
                            hour5.Add(Convert.ToBoolean(rdr1["hour5"].ToString()));
                        }
                        con2.Close();
                    }
                    string[] faculty1array = faculty1.ToArray();
                    string[] faculty2array = faculty2.ToArray();
                    string[] faculty3array = faculty3.ToArray();
                    string[] faculty4array = faculty4.ToArray();
                    string[] faculty5array = faculty5.ToArray();

                    string[] subject1array = subject1.ToArray();
                    string[] subject2array = subject2.ToArray();
                    string[] subject3array = subject3.ToArray();
                    string[] subject4array = subject4.ToArray();
                    string[] subject5array = subject5.ToArray();

                    bool[] hour1array = hour1.ToArray();
                    bool[] hour2array = hour2.ToArray();
                    bool[] hour3array = hour3.ToArray();
                    bool[] hour4array = hour4.ToArray();
                    bool[] hour5array = hour5.ToArray();

                    for (int i = 0; i < datearray; i++)
                    {
                        if (faculty1array[i] == lblWelcomeName.Text)
                        {
                            var chk1 = (CheckBox)Gv1.Rows[i].Cells[0].FindControl("chkHour1");
                            chk1.Enabled = false;
                        }
                        if (faculty2array[i] == lblWelcomeName.Text)
                        {
                            var chk2 = (CheckBox)Gv1.Rows[i].Cells[0].FindControl("chkHour2");
                            chk2.Enabled = false;
                        }
                        if (faculty3array[i] == lblWelcomeName.Text)
                        {
                            var chk3 = (CheckBox)Gv1.Rows[i].Cells[0].FindControl("chkHour3");
                            chk3.Enabled = false;
                        }
                        if (faculty4array[i] == lblWelcomeName.Text)
                        {
                            var chk4 = (CheckBox)Gv1.Rows[i].Cells[0].FindControl("chkHour4");
                            chk4.Enabled = false;
                        }
                        if (faculty5array[i] == lblWelcomeName.Text)
                        {
                            var chk5 = (CheckBox)Gv1.Rows[i].Cells[0].FindControl("chkHour5");
                            chk5.Enabled = false;
                        }
                    }

                }
            }
        }

0 个答案:

没有答案