public partial class D2 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void lbtDel_Command(object sender, CommandEventArgs e)
    {
            if (Session["admin"] != null)
            {
                //string userID = Request.QueryString["userID"].ToString();
                string userID = e.CommandArgument.ToString();
                SqlConnection conn = DB.createCon();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "delete from guest where ID='" + userID + "'";
                cmd.Connection = conn;
                if (Convert.ToInt32(cmd.ExecuteNonQuery())> 0)
                {
                    Response.Write("<script>alert('删除成功!');location.href='D2.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('删除失败!');location.href='D2.aspx';</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('对不起,只有管理员有权利删除留言,如果你是管理员,请先登录!');location.href='D1.aspx';</script>");
            }
    }
    protected void lbtReply_Command(object sender, CommandEventArgs e)
    {
        if (Session["admin"] != null)
        {
            //string userID = Request.QueryString["userID"].ToString();
            string userID = e.CommandArgument.ToString();
            Response.Redirect("D5.aspx?userID=" + userID + "");
        }
        else
        {
            Response.Write("<script>alert('对不起,只有管理员有权利回复,如果你是管理员,请先登录!');location.href='D1.aspx';</script>");
        }
    }
}
这是怎么回事,老是显示删除失败

解决方案 »

  1.   

    数据库中有userID对应的数据么?
      

  2.   

    是不是userID里面没有取到值啊,
      

  3.   

    userID是我定义的变量啊,ID才是数据库中定义的
      

  4.   

    userID是我定义的变量啊,ID才是数据库中定义的
      

  5.   

    你应该先调试下userID这个变量有没有值,有值的话,还要看一下这个值在数据库中有没有与对应的值
      

  6.   

    首先检查userID 是否取到了值
    然后检查数据库中的“ID”字段是否是字符型
    另外Convert.ToInt32(cmd.ExecuteNonQuery())这句话我没太看懂,这样的转换有什么意义么?
      

  7.   

    ID是int型的  我不明白 string userID = e.CommandArgument.ToString();
    userID没取到值,Convert.ToInt32(cmd.ExecuteNonQuery())是返回执行SQL语句后影响的行数,Convert.ToInt32也可以不要的
      

  8.   

    using(SqlConnection conn = DB.createCon())
    {
    conn.open();
      SqlCommand cmd = new SqlCommand("delete from guest where ID='" + userID + "'",conn);
      int i=Convert.ToInt32(cmd.ExecuteNonQuery());
     }
      

  9.   

    我怎么觉得是你的命令类型没有设置成text?
      

  10.   

    把userID用params试一下,不要直接加进去
      

  11.   

    string userID = e.CommandArgument.ToString();
    断点设在这,调试看看值
      

  12.   

    是没有这句话的问题么?
    cmd.CommandType = CommandType.Text;
      

  13.   

    感觉你的数据库中没有e.commandargument这样的userid
      

  14.   

    你跟踪一下看看cmd.ExecuteNonQuery()返回的是什么不就完了吗
      

  15.   

    前台有给这个控件lbtDel设置commandargument?
    <linkbutton id="lbtDel" runat="server" commandargument="<%#Eval("UserID")>"
      

  16.   

    如果ID在数据库是int类型吧, 但是你赋了一个string类型的值  肯定不行
      

  17.   

       终于被我弄好了,是CommandArgument的问题
      

  18.   

    int userID =Convert.ToInt32(e.CommandArgument.ToString());
     if(userID==null){
      Response.Write(获取id值失败);
    }else{  SqlConnection conn = DB.createCon();
       ..........
    }