简体   繁体   中英

insert and display image in vb.net from sql server database

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM