我的数据库有一字段为Bit类型,在GridView显示为check控件,修改过数据后,我不知道怎么把这写到数据库!!
代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string str = "update users set yanzheng='" + (Text)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).ToString() + "'"; 
        sql.DataCom(str);
        str = "select * from users";
        this.GridView1.DataSource = sql.DataSetOne(str);
        this.GridView1.DataBind();
    }
(Text)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).ToString() 不行,哪个高手能解决??

解决方案 »

  1.   

    byte check;
    if (GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).ToString() == "true")
        check = 1;
    else
        check = 0;string str = "update users set yanzheng='" + check.Tostring() + "'"; 
      

  2.   


    //未查有无Convert.ToByte(object),若无
    byte tempByte = (byte)Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).ToString());
    string str = "update users set yanzheng=" + tempByte + "";  
      

  3.   

    可以直接使用Convert.ToByte();
      

  4.   

    将Check列转换成模版。checkBox cb=(CheckBox)GridView1.Rows[e.RowIndex].Cells[10].FindControl("CheckBox1");
    if(cb!=null)
    {
      if(cb.Checked)
    {
       str = "update users set yanzheng='0'" ;
    }
    else
    {
      str="update users set yanzheng='1'" ;
    }}
      

  5.   

    请不要在页面上拼凑Sql字符串 . 如果你没有数据库访问层或ORM代码可以复用最不济你也可以用个数据源控件.
    再不行你要写Sql在页面里也请使用@参数, 则不存在这个问题,因为这本来就是个bool值,是直接插入bit型字段的.你这个代码安全性接近0....随时准备被注入吧~~~~~~~~~~只要保存你的客户端html页面,修改控件值(注入攻击),用base64重新编码一次试图状态,然后向服务器post,你的数据库就完了...