CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

上传图片的困惑!!(初学者不要笑我)

楼主bingbing2(则卷大饼)2003-11-04 00:08:54 在 .NET技术 / ASP.NET 提问

提交以后报错如下:  
  未将对象引用设置到对象的实例。    
  说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。    
   
  异常详细信息:   System.NullReferenceException:   未将对象引用设置到对象的实例。  
   
  源错误:    
   
   
  行   53:   private   void   Button1_Click(object   sender,   System.EventArgs   e)  
  行   54:   {  
  行   55:   Stream   imgdatastream   =   File2.PostedFile.InputStream;  
  行   56:   int   imgdatalen   =   File2.PostedFile.ContentLength;  
  行   57:   string   imgtype   =   File2.PostedFile.ContentType;  
     
  谁知道问题在哪里?  
  代码如下:  
  public   class   WebForm1   :   System.Web.UI.Page  
  {  
  protected   System.Web.UI.WebControls.TextBox   TextBox2;  
  protected   System.Web.UI.HtmlControls.HtmlInputFile   File2;  
  protected   System.Web.UI.WebControls.Button   Button1;  
  protected   System.Web.UI.WebControls.HyperLink   File1;  
   
  private   void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  //   在此处放置用户代码以初始化页面  
  }  
   
  #region   Web   窗体设计器生成的代码  
  override   protected   void   OnInit(EventArgs   e)  
  {  
  //  
  //   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。  
  //  
  InitializeComponent();  
  base.OnInit(e);  
  }  
   
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {          
  this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);  
  this.Load   +=   new   System.EventHandler(this.Page_Load);  
   
  }  
  #endregion  
   
  private   void   Button1_Click(object   sender,   System.EventArgs   e)  
  {  
  Stream   imgdatastream   =   File2.PostedFile.InputStream;  
  int   imgdatalen   =   File2.PostedFile.ContentLength;  
  string   imgtype   =   File2.PostedFile.ContentType;  
  string   imgtitle   =   TextBox2.Text;  
  byte[]   imgdata   =   new   byte[imgdatalen];  
  int   n   =   imgdatastream.Read(imgdata,0,imgdatalen);  
  string   connstr="server=(local);uid=sa;pwd='iloveu';database=myweb";  
   
  SqlConnection   connection   =   new   SqlConnection(connstr);  
   
  SqlCommand   command   =   new   SqlCommand("INSERT   INTO   ImageStore(imgtitle,imgtype,imgdata)   VALUES   (   @imgtitle,   @imgtype,@imgdata   )",   connection   );  
  SqlParameter   paramTitle   =   new   SqlParameter("@imgtitle",   SqlDbType.VarChar,50   );  
  paramTitle.Value   =   imgtitle;  
  command.Parameters.Add(   paramTitle);  
  SqlParameter   paramData   =   new   SqlParameter(   "@imgdata",   SqlDbType.Image   );  
  paramData.Value   =   imgdata;  
  command.Parameters.Add(   paramData   );  
  SqlParameter   paramType   =   new   SqlParameter(   "@imgtype",   SqlDbType.VarChar,50   );  
  paramType.Value   =   imgtype;  
  command.Parameters.Add(   paramType   );  
  connection.Open();  
  int   numRowsAffected   =   command.ExecuteNonQuery();  
  connection.Close();    
  }  
  } 问题点数:0、回复次数:6Top

1 楼saucer(思归)回复于 2003-11-04 02:31:36 得分 0

did   you   forget   to   set   enctype   on   the   form?  
   
  <form   runat=server   enctype="multipart/form-data">  
    <input   type=file   runat=server   id=File2>  
  ....Top

2 楼bingbing2(则卷大饼)回复于 2003-11-04 08:16:54 得分 0

有设置阿  
  <form   id="Form1"   method="post"   runat="server"   enctype="myltipart/formdata">  
  .  
  .  
  <INPUT   id="File2"   type="file"   name="File2"   runat="server">Top

3 楼wwonion(洋葱)回复于 2003-11-04 08:20:58 得分 0

没有声明   File2Top

4 楼bingbing2(则卷大饼)回复于 2003-11-04 08:29:19 得分 0

有啊  
  ....  
  protected   System.Web.UI.WebControls.TextBox   TextBox2;  
  protected   System.Web.UI.HtmlControls.HtmlInputFile   File2;  
  protected   System.Web.UI.WebControls.Button   Button1;  
  ....Top

5 楼chinanewway(新路了无痕)回复于 2003-11-04 09:06:57 得分 0

你必须判断文件是否为空,你试想,若文件为空,你所作的那些又有何用?Top

6 楼bingbing2(则卷大饼)回复于 2003-11-04 09:11:25 得分 0

判断再说了,我只要选择不是空的文件就行了,问题是他说我第一句就错了阿Top

相关问题

  • php保存图片进mysql的困惑
  • 困惑......困惑......
  • 关于变量传递的困惑。。。。。。。。。。。
  • 困惑
  • 困惑!!!!!
  • 困惑。。。
  • 困惑
  • 困惑
  • 困惑
  • 困惑!

关键词

  • 代码
  • 文件
  • imgtype
  • imgdata
  • imgtitle
  • imgdatalen
  • imgdatastream
  • postedfile
  • paramtitle
  • paramdata

得分解答快速导航

  • 帖主:bingbing2

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo