Update should not be done if the checkbox is disabled

I have two pages, one is admin and the other is faculty. The administrator appoints teachers. The faculty wants to select only the required hours. My problem is that I do not want to check the update box for disabled. I tried the code below, but it is also updated for a disabled checkbox.

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) )))) { SqlCommand comm; CheckBox aa; bool hour1 = (r.FindControl("chkHour1") as CheckBox).Checked; bool hour2 = (r.FindControl("chkHour2") as CheckBox).Checked; bool hour3 = (r.FindControl("chkHour3") as CheckBox).Checked; bool hour4 = (r.FindControl("chkHour4") as CheckBox).Checked; bool hour5 = (r.FindControl("chkHour5") as CheckBox).Checked; string subject1 = ddlsubj.SelectedValue; string subject2 = ddlsubj.SelectedValue; string subject3 = ddlsubj.SelectedValue; string subject4 = ddlsubj.SelectedValue; string subject5 = ddlsubj.SelectedValue; string datedif = r.Cells[0].Text; aa=(CheckBox)sender; con2.Open(); comm = new SqlCommand(); comm.Connection = con2; comm.CommandType = CommandType.Text; if (aa.Enabled==false) { comm = new SqlCommand("UPDATE test SET aa=false when subject1=CASE WHEN (@hour1)= 'false' THEN NULL ELSE @subject1 END, subject2=CASE WHEN (@hour2)= 'false' THEN NULL ELSE @subject2 END, subject3=CASE WHEN (@hour3)= 'false' THEN NULL ELSE @subject3 END, subject4=CASE WHEN (@hour4)= 'false' THEN NULL ELSE @subject4 END, subject5=CASE WHEN (@hour5)='false' THEN NULL ELSE @subject5 END, hour1 = CASE WHEN (@hour1)= 'false' THEN 'false' ELSE 'true' END, hour2 = CASE WHEN (@hour2)= 'false' THEN 'false' ELSE 'true' END, hour3 = CASE WHEN (@hour3)= 'false' THEN 'false' ELSE 'true' END , hour4 = CASE WHEN (@hour4)= 'false' THEN 'false' ELSE 'true' END , hour5 = CASE WHEN (@hour5)= 'false' THEN 'false' ELSE 'true' END where datedif=@datedif ", con2); } else { comm = new SqlCommand("update test set aa=true when subject1=CASE WHEN (@hour1)= 'false' THEN NULL ELSE @subject1 END, subject2=CASE WHEN (@hour2)= 'false' THEN NULL ELSE @subject2 END, subject3=CASE WHEN (@hour3)= 'false' THEN NULL ELSE @subject3 END, subject4=CASE WHEN (@hour4)= 'false' THEN NULL ELSE @subject4 END, subject5=CASE WHEN (@hour5)='false' THEN NULL ELSE @subject5 END, hour1 = CASE WHEN (@hour1)= 'false' THEN 'false' ELSE 'true' END, hour2 = CASE WHEN (@hour2)= 'false' THEN 'false' ELSE 'true' END, hour3 = CASE WHEN (@hour3)= 'false' THEN 'false' ELSE 'true' END , hour4 = CASE WHEN (@hour4)= 'false' THEN 'false' ELSE 'true' END , hour5 = CASE WHEN (@hour5)= 'false' THEN 'false' ELSE 'true' END where datedif=@datedif ", con2); } comm.Parameters.AddWithValue("@subject1",subject1); comm.Parameters.AddWithValue("@subject2", subject2); comm.Parameters.AddWithValue("@subject3", subject3); comm.Parameters.AddWithValue("@subject4", subject4); comm.Parameters.AddWithValue("@subject5", subject5); comm.Parameters.AddWithValue("@hour1", hour1); comm.Parameters.AddWithValue("@datedif", datedif); comm.Parameters.AddWithValue("@hour2", hour2); comm.Parameters.AddWithValue("@hour3", hour3); comm.Parameters.AddWithValue("@hour4", hour4); comm.Parameters.AddWithValue("@hour5", hour5); comm.ExecuteNonQuery(); con2.Close(); } } } 
+6
source share
2 answers

You want to delete the IF statement first, since it is not needed. It checks, as others have said, both true OR (||) false, and always will be true.

Secondly, you do not see the place where you are checking .Enabled == False This may be what you are looking for.

+1
source

According to your code, if the checkbox is disabled, you execute one request, and if the checkbox is enabled, you execute another request

 if (aa.Enabled==false) { } else{ } 

if you do not want to perform with the checkboxes disabled, delete the part below

 if (aa.Enabled==false) { } 
+1
source

Source: https://habr.com/ru/post/987479/


All Articles