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
------------------------------- 2 1215
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)
"Adotek" <ad****@adotek.co.uk> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... 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 -------------------------------
Thanks for your reply.
Agreed, solution 1 sounds very lame! :)
Do you have any examples for 2 & 3? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Scott |
last post by:
I have a clickable graph that resides on page 1. If user clicks a data point
on the graph, the page runs again yeilding a 2nd graph that shows a more
detailed graph.
Problem is, I have a...
|
by: Andrew Alger |
last post by:
ok i have two forms. Customer.aspx and Parent_Searh.aspx. There is a button
on Customer.aspx that when executed runs javascript code to open up
parent_search as a popup. After the user searches...
|
by: ppatel |
last post by:
Problem
I have a problem with web image button control click event. The click event does not get trigger until it has not been clicked once or page refresh occures(which is fine).
When click...
|
by: Steve Wark |
last post by:
I have created a ASP.NET application and created two forms within the
application (Webform1.aspx & Webform2.aspx). On the first form I have
placed a textbox (TextBox1) and a button, which when...
|
by: PK9 |
last post by:
I'm having an issue with the "Refresh" of an asp.net page. The refresh is
actually calling my last onClick event. I thought that asp.net was supposed
to be stateless in that it shouldn't...
|
by: Jim Little |
last post by:
Hello,
I'm driving myself crazy either because I'm missing something about ASP.NET,
or what I'm trying to do simply can't be done.
First, I am not using session variables to track state. My...
|
by: IkBenHet |
last post by:
Hello,
Currently I am using a large input form on a website that is based on
ASP and JavaScript.
Depending on the values that are filled in by the user the forms does a
refresh and makes...
|
by: TCORDON |
last post by:
I have a user control that contains 2 image buttons, when you click one of
them, both must change the image source, the thing is that the first time
you click any one of them the page appears to do...
|
by: Juan Romero |
last post by:
Hey guys, please HELP
I am going nuts with the datagrid control.
I cannot get the damn control to refresh. I am using soap to get information
from a web service. I have an XML writer output...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |