参考了别人的一个例子,向Sql数据库中插入图片时,出现:“未将对象引用设置到对象的实例”,错误在fileLength = Up_file.PostedFile.ContentLength;这句,调试时还出现在 System.NullReferenceException 中第一次偶然出现的“App_Web_5y6xpzfn.dll”类型的异常的说明,以下是原代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class UploadImage : System.Web.UI.Page
{
    protected HtmlInputFile Up_file;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int fileLength ;
        Up_file = new HtmlInputFile();
        fileLength = Up_file.PostedFile.ContentLength;
        
        try
        {
            if (fileLength == 0)
            {
                LB_message.Text = "<b>请你选择你要上传的文件</b>";
            }
            else
            {
                Byte[] FileByteArray = new Byte[fileLength];
                Stream StreamObject = Up_file.PostedFile.InputStream;
                StreamObject.Read(FileByteArray, 0, fileLength);
                SqlConnection Con = new SqlConnection("Data Source=(local);Initial Catalog=xiaoyuan;uid=sa;pwd=sa;");
                string SqlCmd = "insert into z_UserInfo(User_image,User_imageType)values(@User_image,@User_imageType)";
                SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
                CmdObj.Parameters.Add("@User_image", SqlDbType.Binary, fileLength).Value = FileByteArray;
                CmdObj.Parameters.Add("@User_imageType", SqlDbType.VarChar, 50).Value = Up_file.PostedFile.ContentType;
               
                Con.Open();
                CmdObj.ExecuteNonQuery();
                Con.Close();
                LB_message.Text = "<p><b>OK!你已经成功上传你的图片</b>";            }
        }
        catch (Exception ex)
        {
           LB_message.Text = ex.Message.ToString();
        }    }
}
小弟是初学者,这是我的实习作业,周末就要交了,恳求大哥们赐教,不盛感激。

解决方案 »

  1.   

    你的Up_file的文件在哪里附加的?没有文件,你那句当然出错
      

  2.   

    CmdObj .CommandText = strSql.ToString();
                CmdObj .Parameters.Add("@img", System.Data.SqlDbType.Image);
                CmdObj .Parameters["@img"].Value = FileByteArray;
      

  3.   

    Up_file = new HtmlInputFile();
    fileLength = Up_file.PostedFile.ContentLength;Upfile是刚刚创建的,没有Up_file.PostedFile实例哦,当然会报错了。上载的要点:1.<form id="form1" enctype="multipart/form-data" method="post" runat="server" style="height:900px">2. HttpFileCollection files = Request.Files;
      

  4.   

    Up_file是一个HtmlInputFile控件,不是点击它自身的“浏览”就能把文件加到里面吗?
      

  5.   

    protected HtmlInputFile Up_file;//定义一个变量?
    Up_file = new HtmlInputFile();//什么意思?
      

  6.   

    to:pppop3
    因为调试的时候老是说我未将对象实例化,所以加上了Up_file = new HtmlInputFile();,小弟初学者,不知怎么办,请大哥不吝赐教。to:xxoo2007() 
    大哥,能否再说的详细点,我是初学者,特别是第2条的具体用法,不胜感激
      

  7.   

    <form id="form1" enctype="multipart/form-data" method="post" runat="server" style="height:900px">
    <input type="File" name="aa"/>
    </form>这个是前台的,注意enctype属性还有name="aa"都是必须的。后台:
    HttpFileCollection files = Request.Files; //reuquest对象是请求对象,里面带有上载文件的信息
    for (int iFile = 0; iFile < files.Count; iFile++) { //循环处理多个上载的文件
        HttpPostedFile postedFile = files[iFile];
         string fileName;
         string saveName;
        fileName = System.IO.Path.GetFileName(postedFile.FileName); //取得上载文件名称
        if (fileName != "" && postedFile.ContentLength / 1024 <= 5000)
         {
            ....... //处理一下,生成文件名称等等         postedFile.SaveAs(saveName); //实际存盘,savename使用服务器的路径地址
         }
      

  8.   

    fileLength = Up_file.PostedFile.ContentLength;
    --------------------------------
    Up_file.PostedFile=null
    当然会报错检查你的控件是否已经有值 
      

  9.   

    我没看你的代码,就知道那是因为你SQL的原因,或者是查不到数据
      

  10.   

    不妨试试楼上的检查一下是不是sql错误?
      

  11.   

    to:symbol441(≮西门潇洒≯)
    控件是没值,怎么给控件即HtmlInputFile赋值呢?或者得到控件的值
      

  12.   

    to:xxoo2007() 
    试了您的方法,但是说HttpPostedFile postedFile = files[iFile];此句索引值超出范围,不知是怎么回事。
      

  13.   

    谢谢各位大哥的关注,结合大家的讲解与参考书的做法,我的主要问题已解决,最后将HtmlInputFile控件放在PlaceHolder控件里,仍然采用我原来的做法,并让它得到了上传文件的值。当然这个还是不完整的,不过这是另一个问题了,向SQL中插入是实现了。散分了!!!