why does refresh on browser break this code?

  • Thread starter Thread starter JBD
  • Start date Start date
J

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
 
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.
 
Back
Top