Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update should not be done when the checkbox is disabled

Tags:

c#

sql

asp.net

I have two pages, one is admin and another one is faculty. Admin allocates subject to faculty. Faculty only want to select there required hours only. My issue is that I don't want to perform update query for disabled check box. I have tried the below code, but it is updating for disabled check box too.

  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();
       }
      } 
     }
like image 824
Ravinther M Avatar asked May 19 '15 07:05

Ravinther M


People also ask

Why can't my checkbox be checked or unchecked?

Do you mean that your checkbox can not be checked or unchecked? Please check: 1)the checkbox's DisplayMode, set it to :DisplayMode.Edit 2)the gallery's DisplayMode, set it to :DisplayMode.Edit //the gallery's mode will affect all the controls inside it.

How to change the display mode of a checkbox in a gallery?

1)the checkbox's DisplayMode, set it to :DisplayMode.Edit 2)the gallery's DisplayMode, set it to :DisplayMode.Edit //the gallery's mode will affect all the controls inside it. What's more, make sure that your sql table has primary key.

Is there a control in front of the checkbox?

Reply MAC9090 Regular Visitor In response to gabibalaban Mark as New Bookmark Subscribe Mute Subscribe to RSS Feed Permalink Print Email to a Friend Report Inappropriate Content ‎03-04-202009:24 AM Thanks @gabibalaban. No there are no other controls in front of the checkbox.

What is the difference between patchonuncheck and updateif?

OnUncheck- UpdateIf('[dbo].[Pending]'; ID=ThisItem.ID, {Verified: 0}) This is assuming there is an ID field in your data source that uniquely identified the record. If not, substitute with the appropriate identifier field name. UpdateIf is much more reliable than Patch.


2 Answers

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

Second, do not see anywhere where you check for .Enabled == False This may be for what you are looking.

like image 145
PCPGMR Avatar answered Sep 19 '22 11:09

PCPGMR


According to your code if check box is disabled you perform one query and if check box enabled you perform another query

if (aa.Enabled==false)
{

}
else{

}

if you don't want to perform on disabled check boxes remove below part

if (aa.Enabled==false)
{

}
like image 21
user1659922 Avatar answered Sep 21 '22 11:09

user1659922