We help IT Professionals succeed at work.
Get Started

Insert value of fileUpload control into SQL column

badstyle
badstyle asked
on
682 Views
Last Modified: 2013-12-25
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!


B
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
            Next
 
            If fileOK Then
                Try
                    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
                    objCommand.Parameters.Add(objUserName)
 
                    Dim objUserImage As New System.Data.SqlClient.SqlParameter("@UserImage", System.Data.SqlDbType.NVarChar)
                    objUserImage.Value = userImage
                    objCommand.Parameters.Add(objUserImage)
 
                    Try
                        objConn.Open()
                        objCommand.ExecuteNonQuery()
                        MsgBox("Record Inserted")
                    Catch exc As System.Exception
                        MsgBox(exc.Message())
                    Finally
                        objConn.Close()
                    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:" & _
                       fileExtension
 
 
 
                Catch ex As Exception
                    lbl_image.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
                lbl_image.Text = "You have not specified a file."
            End If
        End If
 
        Page.MaintainScrollPositionOnPostBack = True
 
    End Sub

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
Unlock 2 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE