Solved

statement inserts 2 DB records instead of 1

Posted on 2009-07-06
7
154 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now