更新的查询无法正常工作

时间:2015-05-25 10:21:09

标签: c# asp.net

阻止期间和阻止期间值的整列我无法更新

我有一个教师和管理页面。如果管理员阻止它应该阻止一段时间。然后教师可以选择那里的主题。我几乎做了所有事情。如果教师选择最后一小时被选中我无法更新整行和列值不工作database.my查询其工作原理如果主题值为null且hour1值为true则只有更新查询才有效。我将展示一些我厌倦的代码请检查并给我一些提示来克服

<asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gv1_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="mGrid1">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="datedif" HeaderText="Day/Hour" SortExpression="datedif" />
                <asp:TemplateField HeaderText="Hour1">
                    <EditItemTemplate>
                        <asp:CheckBox ID="chkColumn1" runat="server" OnClick="Check_Click(this)" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkHour1" runat="server" Checked='<%# bool.Parse( Eval("hour1").ToString()) %>' Enabled='<%# Eval("hour1").ToString().Equals("False") %>'
                            OnCheckedChanged="CheckBox1_CheckedChanged" OnClick="Check_Click(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Hour2">
                    <EditItemTemplate>
                        <asp:CheckBox ID="ChkColumn2" runat="server" OnClick="Check_Click(this)" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkHour2" runat="server" Checked='<%# bool.Parse( Eval("hour2").ToString()) %>' Enabled='<%# Eval("hour2").ToString().Equals("False") %>'
                            OnCheckedChanged="CheckBox6_CheckedChanged" OnClick="Check_Click(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Hour3">
                    <EditItemTemplate>
                        <asp:CheckBox ID="chkColumn3" runat="server" OnClick="Check_Click(this)" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkHour3" runat="server" Checked='<%# bool.Parse( Eval("hour3").ToString()) %>' Enabled='<%# Eval("hour3").ToString().Equals("False") %>'
                            OnCheckedChanged="CheckBox3_CheckedChanged" OnClick="Check_Click(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Hour4">
                    <EditItemTemplate>
                        <asp:CheckBox ID="chkColumn4" runat="server" OnClick="Check_Click(this)" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkHour4" runat="server" Checked='<%# bool.Parse( Eval("hour4").ToString()) %>' Enabled='<%# Eval("hour4").ToString().Equals("False") %>'
                            OnCheckedChanged="CheckBox4_CheckedChanged" OnClick="Check_Click(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Hour5">
                    <EditItemTemplate>
                        <asp:CheckBox ID="chkColumn5" runat="server" OnClick="Check_Click(this)" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkHour5" runat="server" Checked='<%# bool.Parse( Eval("hour5").ToString()) %>' Enabled='<%# Eval("hour5").ToString().Equals("False") %>'
                            OnCheckedChanged="CheckBox5_CheckedChanged" OnClick="Check_Click(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>


 protected void btnsubmit_Click(object sender, EventArgs e)
        {
            using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                con1.Open();
                SqlCommand cmd = new SqlCommand("insert into subjectdetails (dd,batch,wk) (select datedif,batch,daywk from datelist where batch = '" + ddlbatch.SelectedValue + "' and WeekMonth ='" + ddlmonths.SelectedValue + "')", con1);
                cmd.ExecuteNonQuery();
                con1.Close();
            }
            using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {

                foreach (GridViewRow r in Gv1.Rows)
                {
                    if (
                        (
                        (
                        (
                           (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour1"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour1"))).Checked == false)
                        || (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour2"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour2"))).Checked == false)
                        || (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour3"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour3"))).Checked == false)
                        || (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour4"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour4"))).Checked == false)
                        || (((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour5"))).Checked == true || ((CheckBox)(Gv1.Rows[r.RowIndex].FindControl("chkHour5"))).Checked == false)
                        )
                        )
                        )
                        )
                    {
                        bool hours1 = (r.FindControl("chkHour1") as CheckBox).Checked;
                        bool hours2 = (r.FindControl("chkHour2") as CheckBox).Checked;
                        bool hours3 = (r.FindControl("chkHour3") as CheckBox).Checked;
                        bool hours4 = (r.FindControl("chkHour4") as CheckBox).Checked;
                        bool hours5 = (r.FindControl("chkHour5") as CheckBox).Checked;
                        object subject1 = DBNull.Value, subject2 = DBNull.Value, subject3 = DBNull.Value, subject4 = DBNull.Value, subject5 = DBNull.Value;
                        bool hour1 = true;
                        bool hour2 = true;
                        bool hour3 = true;
                        bool hour4 = true;
                        bool hour5 = true;
                        string datedif = r.Cells[0].Text;


                        SqlCommand com1 = new SqlCommand("select subject1,hour1 from test", con2);
                        con2.Open();
                        SqlDataReader rdr1 = com1.ExecuteReader();
                        while (rdr1.Read())
                        {
                            subject1 = rdr1["subject1"];
                            hour1 = Convert.ToBoolean(rdr1["hour1"]);
                        }
                        con2.Close();


                        SqlCommand com2 = new SqlCommand("select subject2,hour2 from test", con2);
                        con2.Open();
                        SqlDataReader rdr2 = com2.ExecuteReader();
                        while (rdr2.Read())
                        {
                            subject2 = rdr2["subject2"];
                            hour2 = Convert.ToBoolean(rdr2["hour2"]);
                        }
                        con2.Close();


                        SqlCommand com3 = new SqlCommand("select subject3,hour3 from test", con2);
                        con2.Open();
                        SqlDataReader rdr3 = com3.ExecuteReader();
                        while (rdr3.Read())
                        {
                            subject3 = rdr3["subject3"];
                            hour3 = Convert.ToBoolean(rdr3["hour3"]);
                        }
                        con2.Close();


                        SqlCommand com4 = new SqlCommand("select subject4,hour4 from test", con2);
                        con2.Open();
                        SqlDataReader rdr4 = com4.ExecuteReader();
                        while (rdr4.Read())
                        {
                            subject4 = rdr4["subject4"];
                            hour4 = Convert.ToBoolean(rdr4["hour4"]);
                        }
                        con2.Close();


                        SqlCommand com5 = new SqlCommand("select subject5,hour5 from test", con2);
                        con2.Open();
                        SqlDataReader rdr5 = com5.ExecuteReader();
                        while (rdr5.Read())
                        {
                            subject5 = rdr5["subject5"];
                            hour5 = Convert.ToBoolean(rdr5["hour5"]);
                        }
                        con2.Close();
                        SqlCommand comm1 = new SqlCommand();
                        SqlCommand comm2 = new SqlCommand();
                        SqlCommand comm3 = new SqlCommand();
                        SqlCommand comm4 = new SqlCommand();
                        SqlCommand comm5 = new SqlCommand();
                        con2.Open();

                        if (subject1 == DBNull.Value && hour1)
                        {
                            comm1.CommandText = "UPDATE test SET subject1=@subject1,hours1 =@hour1 where datedif=@datedif";
                            comm1.Connection = con2;
                        }
                        else
                        {
                            comm1.CommandText = "UPDATE test SET subject1=subject1,hours1=hour1";
                            comm1.Connection = con2;
                        }
                        if (subject2 == DBNull.Value && hour2)
                        {
                            comm2.CommandText = "UPDATE test SET subject2=@subject2,hours2 =@hour2 where datedif=@datedif";
                            comm2.Connection = con2;
                        }
                        else
                        {
                            comm2.CommandText = "UPDATE test SET subject2=subject2,hours2=hour2";
                            comm2.Connection = con2;
                        }
                        if (subject3 == DBNull.Value && hour3)
                        {
                            comm3.CommandText = "UPDATE test SET subject3=@subject3,hours3=@hour3 where datedif=@datedif";
                            comm3.Connection = con2;
                        }
                        else
                        {
                            comm3.CommandText = "UPDATE test SET subject3=subject3,hours3=hour3";
                            comm3.Connection = con2;
                        }
                        if (subject4 == DBNull.Value && hour4)
                        {
                            comm4.CommandText = "UPDATE test SET subject4=@subject4,hours4 =@hour4 where datedif=@datedif";
                            comm4.Connection = con2;
                        }
                        else
                        {
                            comm4.CommandText = "UPDATE test SET subject4=subject4,hours4=hour4";
                            comm4.Connection = con2;
                        }
                        if (subject5 == DBNull.Value && hour5)
                        {
                            comm5.CommandText = "UPDATE test SET subject5=@subject5,hours5=@hour5 where datedif=@datedif";
                            comm5.Connection = con2;
                        }
                        else
                        {
                            comm5.CommandText = "UPDATE test SET subject5=subject5,hours5=hour5";
                            comm5.Connection = con2;
                        }
                        comm1.Parameters.AddWithValue("@subject1", ddlsubj.SelectedValue);
                        comm2.Parameters.AddWithValue("@subject2", ddlsubj.SelectedValue);
                        comm3.Parameters.AddWithValue("@subject3", ddlsubj.SelectedValue);
                        comm4.Parameters.AddWithValue("@subject4", ddlsubj.SelectedValue);
                        comm5.Parameters.AddWithValue("@subject5", ddlsubj.SelectedValue);
                        comm1.Parameters.AddWithValue("@datedif", datedif);
                        comm2.Parameters.AddWithValue("@datedif", datedif);
                        comm3.Parameters.AddWithValue("@datedif", datedif);
                        comm4.Parameters.AddWithValue("@datedif", datedif);
                        comm5.Parameters.AddWithValue("@datedif", datedif);
                        comm1.Parameters.AddWithValue("@hour1", hours1);
                        comm2.Parameters.AddWithValue("@hour2", hours2);
                        comm3.Parameters.AddWithValue("@hour3", hours3);
                        comm4.Parameters.AddWithValue("@hour4", hours4);
                        comm5.Parameters.AddWithValue("@hour5", hours5);
                        comm1.ExecuteNonQuery();
                        comm2.ExecuteNonQuery();
                        comm3.ExecuteNonQuery();
                        comm4.ExecuteNonQuery();
                        comm5.ExecuteNonQuery();
                        con2.Close();
                    }
                }

            }

0 个答案:

没有答案