Having problims adding a picture files to the database. Bot sure where i am going worng, ignore the commented out lines that was my first attempt. A little help please!

'If the query found that the name had not been entered before add it into the table
        If imgUpload.PostedFile Is Nothing Then

            Label1.Visible = True
            Label1.Text = "No file specified"

            Exit Sub

            'Declaration of variables
            Dim connection As SqlConnection = Nothing
            Dim img As FileUpload = CType(imgUpload, FileUpload)
            Dim imgByte As Byte() = Nothing
            If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
                'To create a PostedFile
                Dim File As HttpPostedFile = img.PostedFile
                'Create byte Array with file len
                imgByte = New Byte(File.ContentLength - 1) {}
                'force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength)
            End If

            'assigns ext to the file name
            Dim ext As String = imgUpload.FileName
            'converts name to lower case
            ext = ext.ToLower

            'Declaration of variables
            'Dim imgType = objFile.PostedFile.ContentType

            'if statement to check extension validation
            If ext = ".jpg" Then
            ElseIf ext = ".bmp" Then
            ElseIf ext = ".gif" Then
            ElseIf ext = "jpg" Then
            ElseIf ext = "bmp" Then
            ElseIf ext = "gif" Then
                Label1.Text = "Only gif, bmp, or jpg format files supported."
                Exit Sub
            End If

            'SQL query to access the table tblUser
            'sql = "INSERT INTO tblUser"
            'sql = sql & " SET UserPic " = "'" &  & "'"
            'sql = sql & "Values('" & objFile.Value & "')"

            'Declaration of variables
            Dim myConnection3 As OleDbConnection
            Dim myCommand3 As OleDbCommand

            'Creates a connection to the database
            myConnection3 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" _
            & "Data Source=" & Server.MapPath("DB\ScrumManagementSystem.accdb") & ";")

            'Opens a connection 

            Dim sql As String = "INSERT INTO tblUser(UserPic) VALUES(@eimg) SELECT @@IDENTITY"
            Dim cmd As SqlCommand = New SqlCommand(sql)
            cmd.Parameters.AddWithValue("@eimg", imgByte)

            'Create the command object to pass in the SQL string
            myCommand3 = New OleDbCommand(sql, myConnection3)

            'closes the connection

            Label1.Visible = True
            Label1.Text = "File uploaded Sucessfully!"
        End If

It is much more efficient (specially using access) to store just the file name & path of your file in the database and retrieve that info to use in loading your file. Images take up larges amounts of space and everytime you edit a record that contains a picture (even if your not changing the picture itself) the database makes a new copy of that record which keep accumalating the size of your database.

This article has been dead for over six months. Start a new discussion instead.