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
-------------------------------
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
-------------------------------