怎么会报错?晕了。在线等待
我参照孟子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
这是什么原因?望各位高手予以指点。