上传图片的困惑!!(初学者不要笑我)
提交以后报错如下:
未将对象引用设置到对象的实例。
说明: 执行当前 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




