怎么会报错?晕了。在线等待

tommerxx 2003-10-18 12:06:31
我参照孟子E章提供的http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506编写如下代码:

Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream
' 获得图片的大小
intImageSize = photos.PostedFile.ContentLength
' 获得图片类型
strImageType = photos.PostedFile.ContentType
'读取图片
ImageStream = PersonImage.PostedFile.InputStream
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

Dim photoConn As New SqlConnection()
photoConn.ConnectionString = "Data Source=Localhost; Initial Catalog=userinfo; user id=sa; password=sa;"
photoConn.Open()

Dim myCommand As New SqlCommand("photo", photoConn)
myCommand.CommandType = CommandType.StoredProcedure

Dim thephotos As New SqlParameter("@photos", SqlDbType.Image)
thephotos.Value = ImageContent
myCommand.Parameters.Add(thephotos)

Dim thephototype As New SqlParameter("@phototype", SqlDbType.VarChar, 255)
thephototype.Value = strImageType
myCommand.Parameters.Add(thephototype)

Try
myCommand.ExecuteNonQuery()
Response.Redirect("myclassphoto.aspx?classid=" & Request("classid"))
Catch SQLexc As SqlException
Response.Write("添加失败,原因:" & SQLexc.ToString())
End Try

photoConn.Close()

运行时报错:

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 284: Dim ImageStream As Stream
行 285: ' 获得图片的大小
行 286: intImageSize = photos.PostedFile.ContentLength (此行红色)
行 287: ' 获得图片类型
行 288: strImageType = photos.PostedFile.ContentType

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
myguestbook.myclassdetail.Button2_ServerClick(Object sender, EventArgs e) in D:\myguestbook\myguestbook\myclasswords.aspx.vb:286
System.Web.UI.HtmlControls.HtmlButton.OnServerClick(EventArgs e) +108
System.Web.UI.HtmlControls.HtmlButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1263

这是什么原因?望各位高手予以指点。
...全文
41 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-10-18
  • 打赏
  • 举报
回复
<form runat=server enctype="multipart/form-data">
<input type="file" id="photos" runat=server />
...
wolve 2003-10-18
  • 打赏
  • 举报
回复
If Not Me.Request.Files.Count > 0 Then
Return
End If


Dim file As HttpPostedFile = Me.Request.Files(0)
If file Is Nothing Or file.ContentLength > FileManager.MAX_FILE_LENGTH Then
Return
End If
Dim buffer(FileManager.MAX_FILE_LENGTH) As Byte
file.InputStream.Read(buffer, 0, buffer.Length)
Dim conn As SqlClient.SqlConnection = ConnectionFactory.GetConnection()
Dim name() As String = file.FileName.Split("\")
conn.Open()
FileManager.SaveFileToDB(conn, buffer, file.ContentType, TextBox1.Text, TextBox2.Text)
conn.Dispose()
file = Nothing
buffer = Nothing

先判断一下有没有上传文件。
coldice 2003-10-18
  • 打赏
  • 举报
回复
你必须的在aspx页面中定义一个ID为photos的上传文件组件

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧