views:

417

answers:

1

i need to upload and display images to and from database. i have written this code for uploading and it uploads fine. except 1 problem. It crashes when i dont select an image. can someone help me fix it for null value? also how do you display an image in IE?

code for inserting image -

Dim imageInfo As FileInfo = Nothing
Dim data() As Byte = Nothing
imageInfo = New FileInfo(Me.UploadLogo.Value.Trim())
Dim imagestream As FileStream = New FileStream(imageInfo.ToString, FileMode.Open)

if name_id > 0
    ReDim data(imagestream.Length - 1)
    imagestream.Read(data, 0, imagestream.Length)
    imagestream.Close()
    Sqlstr = "UPDATE logos WITH(ROWLOCK) " & _
             "SET Logo=@Logo,Modified_Date=GETDATE() " & _
             "WHERE ID = " + name_id.ToString + ""
Else
    Sqlstr = "INSERT logos (Logo,Created_Date) " & _
             "VALUES ("@Logo,GETDATE())"
End If

SqlCmd = New SqlCommand(Sqlstr, SqlCnn)
Dim pictureParameter As SqlParameter = Nothing
pictureParameter = New SqlParameter("@Logo", SqlDbType.Image)
pictureParameter.Value = data
SqlCmd.Parameters.Add(pictureParameter)
SqlCmd.ExecuteScalar()

this works fine only if an image is selected, crashes for NULL values. Also please help me with image display. thanks

A: 

To solve your "file not selected problem", you should have an If statement along the lines of:

If Not File.Exists(Me.UploadLogo.Value.Trim())
   ' Exit out or handle no file selected
End If
Nick