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

Dani AI

Generated

Brief summary and focused troubleshooting steps for the duplicate-insert symptom seen here: can upload files and confirmed the stored procedure works (as asked), yet each image row is inserted twice. The two highest-probability causes are (A) the button click handler being invoked twice (duplicate server-side wiring or a client-side double-submit) or (B) the insert being called twice because the same request is submitted twice. One immediate red flag in the posted code is the empty Catch — any exception or unexpected flow will be silently swallowed. Add logging or rethrow the exception so you can see what really happens during execution.

Practical diagnostics to run (non-invasive, fast):

  • Log a small, unique message at the top of the upload handler and immediately before the DB call (include timestamp, loop index and filename). This proves whether the handler/loop runs twice for the same file.
  • Use SQL Profiler / Extended Events to watch the stored-proc calls and parameters — that will tell you whether SQL is being called once or twice.
  • Inspect the .aspx markup and code-behind for duplicate wiring: a button with an OnClick attribute plus a method that also uses Handles btnUploadAll.Click (or an AddHandler in Page_Load) can cause the handler to be bound twice. Also check for client scripts that call form.submit() in addition to the normal postback.

Fixes and hardening (recommended):

  • Remove duplicate event wiring (keep either the markup OnClick or the Handles clause, not both).

  • Prevent client double-clicks by disabling the button on first click:

    OnClientClick="this.disabled=true; this.form.submit();"
  • Replace the empty Catch with logging (Trace, file, or your logging framework) or rethrow so errors are visible.

  • Add a defensive DB constraint or IF NOT EXISTS (...) INSERT ... in the stored procedure to guard against duplicates at the storage level.

  • Use Using blocks for connections/commands so resources are always disposed.

Recommended Answers

All 3 Replies

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.