Solved

statement inserts 2 DB records instead of 1

Posted on 2009-07-06
7
158 Views
Last Modified: 2012-05-07
The below code demonstrates the way I am uploading a file to my webserver, and attempting to AFTER upload perform a simple INSERT statement into my SQL database table.  Now, this routine works good except for the fact I am getting (2) rows added to the database instead of just one.  The file DOES upload, and the INSERT does occur, just inserts 2 records instead of one.  What am I doing wrong?
Dim filepath As String = fileUpEx.PostedFile.FileName
        Dim pat As String = "\\(?:.+)\\(.+)\.(.+)"
        Dim r As Regex = New Regex(pat)
        Dim m As Match = r.Match(filepath)
        Dim file_ext As String = m.Groups(2).Captures(0).ToString()
        Dim filename As String = m.Groups(1).Captures(0).ToString()
        Dim file As String = filename & "." & file_ext
        fileUpEx.PostedFile.SaveAs(Server.MapPath("\productimages\") & file)
        lblStatus.Text = file
 
        Dim connect As SqlConnection = New SqlConnection
        connect.ConnectionString = "<MY CONNECTION>"
        connect.Open()
        Dim strSQLInsert As String
        strSQLInsert = "INSERT INTO Products_Images(ProductID,FileName) values ("
        strSQLInsert = strSQLInsert & "'" & Trim(Request.QueryString("ProductID")) & "',"
        strSQLInsert = strSQLInsert & "'" & Trim(file) & "')"
 
        Dim command1 As New SqlCommand(strSQLInsert, connect)
        Try
            command1.ExecuteNonQuery()
        Finally
            command1.Dispose()
            connect.Close()
            connect = Nothing
        End Try

Open in new window

0
Comment
Question by:chrisryhal
  • 4
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
sunil_mails earned 250 total points
ID: 24784298
ur function is being called twice .. it may be due to page post back

debug using bookmarks
0
 
LVL 7

Expert Comment

by:meispisces
ID: 24784554
Whats the name of function where this code appears / is written?
0
 
LVL 2

Author Comment

by:chrisryhal
ID: 24784631
Its a button click
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 7

Assisted Solution

by:meispisces
meispisces earned 250 total points
ID: 24791395
So just check whether  a button click on HTML side has something like OnClick event and on code side do you have something like Button1_Click  ..... Handles Event Name (where you have a button Click event written)?

If you both mentioned remove anyone of them while keeping the second one in-tact

Hope this helps you!

thanks
0
 
LVL 7

Expert Comment

by:meispisces
ID: 24806162
chrisryhal, is your problem solved?
please reply
Thankx
0
 
LVL 2

Author Closing Comment

by:chrisryhal
ID: 31600114
It was a little of both.  The onclick event was there and also the postback error so I split the points.  Thanks for the pointers
0
 
LVL 7

Expert Comment

by:meispisces
ID: 24820695
most welcome...!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question