Solved

statement inserts 2 DB records instead of 1

Posted on 2009-07-06
7
155 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

8 Experts available now in Live!

Get 1:1 Help Now