troubleshooting Question

VB.Net Loade and save image jpg file

Avatar of K Feening
K FeeningFlag for Australia asked on
Visual Basic.NETImages and PhotosASP
3 Comments1 Solution13 ViewsLast Modified:

Hi I am trying to upload an image - Logo.jpg  display it in an asp image (Image1) then save it to SQL 2014 Server database file 

File name - LogoFile Fields ID - Type = Integer, Logo - Type = Image

Then when program rerun get the image from database and redisplay in the asp image

I have tried a few web for information sites to Save the data but without the Upload showning the jpg I doint know if it works

Folowing is the code for Uploading the logo

then displaying when program is rerun

and Saving the logo


Protected Sub bLogo_Click(sender As Object, e As EventArgs) Handles bLogo.Click
        Dim FolderPath As String = ConfigurationManager.AppSettings("LogoPath")  
      
If FileUpload1.HasFile Then
            Dim fileDelete = FolderPath & FileUpload1.FileName
            If System.IO.File.Exists(fileDelete) = True Then
                Try
                    System.IO.File.Delete(fileDelete)
                Catch ex As Exception       
                Finally
                End Try
            End If
            Dim filePath As String = FolderPath & FileUpload1.FileName
            FileUpload1.SaveAs(filePath)
            Image1.Attributes.Add("style", "background-repeat: no-repeat; background-image:url(filepath)")   ???
            Image1.ImageUrl = filePath     ??? 
        Else
            Exit Sub
        End If
    End Sub

Open in new window

If Not Me.IsPostBack Then
            Dim conn As New SqlConnection(connString)
            Using sqlCommand = New SqlCommand()
                With sqlCommand
                    If conn.State = ConnectionState.Open Then conn.Close()
                    conn.Open()
                    .CommandText = "SELECT Logo FROM LogoFile where Id=@Id"
                    .Parameters.Clear()
                    .Parameters.AddWithValue("@Id", 1)
                    .Connection = conn
                    .CommandType = CommandType.Text
                    Dim logo = .ExecuteScalar()

                    Image1.ImageUrl = logo ???????
                End With
                sqlCommand.Dispose()
            End Using

            conn.Close()
            conn.Dispose()

        End If

Open in new window

Dim conn As New SqlConnection(connString)
conn.Open()
Dim SqlCommand = New SqlCommand() With SqlCommand    .CommandText = "Insert Into File (Id, Logo)"
   .CommandText = .CommandText & "  Values (@ID, @Logo)"
   .Connection = conn
   .CommandType = CommandType.Text
   .Parameters.Clear()    .Parameters.AddWithValue("@FD", "FD")    Dim url As String = Image1.ImageUrl
                    Dim bytes As Byte() = Convert.FromBase64String(Image1.ImageUrl.Split(","c)(1))
                    .Parameters.AddWithValue("@Logo", bytes)    .ExecuteNonQuery() end with

Open in new window


ASKER CERTIFIED SOLUTION
K Feening
Programmer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 3 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 3 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004