try
    {
        int classID = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex]["cid"].ToString());
        String sqlStr = "delete from nclass where cid=@cid ";
        SqlCommand cmd = new SqlCommand(sqlStr, conn);
        cmd.Parameters.Add("@cid", SqlDbType.Int, 4);
        cmd.Parameters["@cid"].Value = classID;
        conn.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            bind();
        }
        else
        {
            message("服务器忙");
        }
        conn.Close();
    }
    catch
    {
        throw;
    }
我这样删除有一个问题,就是当我点删除的删除成功了他也会执行else里面的代码
后来我打断点之后发现,点一次删除它执行了2次,第一次删除成功后,就接着执行第二次删除,这样当然不会有数据让他删除了,有没有谁遇到过这种问题,有的话说一下怎么解决,我用VS05的时候就没有这个问题,难道是我用08打开的时候转换出了问题?

解决方案 »

  1.   

    int i = 0;
    try
      {
      int classID = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex]["cid"].ToString());
      String sqlStr = "delete from nclass where cid=@cid ";
      SqlCommand cmd = new SqlCommand(sqlStr, conn);
      cmd.Parameters.Add("@cid", SqlDbType.Int, 4);
      cmd.Parameters["@cid"].Value = classID;
      conn.Open();
       i = cmd.ExecuteNonQuery();
        catch
      {
      throw;
      }
      finally
    {
    if (i > 0)
      {
      bind();
      }
      else
      {
      message("服务器忙");
      }
      conn.Close();
      }}
      

  2.   

    不是,我打断点看的情况是,就像我这段代码写在for循环里面一样,执行次数是2次
      

  3.   

    代码放在GridView1_RowDeleting里面在啊
      

  4.   

    按2楼的方法就正确了,但是按道理说不用finally部分这样写也不会错啊,为什么会出现在中情况,难道是vs设置的问题,好了,看看有没有人遇到过这种情况,准备结贴了!~~~