Click here to Skip to main content
16,010,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , i Have a problem with checkboxes. I have 6 columns. Id, Pages, ADD, VIEW, EDIT and DELETE.
The ID containd serial No., Pages containd all the aspx pages in my application. Add, View, Edit, and Delete template fields containd checkboxes. When i will check the checkboxes then Y should be stores in database else N. But it is not occuring. please help.
My database contains
ID    Pages           Add     View    Edit    Delete
1     Checkin         N       NULL    NULL    NULL
2     Checkout        N       NULL    NULL    NULL
3     UserCreation    N       NULL    NULL    NULL
4     AssetType       N       NULL    NULL    NULL
5     VendorDetails   N       NULL    NULL    NULL
6     CustomerDetails N       NULL    NULL    NULL
7     ItemStatus      N       NULL    NULL    NULL
8     ComanyDetails   N       NULL    NULL    NULL
NULL  NULL            NULL    NULL    NULL    NULL

My code is
C#
 protected void Button3_Click(object sender, EventArgs e)
    {

        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{
        //    CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[2].FindControl("CheckBox1");
        //    if (chk != null)
        //    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;

            if (isChecked)
            {

                // if (!Page.IsPostBack)

                string strID = GridView1.Rows[i].Cells[0].Text;
                string Pages = GridView1.Rows[i].Cells[1].Text;
                string status = "Y";

               
                con.Open();
                string query ="Update Role1  set [Add] ='" + status + "' where ID= '" + strID + "'  and Pages= '" + Pages + "'";
                SqlCommand cmd = new SqlCommand(query,con);
               cmd.ExecuteNonQuery();

                GridView1.DataBind();
                con.Close();
            }


            else
            {

                string strID = GridView1.Rows[i].Cells[0].Text;
                string Pages = GridView1.Rows[i].Cells[1].Text;
                string status = "N";


                
                //cmd = new SqlCommand("insert into Role1(Checkin_Add) values(" + status + " where ID= '" + strID + "' and Role_Name='" + strName + "')", con);
              string query = "Update Role1  set [Add] ='" + status + "' where ID= '" + strID + "'  and Pages= '" + Pages + "'";
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                cmd.ExecuteNonQuery();
                
                GridView1.DataBind();
              con.Close();
            }
        }   
    }     
}
Posted
Updated 24-Mar-14 3:28am
v3

1 solution

I think the problem is not around the checkboxes. The problem is if you use in the database table for ID a number column your where condition in your SQL script won't work properly with apostrophe.

You have to find your ID without apostrophe:
C#
string query = "UPDATE Role1 SET [Add] = '" + status + "' WHERE ID = " + strID + " AND Pages = '" + Pages + "'";


By the way SQL script concatenating let SQL injection attack in your code, so you have to use SqlCommand and SqlParameters to defend your code from injection. You can read further about SQL commands and parameters from here[^].
 
Share this answer
 
v2

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