Refresh Carrys out Click Code

A

Adotek

Hi All,

I've built a simple page that populates a datagrid from an access
database. On the page is a form, with a button. When the button is
pressed, the contents of the form are added to the database and the new
data is displayed. Great - Works fine!

BUT...If the user then refreshes the page, the data is AGAIN added to
the data, which appears in the DG following the refresh. Hit refresh
again, and the same happens...over and over.

Below are the relevent chunks of code, can anyone tell me where I've
gone wrong? Or perhaps this is default behaviour that I need to catch
some how?

Thanks!
Simon.

-------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Page.IsPostBack = False Then
'Connect to the database
Dim ConnOLE As OleDbConnection = QuikWebDB.OpenDBConn()
RefreshData(ConnOLE)
'Close database connection
ConnOLE.Dispose()
End If
End Sub
-------------------------------
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
If Page.IsPostBack = True Then
'Connect to the database
Dim ConnOLE As OleDbConnection = QuikWebDB.OpenDBConn()
'Add the new record
Dim dblAmount As Double = CDbl(Me.txtAmount.Text)
If Me.ddlDebitCredit.SelectedValue.ToLower = "debit" Then
dblAmount = dblAmount - (dblAmount * 2)
End If
Dim oledbcmd As New OleDbCommand
oledbcmd.CommandText = "Insert into accounts (description,
transdate, amount, recordedby) VALUES ('" & Me.txtDescription.Text &
"','" & Now() & "'," & dblAmount & ",'" & User.Identity.Name & "');"
oledbcmd.Connection = ConnOLE
oledbcmd.ExecuteNonQuery()
'Display the newly added record
RefreshData(ConnOLE)
'Close database connection
ConnOLE.Dispose()
End If
End Sub
-------------------------------
Private Sub RefreshData(ByVal OLEDBConn As OleDbConnection)
'Get some data
Dim oledbcmd As New OleDbCommand
oledbcmd.CommandText = "SELECT * FROM ACCOUNTS;"
oledbcmd.Connection = OLEDBConn
Dim DSAccounts As New DataSet
Dim DAAccounts As New OleDbDataAdapter(oledbcmd)
Dim introwcount As Integer = DAAccounts.Fill(DSAccounts,
"Accounts")
Me.DGFinance.DataSource = DSAccounts
DataBind()
End Sub
-------------------------------
Dim AccBalance As Double
Private Sub DGFinance_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DGFinance.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem) Then
'Increment the totals as each row is populated
AccBalance += CDbl(e.Item.Cells(2).Text)
e.Item.Cells(3).Text = AccBalance.ToString("C2")
End If
End Sub
End Class
-------------------------------
 
B

Bruce Barker

a button click is a postback. if the user hit refresh the postback happens
again, becuase the browser resends the data (usually after a warning).

you have to code a solution for this. common ones:

1) big warning telling user to not refresh or use back (lame, but all too
common)
2) use a quanity metaphor so that reposts have the same net effect
3) pass a unique tran key to detect dup post, and igone or reprocess

-- bruce (sqlqowrk.com)
 
A

Adotek

Thanks for your reply.

Agreed, solution 1 sounds very lame! :)

Do you have any examples for 2 & 3?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top