Record Added to Bound Datagrid on Refresh

J

Jim Bayers

I have a datagrid on a form that's bound to a datareader. When I click
on my browser's refresh button, it adds a duplicate record to the bottom
of the datagrid. This record appears in the underlying sql table. Why
would it do this? How do you stop it. I just want the datagrid to
display existing records, not modify the database.


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("MyDSN"))
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
 
E

Eliyahu Goldin

The refresh button always repeat the last post to the server. You can't
change it. You can implement a sort of protection against repeating the same
operation in your code.

Eliyahu
 

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

Top