Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
3.18/5 (3 votes)
See more:
In my web application i am trying to send gridview data in mail based on checkbox and radiobutton selection. Code for the same is as below:

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            string nam = Request.QueryString["name"];
            string type = Request.QueryString["type"];
            string rn = Request.QueryString["code"];
            DataTable dtable = new DataTable();
            dtable.Columns.Add(new DataColumn("date", typeof(DateTime)));
            dtable.Columns.Add(new DataColumn("hf", typeof(float)));
            dtable.Columns.Add(new DataColumn("status", typeof(string)));
            Session["dt"] = dtable;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();
            for (int i = 0; i < final.Rows.Count; i++)
            {
                dtable=(DataTable)Session["dt"];
                GridViewRow row = final.Rows[i];
                CheckBox chk=(CheckBox)row.Cells[3].FindControl("CheckBox1");                
                if (chk.Checked)
                {    
                
                    RadioButtonList rButton = (RadioButtonList)final.Rows[i].Cells[2].FindControl("RadioButtonList1");
                    if (rButton.SelectedValue == "Half")
                    {
                        //update database,fill gridview with data table values
                    }
                    else
                    {
                       //update database,fill gridview with data table values
                    }
                }
                else
                {
                    //Only this code is working
                }
            }            
            Gridview1.visible = true;
        }


In above code i have gridview with name,dates,radiobutton,checkbox.last two controls are templatefields.The problem is checkbox is checked or not is not working(the bold marked)..that is it enters the last 'else' evertime.Any help would be greatly appreciated....
Posted
Updated 18-Mar-13 21:28pm
v7
Comments
Jegan Thiyagesan 18-Mar-13 13:39pm    
Can you try to reduce the unnecessary repeated code, it makes the overall code lengthy, difficult to read the code and definitely makes it hard to spot the problem.

1 solution

Hi,

first of all, it is not an answer but a suggestion, can you refactor your code so that is readable?
for example:

C#
//-----------------------------------------------------------------
CheckBox cBox = (CheckBox)final.Rows[i].Cells[3].FindControl("CheckBox1");
if (cBox != null)
{
    RadioButtonList rButton = (RadioButtonList)final.Rows[i].Cells[2].FindControl("RadioButtonList1");
    if (rButton.SelectedValue == "Half")
    {
        UpdateDataTable(0.5, "Approved", dtable,  nam,  type,  rn,  conn);
    }
    else
    {
        UpdateDataTable(1.0, "Approved", dtable, nam, type, rn, conn);
    }
}
else
{
    UpdateDataTable(0.0, "Disapproved", dtable, nam, type, rn, conn);
}
//----------------------------------------------------------------------

private void UpdateDataTable(float hf, string status, DataTable dTable, string nam, string type, string rn, SqlConnection conn)
{
    DataRow dd = dtable.NewRow();
    dd["date"] = Convert.ToDateTime(final.Rows[i].Cells[1].Text);
    dd["hf"] = hf;
    dd["status"] = "Approved";
    dtable.Rows.Add(dd);
    GridView1.DataSource = dTable;
    GridView1.DataBind();
    SqlCommand cmd = new SqlCommand("insert into leave_rec VALUES('" + nam + "','" + dd["date"] + "','" + dd["hf"] + "','" + type + "','" + status + "')", conn);
    cmd.ExecuteNonQuery();
    calculate_half(type, nam);
    Session["dt"] = dTable;
}


Second, There are few things that I noticed, where you are parsing the wrong type on rButton check:
C#
dd["date"] = final.Rows[i].Cells[1].Text;
dd["hf"] = "1";

you have defined them as DateTime and float, but assigning them string.

Finally, this is where you are having the problem I guess, lines like this,
C#
CheckBox cBox = (CheckBox)final.Rows[i].Cells[3].FindControl("CheckBox1");

RadioButtonList rButton =(RadioButtonList)final.Rows[i].Cells[2].FindControl("RadioButtonList1");

How many controls a cell in a specific row can have in your application
If the cell can have only one control why don't you take that cell as the control, why are you searching for it?
i.e.
C#
CheckBox cBox = (CheckBox)final.Rows[i].Cells[3];


or if you know there is a check box control named "CheckBox1" then look for that control.
i.e.
C#
if (CheckBox1.checked)


I am not sure I answered your question, reducing unnecessary code will help you find the problem faster.


Regards
Jegan
 
Share this answer
 
Comments
Thanks7872 19-Mar-13 0:49am    
the problem is in for loop only.Below is the code i have modified......

for (int i = 0; i < final.Rows.Count; i++)
{
dtable=(DataTable)Session["dt"];
GridViewRow row = final.Rows[i];
CheckBox chk=(CheckBox)row.Cells[3].FindControl("CheckBox1");

if (chk.Checked)
{

RadioButtonList rButton = (RadioButtonList)row.Cells[2].FindControl("RadioButtonList1");
if (rButton.SelectedValue == "Half")
{
DataRow dd = dtable.NewRow();
dd["Date"] = Convert.ToDateTime(row.Cells[1].Text);
dd["Half/Full"] = 0.5;
dd["Status"] = "Approved";
dtable.Rows.Add(dd);
GridView1.DataSource = dtable;
GridView1.DataBind();
SqlCommand cmd = new SqlCommand("insert into leave_rec VALUES('" + nam + "','" + dd["Date"] + "','" + dd["Half/Full"] + "','" + type + "','Approved')", conn);
cmd.ExecuteNonQuery();
calculate_half(type, nam);
Session["dt"] = dtable;
}
else
{
DataRow dd = dtable.NewRow();
dd["Date"] = row.Cells[1].Text;
dd["Half/Full"] =1;
dd["Status"] = "Approved";
dtable.Rows.Add(dd);
GridView1.DataSource = dtable;
GridView1.DataBind();
SqlCommand cmd = new SqlCommand("insert into leave_rec VALUES('" + nam + "','" + dd["Date"] + "','" + dd["Half/Full"] + "','" + type + "','Approved')", conn);
cmd.ExecuteNonQuery();
calculate_full(type, nam);
Session["dt"] = dtable;
}
}
else
{
DataRow dd = dtable.NewRow();
dd["Date"] = Convert.ToDateTime(row.Cells[1].Text);
dd["Half/Full"] = 0;
dd["Status"] = "Disapproved";
dtable.Rows.Add(dd);
GridView1.DataSource = dtable;
GridView1.DataBind();
SqlCommand cmd = new SqlCommand("insert into leave_rec VALUES('" + nam + "','" + dd["Date"] + "','" + dd["Half/Full"] + "','" + type + "','Disapproved')", conn);
cmd.ExecuteNonQuery();
Session["dt"] = dtable;
}
}
in above code,datatable is getting filled and i am getting values in gridview also,but eveytime it is entering the last else,hence its clear that chk.checked condition is not satisfying at all...Please suggest me the way to overcome the same.
Jegan Thiyagesan 19-Mar-13 7:45am    
Stick a break point at the line CheckBox chk=(CheckBox)row.Cells[3].FindControl("CheckBox1");, when the it hits, open the "Autos" window (you can find this window under "Debug" menu "windows" sub menu item) and check the type of the row.Cells[3]. Check does it have control named "Checkbox1".

Then step into using the "F10" key, check the value of the CheckBox chk, look whether it is not null, and finally, if you do find that the check box exists, then look for whether has it "Checked" stte set to true.
Jegan Thiyagesan 19-Mar-13 7:47am    
you still haven't optimized your code as I mentioned earlier.

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