日期:2014-05-17  浏览次数:20469 次

HasFile提示未将对象引用设置到对象的实例,怎么解决?
代码如下:
  using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;


public partial class PictureManage_InsertPage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  userIdValue.Text = Membership.GetUser().ProviderUserKey.ToString();
  }
  protected void InsertDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
  {
  e.Values["userID"]=Membership.GetUser().ProviderUserKey;
  e.Values["uploadDate"]=DateTime.Now ;
  }
  protected void InsertDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
  {
  if (e.AffectedRows > 0)
  {
  DataView results = (DataView)maxPictureID.Select(DataSourceSelectArguments.Empty );
  int PictureAddId = Convert.ToInt16(results[0][0]);
  FileUpload imageupload = (FileUpload)InsertDetailsView.FindControl("imageupload");
  if (imageupload.HasFile )
  {
  String path = Server.MapPath("~/Image/");
  imageupload.SaveAs(path + PictureAddId + ".jpg");

  }
  }
  }
}

在浏览器中查看结果提示为:

未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 


行 31: int PictureAddId = Convert.ToInt16(results[0][0]);
行 32: FileUpload imageupload = (FileUpload)InsertDetailsView.FindControl("imageupload");
行 33: if (imageupload.HasFile)
行 34: {
行 35: String path = Server.MapPath("~/image/");
 

源文件: d:\Users\Administrator\Desktop\WebSite2\PictureManage\InsertPage.aspx.cs 行: 33 

堆栈跟踪: 


[NullReferenceException: 未将对象引用设置到对象的实例。]
  PictureManage_InsertPage.InsertDetailsView_ItemInserted(Object sender, DetailsViewInsertedEventArgs e) in d:\Users\Administrator\Desktop\WebSite2\PictureManage\InsertPage.aspx.cs:33
  System.Web.UI.WebControls.DetailsView.OnItemInserted(DetailsViewInsertedEventArgs e) +78
  System.Web.UI.WebControls.DetailsView.HandleInsertCallback(Int32 affectedRows, Exception ex) +69
  System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +130
  System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +278
  System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +632
  System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +88
  System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
  System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +113
  System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
  System.Web.UI.WebControls.LinkButton.OnCommand(CommandEven