SqlCommand scd = new SqlCommand("delete from tb_user where UserName=" + UserName, con);
小弟在编写删除数据库某一行的时候出现的情况,提示要删除的那一行的 UserName 列名无效。
高手解决protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConSQL"]);
        con.Open();
        string UserName = GridView1.DataKeys[e.RowIndex].Value.ToString();
        SqlCommand scd = new SqlCommand("delete from tb_user where UserName=" + UserName, con);
        scd.ExecuteNonQuery();
        this.bind();
    }

解决方案 »

  1.   

    我也是菜鸟.,,应该是你的UserName没有加上引号 SqlCommand scd = new SqlCommand("delete from tb_user where UserName= ' " & UserName &" ' ", con); 
      

  2.   

     UserName名字错了,查看数据库中的列名是否这样的
      

  3.   

    SqlCommand scd = new SqlCommand("delete from tb_user where [UserName]='" + UserName +"'", con); 
      

  4.   

     SqlCommand scd = new SqlCommand("delete from tb_user where UserName=" + UserName, con); 加号后面的UserName如果不是int,就要加单引号另外 确定数据库表中 有UserName列
      

  5.   

    你数据库中字段到底存在不,还是可能就是,net的问题,我也遇到过,你从写一次!