473,473 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why does refresh on browser break this code?

JBD
Hi, I have a button that fires the below sub routine.
This works fine if the user clicks the button as they are
supposed to, even if they click it repeatedly. However,
if you click "refresh" on the browswer, then it
duplicates records. I believe this is happening because
somehow it isnt doing the first part of my code which is
to to an archive update.

I can't for the life of me figure out why this happens
only when you click refresh on the browser. Like I said,
this error wont happen from repeatedly clicking the
button. What could be going on here? please help, thanks:

Sub Button1_Click_1(ByVal sender As Object, ByVal e As
EventArgs)

userID = lbluserid.Text
Dim rs As String = txtNewRs.Text
Dim tbox As TextBox
Dim tlabel As Label
Dim tcheckbox As CheckBox
Dim fldVid, fldSeaid As String
Dim fldDate As Date
Dim FldArriveon, intavail, fldRoIdno, fldAllot, fldbook,
fldAvail, fldMinStay, fldCutoff As Integer
Dim fldNet As Decimal
Dim ky As Integer
Dim itm As DataListItem
Dim dt As DateTime

'update days in season and set archive to 1 and date to
now.
MyConnection.Open()
' Start a local transaction.
Dim myTrans As SqlTransaction =
MyConnection.BeginTransaction()
' Enlist the command in the current transaction.
Dim sql As String

Dim comm As SqlCommand = MyConnection.CreateCommand()

Try
For Each itm In DataList1.Items
comm = New SqlCommand
comm.Connection = MyConnection

ky = CInt(DataList1.DataKeys(itm.ItemIndex))
comm.CommandText = "UPDATE mytable SET
fldArchive=1,fldDateTime='" & Now & "' Where fldxrec = "
& ky
comm.Transaction = myTrans
comm.ExecuteNonQuery()

Next

'insert new records

For Each itm In DataList1.Items

ky = CInt(DataList1.DataKeys(itm.ItemIndex))
tlabel = CType(itm.FindControl("lblDate"), Label)
fldDate = tlabel.Text

tbox = CType(itm.FindControl("txtNet"), TextBox)
fldNet = tbox.Text
tbox = CType(itm.FindControl("txtAllot"), TextBox)
fldAllot = tbox.Text
tlabel = CType(itm.FindControl("lblBook"), Label)
fldbook = tlabel.Text
tlabel = CType(itm.FindControl("lblAvail"), Label)
fldAvail = tlabel.Text
tbox = CType(itm.FindControl("txtMinStay"), TextBox)
fldMinStay = tbox.Text
tbox = CType(itm.FindControl("txtCutoff"), TextBox)
fldCutoff = tbox.Text
tcheckbox = CType(itm.FindControl("CbArriveOn"),
CheckBox)
If tcheckbox.Checked = True Then
FldArriveon = 1
Else
FldArriveon = 0
End If

intavail = fldAllot - fldbook
If intavail < 0 Then
intavail = 0
End If
comm = New SqlCommand
comm.CommandText = "Insert into mytable (fldSeaId,
fldNet, fldAllotment, fldbook, fldAvail, fldMinStay,
fldCutOff, fldroidno, fldDate, fldVid, fldArriveon)
values (@Seaid, @fldNet, @fldallot, @fldbook, @fldAvail,
@fldMinStay, @fldCutOff, @roidno, @fldDate, @vid,
@fldArriveon)"
comm.Connection = MyConnection
comm.Transaction = myTrans

comm.Parameters.Add(New SqlParameter("@seaid",
lblRateSet.Text))
comm.Parameters.Add(New SqlParameter("@fldNet", fldNet))
comm.Parameters.Add(New SqlParameter("@fldAllot",
fldAllot))
comm.Parameters.Add(New SqlParameter("@fldbook",
fldbook))
comm.Parameters.Add(New SqlParameter("@fldAvail",
intavail))
comm.Parameters.Add(New SqlParameter("@fldMinStay",
fldMinStay))
comm.Parameters.Add(New SqlParameter("@fldCutOff",
fldCutoff))
comm.Parameters.Add(New SqlParameter("@roidno",
lblsvroomtype.Text))
comm.Parameters.Add(New SqlParameter("@fldDate",
fldDate))
comm.Parameters.Add(New SqlParameter("@vid", userID))
comm.Parameters.Add(New SqlParameter("@fldArriveon",
FldArriveon))

comm.ExecuteNonQuery()
Next
myTrans.Commit()

Catch ex As Exception
Try
myTrans.Rollback()
Catch exc As SqlException
If Not myTrans.Connection Is Nothing Then
Response.Write("An exception of type " & exc.GetType
().ToString() & _
" was encountered while attempting to roll back the
transaction.")
End If
End Try

Response.Write("An exception of type " & ex.GetType
().ToString() & _
"was encountered while inserting the data. Nothing was
written to the dbase: " & ex.Message & "")

Finally
MyConnection.Close()
runfirst()
DataBind1()
End Try
End Sub
Nov 18 '05 #1
1 1500
When user clicks "Refresh" browser sends exactly the same request to the
server as it sent first time.

So the server can not distinguish them (unless you are using special tricks,
which are usually hard to implement ) and executes in exactly the same way
as it did first time.
So here is my "Rule of Thumb". All Action requests should end up with
Response.Redirect.

Use Response.Redirect( Request.RawURL);
George.
"JBD" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hi, I have a button that fires the below sub routine.
This works fine if the user clicks the button as they are
supposed to, even if they click it repeatedly. However,
if you click "refresh" on the browswer, then it
duplicates records. I believe this is happening because
somehow it isnt doing the first part of my code which is
to to an archive update.

I can't for the life of me figure out why this happens
only when you click refresh on the browser. Like I said,
this error wont happen from repeatedly clicking the
button. What could be going on here? please help, thanks:

Sub Button1_Click_1(ByVal sender As Object, ByVal e As
EventArgs)

userID = lbluserid.Text
Dim rs As String = txtNewRs.Text
Dim tbox As TextBox
Dim tlabel As Label
Dim tcheckbox As CheckBox
Dim fldVid, fldSeaid As String
Dim fldDate As Date
Dim FldArriveon, intavail, fldRoIdno, fldAllot, fldbook,
fldAvail, fldMinStay, fldCutoff As Integer
Dim fldNet As Decimal
Dim ky As Integer
Dim itm As DataListItem
Dim dt As DateTime

'update days in season and set archive to 1 and date to
now.
MyConnection.Open()
' Start a local transaction.
Dim myTrans As SqlTransaction =
MyConnection.BeginTransaction()
' Enlist the command in the current transaction.
Dim sql As String

Dim comm As SqlCommand = MyConnection.CreateCommand()

Try
For Each itm In DataList1.Items
comm = New SqlCommand
comm.Connection = MyConnection

ky = CInt(DataList1.DataKeys(itm.ItemIndex))
comm.CommandText = "UPDATE mytable SET
fldArchive=1,fldDateTime='" & Now & "' Where fldxrec = "
& ky
comm.Transaction = myTrans
comm.ExecuteNonQuery()

Next

'insert new records

For Each itm In DataList1.Items

ky = CInt(DataList1.DataKeys(itm.ItemIndex))
tlabel = CType(itm.FindControl("lblDate"), Label)
fldDate = tlabel.Text

tbox = CType(itm.FindControl("txtNet"), TextBox)
fldNet = tbox.Text
tbox = CType(itm.FindControl("txtAllot"), TextBox)
fldAllot = tbox.Text
tlabel = CType(itm.FindControl("lblBook"), Label)
fldbook = tlabel.Text
tlabel = CType(itm.FindControl("lblAvail"), Label)
fldAvail = tlabel.Text
tbox = CType(itm.FindControl("txtMinStay"), TextBox)
fldMinStay = tbox.Text
tbox = CType(itm.FindControl("txtCutoff"), TextBox)
fldCutoff = tbox.Text
tcheckbox = CType(itm.FindControl("CbArriveOn"),
CheckBox)
If tcheckbox.Checked = True Then
FldArriveon = 1
Else
FldArriveon = 0
End If

intavail = fldAllot - fldbook
If intavail < 0 Then
intavail = 0
End If
comm = New SqlCommand
comm.CommandText = "Insert into mytable (fldSeaId,
fldNet, fldAllotment, fldbook, fldAvail, fldMinStay,
fldCutOff, fldroidno, fldDate, fldVid, fldArriveon)
values (@Seaid, @fldNet, @fldallot, @fldbook, @fldAvail,
@fldMinStay, @fldCutOff, @roidno, @fldDate, @vid,
@fldArriveon)"
comm.Connection = MyConnection
comm.Transaction = myTrans

comm.Parameters.Add(New SqlParameter("@seaid",
lblRateSet.Text))
comm.Parameters.Add(New SqlParameter("@fldNet", fldNet))
comm.Parameters.Add(New SqlParameter("@fldAllot",
fldAllot))
comm.Parameters.Add(New SqlParameter("@fldbook",
fldbook))
comm.Parameters.Add(New SqlParameter("@fldAvail",
intavail))
comm.Parameters.Add(New SqlParameter("@fldMinStay",
fldMinStay))
comm.Parameters.Add(New SqlParameter("@fldCutOff",
fldCutoff))
comm.Parameters.Add(New SqlParameter("@roidno",
lblsvroomtype.Text))
comm.Parameters.Add(New SqlParameter("@fldDate",
fldDate))
comm.Parameters.Add(New SqlParameter("@vid", userID))
comm.Parameters.Add(New SqlParameter("@fldArriveon",
FldArriveon))

comm.ExecuteNonQuery()
Next
myTrans.Commit()

Catch ex As Exception
Try
myTrans.Rollback()
Catch exc As SqlException
If Not myTrans.Connection Is Nothing Then
Response.Write("An exception of type " & exc.GetType
().ToString() & _
" was encountered while attempting to roll back the
transaction.")
End If
End Try

Response.Write("An exception of type " & ex.GetType
().ToString() & _
"was encountered while inserting the data. Nothing was
written to the dbase: " & ex.Message & "")

Finally
MyConnection.Close()
runfirst()
DataBind1()
End Try
End Sub

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
When we refresh the page (F5, or icon in browser), it will first trigger ONUNLOAD event and then trigger ONLOAD event. When we close the browser (X on right top icon), it will trigger ONUNLOAD...
4
by: John | last post by:
I get the feeling this is a pretty classic problem, but I'm a bit of an uber newber. Apologies! Products page, user enters a quantity and clicks one of my "Add to Cart" buttons, which bubbles up...
2
by: Mike | last post by:
oh my pages i have an option to allow user to refresh and a text box for them to enter in 10, 20 , 4 and i break it down to seconds. but then the page actaully refreshs the data is not refreshing...
9
by: Rea | last post by:
Hi eb I set some 'Stop' statements and also visual breakpoints in asp code (vbscript). I am doing that in Microsoft Script debugger. Than I refresh the original page and expect execution to halt...
8
by: Jason S | last post by:
Hi, is there any way of getting my VB (6.0) program to automatically 'Refresh' an IE window that might be active (window status not applicable). It needs to be able to determine which active IE...
48
by: Nathan Sokalski | last post by:
Ever since I found out that they didn't give us a way to install both IE6 and IE7 on the same machine, I have been more frustrated and annoyed with Microsoft than I ever have been with any company...
11
pbmods
by: pbmods | last post by:
A somewhat obscure hack has emerged recently that is an offshoot of the now-infamous XSS. It is known as Cross-Site Request Forgery, or XSRF for short. XSRF is a form of temporary identity theft...
10
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
1
by: sva0008 | last post by:
i have a auto suggest script that does not work in firefox , works great on IE. /******************************************************* AutoSuggest - a javascript automatic text input...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.