复选框有问题。请帮忙。 [英] Problem with checkboxes. please help.

查看:59
本文介绍了复选框有问题。请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的复选框有问题。我有6列。 Id,Pages,ADD,VIEW,EDIT和DELETE。

ID包含序列号,Pages包含我的应用程序中的所有aspx页面。添加,查看,编辑和删除模板字段包含复选框。当我检查复选框时,Y应该是数据库中的存储,否则N.但它没有发生。请帮助。

我的数据库包含

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

 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();
            }
        }   
    }     
}

推荐答案

我认为问题不在于复选框。问题是如果你在数据库表中使用ID数字列你的SQL脚本中的where条件将无法正常使用撇号。



你必须找到你的ID没有撇号:

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:
string query = "UPDATE Role1 SET [Add] = '" + status + "' WHERE ID = " + strID + " AND Pages = '" + Pages + "'";





顺便说一下,SQL脚本连接让SQL注入攻击你的代码,所以你必须使用SqlCommand和SqlParameters来保护你的代码免于注入。您可以从这里 [ ^ ]。


这篇关于复选框有问题。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆