Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 30-Jan-13 2:55am
Edited 30-Jan-13 19:44pm
v3
Comments
kshitij_CodeMaster at 30-Jan-13 8:19am
   
cn u post ur aspx file code ??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
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";
   } 
 
}
  Permalink  
v3
Comments
Ankur\m/ at 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. :|
Marcus Kramer at 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/ at 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! :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check whether checkbox "chk1" is not checked
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 Mathew Soji 335
2 BillWoodruff 260
3 OriginalGriff 215
4 Afzaal Ahmad Zeeshan 188
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100