J
Jim Bayers
I have a simple page, all that's on it is a datagrid. The datagrid is
populated on page_load when it is bound to a datareader.
The problem is: when a user refreshes the page, new records are added to
the datagrid, duplicating everything that was there.
How do I prevent this? Should I clear the datagrid at the top of every
page_load? Can I set a variable the first time the page is loaded and
check it for the next refresh?
===========
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim booTest As Boolean = False
If Not Me.IsPostBack Then
intAp = Me.Context.Items.Item("id")
Me.ViewState.Add("id", intAp)
Dim myConnection As SqlConnection = New SqlConnection
(ConfigurationSettings.AppSettings("MyDNS"))
Dim myCommand As SqlCommand = New SqlCommand("spOR_Guests",
myConnection)
myCommand.CommandType = CommandType.StoredProcedure
' @ApID bigint,
myCommand.Parameters.Add("@ApID", SqlDbType.BigInt)
myCommand.Parameters("@ApID").Direction =
ParameterDirection.Input
myCommand.Parameters("@ApID").Value = intAp
Dim dr As SqlDataReader
Try
' get balance
myConnection.Open()
dr = myCommand.ExecuteReader()
' get fee per day
If dr.Read Then
lblFee.Text = Format(dr("Fee"), "currency")
End If
dr.NextResult()
' get balance
If dr.Read Then
lblBalance.Text = Format(dr("Total"), "currency")
End If
dr.NextResult()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Catch exc As SqlException
Response.Write("SQL Error Occured: " & exc.ToString)
Catch exc As Exception
Response.Write("Error Occured: " & exc.ToString)
Finally
If Not dr Is Nothing Then
dr.Close()
End If
myConnection.Close()
End Try
Else
intAp = Me.ViewState.Item("id")
End If
End Sub
populated on page_load when it is bound to a datareader.
The problem is: when a user refreshes the page, new records are added to
the datagrid, duplicating everything that was there.
How do I prevent this? Should I clear the datagrid at the top of every
page_load? Can I set a variable the first time the page is loaded and
check it for the next refresh?
===========
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim booTest As Boolean = False
If Not Me.IsPostBack Then
intAp = Me.Context.Items.Item("id")
Me.ViewState.Add("id", intAp)
Dim myConnection As SqlConnection = New SqlConnection
(ConfigurationSettings.AppSettings("MyDNS"))
Dim myCommand As SqlCommand = New SqlCommand("spOR_Guests",
myConnection)
myCommand.CommandType = CommandType.StoredProcedure
' @ApID bigint,
myCommand.Parameters.Add("@ApID", SqlDbType.BigInt)
myCommand.Parameters("@ApID").Direction =
ParameterDirection.Input
myCommand.Parameters("@ApID").Value = intAp
Dim dr As SqlDataReader
Try
' get balance
myConnection.Open()
dr = myCommand.ExecuteReader()
' get fee per day
If dr.Read Then
lblFee.Text = Format(dr("Fee"), "currency")
End If
dr.NextResult()
' get balance
If dr.Read Then
lblBalance.Text = Format(dr("Total"), "currency")
End If
dr.NextResult()
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Catch exc As SqlException
Response.Write("SQL Error Occured: " & exc.ToString)
Catch exc As Exception
Response.Write("Error Occured: " & exc.ToString)
Finally
If Not dr Is Nothing Then
dr.Close()
End If
myConnection.Close()
End Try
Else
intAp = Me.ViewState.Item("id")
End If
End Sub