protected void btn_Ok_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cka = (CheckBox)GridView1.Rows[i].FindControl("chkid");
                if (cka.Checked)
                {                    int id = Convert.ToInt32(GridView1.DataKeys[i].Value.ToString());                    string  strsql = "delete from orders where id='" + id + "'";                    string strsql123 = "select pdfPath from orders where id ='" +id+ "'";
                      DataRow drw = DBFun.GetDataRow(strsql123);
                    
                       string filename = drw["pdfPath"].ToString();                       string path = Server.MapPath("..//uploadpic//" + filename);
                       System.IO.File.Delete(path);
                           if (DBFun.ExecuteUpdate(strsql))
                           {
                               ltl_Msg.Text = "删除成功!";
                               DataView dvlist = DBFun.GetDataView("select * From orders order by id desc");
                               AspNetPager1.RecordCount = dvlist.Table.Rows.Count;
                               Session["dvlist"] = dvlist;
                               bindData();
                           }
                          
                     }            }        }是循环删除后台上传文件和记录的一个代码,但是报下面这个错误,

解决方案 »

  1.   

    删除要写物理路径而且是绝对路径,且有权限
    如:这样删除可以File.Delete(@"d:\web\a.txt");这样删除不可以File.Delete(@"http://.../web/a.txt");你这样删除虚拟路径,肯定不行啊
      

  2.   

        protected void btn_Ok_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox cka = (CheckBox)GridView1.Rows[i].FindControl("chkid");
                    if (cka.Checked)
                    {                    int id = Convert.ToInt32(GridView1.DataKeys[i].Value.ToString());                    string  strsql = "delete from orders where id='" + id + "'";                    string strsql123 = "select pdfPath from orders where id ='" +id+ "'";
                          DataRow drw = DBFun.GetDataRow(strsql123);
                        
                           string filename = drw["pdfPath"].ToString();
                           if (Directory.Exists(filename) == false)
                           {
                               if (DBFun.ExecuteUpdate(strsql))
                               {
                                   ltl_Msg.Text = "删除成功!";
                                   DataView dvlist = DBFun.GetDataView("select * From orders order by id desc");
                                   AspNetPager1.RecordCount = dvlist.Table.Rows.Count;
                                   Session["dvlist"] = dvlist;
                                   bindData();
                               }
                           }
                           else
                           {                           string path = Server.MapPath("..//uploadpic//" + filename);
                               System.IO.File.Delete(path);
                               if (DBFun.ExecuteUpdate(strsql))
                               {
                                   ltl_Msg.Text = "删除成功!";
                                   DataView dvlist = DBFun.GetDataView("select * From orders order by id desc");
                                   AspNetPager1.RecordCount = dvlist.Table.Rows.Count;
                                   Session["dvlist"] = dvlist;
                                   bindData();
                               }
                           }
                              
                         }            }        }
    现在加了判断文件,但是能删除,但是我选择多个话,每次都只能删除一条
      

  3.   

    跟踪一下,看看id是不是都是第一个id
      

  4.   


    我是用Server.MapPath("..//uploadpic//" + filename)获取的