以下错误信息是在进行上传文件的程序中发生的.
源码是:
<FORM ENCTYPE="multipart/form-data" action="add_newpro.aspx" method="post" RUNAT="server" ID="Form1">
cs源码是:
SqlConnection conn=new SqlConnection(path);
//建立HttpPostedFile对象,用于读取图像文件属性
HttpPostedFile UpFile=UP_FILE.PostedFile; 
//建立FileLength变量存储图片的字节大小
FileLength=UpFile.ContentLength; 错误提示如下:
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 151: SqlConnection conn=new SqlConnection(path);
行 152: //建立HttpPostedFile对象,用于读取图像文件属性
行 153: HttpPostedFile UpFile=UP_FILE.PostedFile; 
行 154: //建立FileLength变量存储图片的字节大小
行 155: FileLength=UpFile.ContentLength;  
 源文件: c:\inetpub\wwwroot\yj\manager\add_newpro.aspx.cs    行: 153 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   yj.manager.add_newpro.upMorefile() in c:\inetpub\wwwroot\yj\manager\add_newpro.aspx.cs:153
   yj.manager.add_newpro.Upload_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\yj\manager\add_newpro.aspx.cs:147
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() 

解决方案 »

  1.   

    不要这样写
    HttpPostedFile UpFile=UP_FILE.PostedFile; 
    直接写呢
    FileLength=UP_FILE.PostedFile.ContentLength;  参考如下,试试
    private void btnsave_Click(object sender, System.EventArgs e)
    {            
        Stream ImageStream;
        string Path=UP_FILE.PostedFile.FileName;// 文件名称
        int Size = UP_FILE.PostedFile.ContentLength; // 文件大小
        string Type = UP_FILE.PostedFile.ContentType; // 文件类型
        ImageStream = UP_FILE.PostedFile.InputStream;
        byte[] Content = new byte[Size];
        int Status = ImageStream.Read(Content, 0, Size);    // 写入数据库
        SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand comm=new SqlCommand("insert into testimage (UserName,Image,Path,Type) values(@UserName,@Image,@Path,@Type)",conn);    comm.CommandType = CommandType.Text;
        comm.Parameters.Add("@UserName", SqlDbType.VarChar, 255).Value = txtUserName.Text;            
        comm.Parameters.Add("@Image", SqlDbType.Image).Value = Content;
        comm.Parameters.Add("@Path", SqlDbType.VarChar, 255).Value = Path;
        comm.Parameters.Add("@Type", SqlDbType.VarChar, 255).Value = Type;    conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
    }
      

  2.   

    请先判断FileUpload.PostedFile != null再进行处理,
    行 153:HttpPostedFile UpFile=UP_FILE.PostedFile;
    行 154://建立FileLength变量存储图片的字节大小
    if(UpFile != null)
    行 155:FileLength=UpFile.ContentLength;
    else
    return ;
    完整例子,
    /// <summary>
    /// 上传文件,如果没有文件返回空引用
    /// </summary>
    /// <param name="filePath">服务器端文件存放路径</param>
    /// <returns>服务器端的文件名称</returns>
    private string  UploadAttachment(string filePath)
    {
    if(FileUpload.PostedFile != null)
    {
    //生成文件路径
    //string filePath = HttpContext.Current.Server.MapPath(".")+"\\Articles\\Data\\Attachments\\";
    //生成限定文件名
    string fileName = FileUpload.PostedFile.FileName;
    //判断当前的服务器端是不是有同名的文件存在
    if(File.Exists(filePath + fileName))
    {
    //文件存在,修改文件名
    fileName =DateTime.Now.ToString("yyyyMMddHHmmss")+ Path.GetExtension(fileName);
    }
    FileUpload.PostedFile.SaveAs(filePath + fileName);
    return fileName;
    }
    return null;
    }