Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button3_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)row.FindControl("chk1");

        string empname = row.Cells[1].Text;
        string leaveid = row.Cells[2].Text;
        if (chk.Checked)
        {
            try
            {
                //Create sql connection and command
                string strConnect;
                strConnect = "Data Source=BALA;Initial Catalog=employees;Persist Security Info=True;User ID=sa;Password=mips123";
                SqlConnection Connection = new SqlConnection(strConnect);
                string strUpdate = "Update Leave set status = 'Approved' WHERE (LeaveID =" + leaveid + ") AND (EmpID = (Select EmpID from Emp where (Empname='" + empname + "')))";

                SqlCommand command = new SqlCommand(strUpdate, Connection);

                Connection.Open();
                command.ExecuteNonQuery();
                Connection.Close();
                Getuser1();

            }
            catch (Exception exc)
            {

            }
        }
        else
        {
            Label2.Text = "Please select atlease one record";
        }
    }
}

Its always calls Else part in Button3_Click bcoz if i press button3 the page was post back that time checkbox unchecked ,so plz help me friendz
Posted
Updated 30-Jan-13 18:44pm
v3
Comments
[no name] 30-Jan-13 8:19am    
cn u post ur aspx file code ??

You have made a very simple error in your logic. Try this.
The problem is that if any row doesn't have the checkbox checked you are displaying the label instead of if none are checked. I've removed the code that isn't relevant to the answer.
C#
protected void Button3_Click(object sender, EventArgs e)
{
   bool isAnythingChecked = false;

   foreach (GridViewRow row in GridView1.Rows)
   {
      CheckBox chk = (CheckBox)row.FindControl("chk1");
      if (chk != null && chk.Checked)
      {
         isAnythingChecked = true;
         // Do your stuff
      }
   }
    
   if (!isAnythingChecked)
   {
      Label2.Text = "Please select atlease one record";
   } 

}
 
Share this answer
 
v3
Comments
Ankur\m/ 31-Jan-13 0:38am    
Though you are right about the label display logic, that wasn't the question at all. The question is why is the if condition never gets called. And the reason is very simple - the condition is never satisfied or true.

I don't know why the answers below were down-voted by a platinum member. :|
fjdiewornncalwe 31-Jan-13 9:39am    
I believe I am correct. The reason it appears that the if never gets called is that any one row that is not checked is causing the label to be populated. It doesn't mean the if wasn't firing, it simply means that there was an appearance to the UI that it was not.
Ankur\m/ 31-Jan-13 22:28pm    
Oh I see your point. You mean even if one checkbox is checked, the user will see the Label text which is what makes him say the if condition is not executed.
I was thinking from a developers perspective. I would have checked database for the records and would have put a debugger on the conditional statements.

I will give it a 4! :)
Because chk.Checked is always false.
Make sure you are checking at least one checkbox. And if it's still the same, put a debugger and see if you are getting the correct control.
 
Share this answer
 
Check whether checkbox "chk1" is not checked
 
Share this answer
 
v2
hi dear,
because when you click button3, page is postbacked.
at a time your grid view bind again, so it is reset to first if any state is not managed.
so even you have checked checkbox, after postback it will unchecked.
so every time your else part is executed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900