首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

周末交作业了,50分“未将对象引用设置到对象的实例”

2012-01-06 
周末交作业了,50分求助:“未将对象引用设置到对象的实例”参考了别人的一个例子,向Sql数据库中插入图片时,出

周末交作业了,50分求助:“未将对象引用设置到对象的实例”
参考了别人的一个例子,向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();
                }

        }
}
小弟是初学者,这是我的实习作业,周末就要交了,恳求大哥们赐教,不盛感激。

[解决办法]
你的Up_file的文件在哪里附加的?没有文件,你那句当然出错
[解决办法]
CmdObj .CommandText = strSql.ToString();
CmdObj .Parameters.Add( "@img ", System.Data.SqlDbType.Image);
CmdObj .Parameters[ "@img "].Value = FileByteArray;

[解决办法]
Up_file 未有值
[解决办法]
<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使用服务器的路径地址
}
[解决办法]
fileLength = Up_file.PostedFile.ContentLength;
--------------------------------
Up_file.PostedFile=null
当然会报错

检查你的控件是否已经有值

热点排行