当运行时会有一个这样的错误:未将对象引用设置到对象的实例;
问题代码是:HttpPostedFile UpFile = UP_FILE.PostedFile;    //HttpPostedFile对象,用于读取图象文件属性 
        FileLength = UpFile.ContentLength;红色的那句,不理解什么意思啊,请各位大哥、大姐帮助!

解决方案 »

  1.   

    UpFile为null,应该是UpFile = UP_FILE.PostedFile没找到.
      

  2.   

    <asp:FileUpload  ID="aa" runat ="server" />这是页面的代码,后面的是
    HttpPostedFile upfile =aa.PostedFile;     //HttpPostedFile对象,用于读取图象文件属性
     FileLength = upfile.ContentLength;
    怎么会接收不到图片的属性呢?郁闷,请高手们不吝赐教啊!
      

  3.   

    Int32 FileLength = 0;                                     //记录文件长度变量       
            HttpPostedFile UpFile = UP_FILE.PostedFile;     //HttpPostedFile对象,用于读取图象文件属性 
            FileLength = UpFile.ContentLength;//记录文件长度
            if (FileLength > 0)
            {
                //建立SQL     Server链接       
                SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                String SqlCmd = "";
                SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
                //记录文件长度,读取时使用       
                SqlTransaction trans;
                Con.Open();
                trans = Con.BeginTransaction();
                CmdObj.Transaction = trans;
                try
                {
                    Byte[] FileByteArray = new Byte[FileLength];             //图象文件临时储存Byte数组       
                    Stream StreamObject = UpFile.InputStream;//建立数据流对像   
                    //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度       
                    StreamObject.Read(FileByteArray, 0, FileLength); ;
                    CmdObj.CommandText = "update a set Image=@Image,ContentType=@ContentType,ImageSize=@ImageSize where id=1";
                    CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
                    CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//记录文件类型       
                    //把其它单表数据记录上传       
                    //CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
                    //记录文件长度,读取时使用       
                    CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
                    CmdObj.ExecuteNonQuery();
                    trans.Commit();
                    Con.Close();
                 }
                catch (Exception ex)
                {
                    Page.RegisterStartupScript("Alert", "<script language=javascript>alert('" + ex.Message + "');</script>");
                    trans.Rollback();
                    Con.Close();
                }
    }
      

  4.   

    UpFile.ContentLength就是获取文件的大小
    对象未实例化,只能说明UP_FILE.PostedFile有问题
      

  5.   

    我也就是这么做的,可就是UP_FILE.PostedFile检测不到图片的大小!不过还是谢谢各位啦!