troubleshooting Question

Insert value of fileUpload control into SQL column

Avatar of badstyle
badstyleFlag for United Kingdom of Great Britain and Northern Ireland asked on
Visual Basic ClassicASP.NET
16 Comments2 Solutions683 ViewsLast Modified:
Hello people,

I am looking at adding a facility for an admin user to upload an image of a selected user to the server, the user will be selected via the UserName.

The code below process the file by renaming the file that of the selected user's UserName, reassigns the file extension to the image  then uploads the image to the relative path. It is then supposed to add the file name of the newfile to the UserImage column of the database. It is this database part that appears to fall flat...

The exception I receive is:
ERROR: Format of the initialization string does not conform to specification starting at index 0.

This doesn't stop the file being renamed and uploaded so is clearly something to do with the insert statement I am using.

Any help much appreciated!

Protected Sub fu_button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles fu_button.Click
        Dim fileOK As Boolean = False
        If fu_image.HasFile Then
            Dim selectedUser As String
            selectedUser = gv_members.SelectedDataKey.Value
            Dim fileExtension As String
            fileExtension = System.IO.Path.GetExtension(fu_image.FileName).ToLower()
            Dim userImage As String
            userImage = selectedUser & fileExtension
            Dim allowedExtensions As String() = {".jpg", ".jpeg", ".png", ".gif"}
            For i As Integer = 0 To allowedExtensions.Length - 1
                If fileExtension = allowedExtensions(i) Then
                    fileOK = True
                End If
            If fileOK Then
                    fu_image.SaveAs(Server.MapPath("~\Resources\images\staff\" & userImage)) 'saves image file to destination as "username.ext"
                    Dim connectionString As String = "ConnectionString"
                    Dim objConn As New System.Data.SqlClient.SqlConnection(connectionString)
                    Dim cmdText = "INSERT INTO vw_aspnet_MembershipUsers (UserName, UserImage) VALUES (@UserName, @UserImage) WHERE (UserName = @UserName));"
                    Dim objCommand As New System.Data.SqlClient.SqlCommand(cmdText, objConn)
                    Dim objUserName As New System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.NVarChar)
                    objUserName.Value = selectedUser
                    Dim objUserImage As New System.Data.SqlClient.SqlParameter("@UserImage", System.Data.SqlDbType.NVarChar)
                    objUserImage.Value = userImage
                        MsgBox("Record Inserted")
                    Catch exc As System.Exception
                    End Try
                    lbl_image.Text = "File name: " & _
                       fu_image.PostedFile.FileName & "<br />" & _
                       "File Size: " & _
                       fu_image.PostedFile.ContentLength & _
                       " kb<br />" & _
                       "Content type: " & _
                       fu_image.PostedFile.ContentType & _
                       "<br />" & _
                       "File Extension is:" & _
                Catch ex As Exception
                    lbl_image.Text = "ERROR: " & ex.Message.ToString()
                End Try
                lbl_image.Text = "You have not specified a file."
            End If
        End If
        Page.MaintainScrollPositionOnPostBack = True
    End Sub
Join our community to see this answer!
Unlock 2 Answers and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros