Solved

statement inserts 2 DB records instead of 1

Posted on 2009-07-06
7
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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