Solved

statement inserts 2 DB records instead of 1

Posted on 2009-07-06
7
157 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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