I have an employee directory application that displays employees in a gridview. When a record is selected, a new page opens and displays all info about the employee, including their photo. I have the code working that displays the photos, however, when no photo is present an exception is thrown that "Unable to cast object of type System.DbNull to System.Byte[]". I'm not sure how to test for no photo before trying to write it out.
My code is as follows (with no error trapping):
PrivateSub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load,Me.Load
Dim tempAsString
Dim connPhotoAs System.Data.SqlClient.SqlConnection
Dim connstringAsString
connstring = Web.Configuration.WebConfigurationManager.ConnectionStrings("connPhoto").ConnectionStringconnPhoto =New System.Data.SqlClient.SqlConnection(connstring)
temp = Request.QueryString("id")Dim SqlSelectCommand2As System.Data.SqlClient.SqlCommand
Dim sqlstringAsString
sqlstring ="Select * from dbo.PhotoDir WHERE (CMS_ID = " + temp +")"
SqlSelectCommand2 =New System.Data.SqlClient.SqlCommand(sqlstring, connPhoto)Try
connPhoto.Open()
Dim myDataReaderAs System.Data.SqlClient.SqlDataReadermyDataReader = SqlSelectCommand2.ExecuteReader
DoWhile (myDataReader.Read())
Response.BinaryWrite(myDataReader.Item("ImportedPhoto"))
Loop
connPhoto.Close()
Catch SQLexecAs System.Data.SqlClient.SqlExceptionResponse.Write("Read Failed : " & SQLexec.ToString())
EndTry
EndSubEndClass
If you could point me in the right direction I would appreciate it.
lwhalen618:
when no photo is present an exception isthrown that "Unable to cast object of type System.DbNull toSystem.Byte[]
lwhalen618:
DoWhile (myDataReader.Read())
Response.BinaryWrite(myDataReader.Item("ImportedPhoto"))
Loop
did you try to check for nulls ??
DoWhile (myDataReader.Read())
if Not IsDBNull(myDataReader.Item("ImportedPhoto")) then
Response.BinaryWrite(myDataReader.Item("ImportedPhoto"))
End if
Loop
hope it works... pls let me know
Good Luck./.
|||I did try testing for null but was doing it incorrectly. Your code worked fine. Thanks!
No comments:
Post a Comment