Hi,

I'm trying to upload multiple image to folder and insert the image information into a database. I have been successful in being able to upload the images to the folder (thanks to a tutorial online) but when I try to add the code to insert the image information to a database it just seems to be ignored. Here's my code:

Protected Sub btnUploadAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUploadAll.Click
        Try
            ' Get the HttpFileCollection
            Dim hfc As HttpFileCollection = Request.Files

            For i As Integer = 0 To hfc.Count - 1
                Dim hpf As HttpPostedFile = hfc(i)
                If hpf.ContentLength > 0 Then
                    hpf.SaveAs(Server.MapPath("images\StudStock") & "\" & System.IO.Path.GetFileName(hpf.FileName))

                    If Session("RefNO") = "" Then

                        generateRefNo()

                    End If

                    Dim myConnection As SqlConnection = New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
                    Dim myCommand As SqlCommand = New SqlCommand("Insert_Stud_Stock_Images", myConnection)
                    ' Mark the Command as a SPROC
                    myCommand.CommandType = CommandType.StoredProcedure
                    ' Add Parameters to SPROC

                    '@ImageName varchar(25)
                    Dim parameterImageName As SqlParameter = New SqlParameter("@ImageName", SqlDbType.VarChar, 100)
                    parameterImageName.Value = hpf.FileName
                    myCommand.Parameters.Add(parameterImageName)

                    '@ImageSize varchar(25)
                    Dim parameterImageSize As SqlParameter = New SqlParameter("@ImageSize", SqlDbType.VarChar, 100)
                    parameterImageSize.Value = hpf.ContentLength
                    myCommand.Parameters.Add(parameterImageSize)

                    '@ImageType varchar(25)
                    Dim parameterImageType As SqlParameter = New SqlParameter("@ImageType", SqlDbType.VarChar, 100)
                    parameterImageType.Value = hpf.ContentType
                    myCommand.Parameters.Add(parameterImageType)

                    '@RefNo varchar(25)
                    Dim parameterRefNo As SqlParameter = New SqlParameter("@RefNo", SqlDbType.VarChar, 25)
                    parameterRefNo.Value = Session("RefNO")
                    myCommand.Parameters.Add(parameterRefNo)


                    Response.Write("File: " & hpf.FileName & " Size: " & hpf.ContentLength & " Type: " & hpf.ContentType & " Uploaded Successfully <br>")

                    myConnection.Open()
                    myCommand.ExecuteNonQuery()
                    myConnection.Close()

                End If
            Next i
        Catch ex As Exception

        End Try
    End Sub

It still uploads the images to the folder and performs the "Response.Write" but nothing is being inserted into the database and no errors are coming up. Any help would be great.

Aaron

Have you tested your SQL procedure from within SQL to make sure it works?

Yeah it works. Here it is:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[Insert_Stud_Stock_Images]
(

@ImageName varchar(200),
@ImageSize varchar(50),
@ImageType varchar(50),
@RefNo  varchar(25)
)

AS
INSERT INTO [Stud_Stock_Images]

(

    [Image_Name], [Image_Size], [Image_Type], [Ref_No]

)

VALUES

( 

@ImageName,
@ImageSize,
@ImageType,
@RefNo

)

OK I have it working now but it inserts the image information twice into the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.