How to Prevent Datagrid Repopulating on Refresh?

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
 
G

Greg Burns

This doesn't make sense to me. A page refresh will cause IsPostBack to
false again for sure. But even so, you are rebinding the datagrid, which
clears it to start with (I thought).

Dumb question, but are these new records duplicated in the datasource?

Also, why use NextResult instead of just retrieving parameters if all you
are returning are scalar values? (Granted, you will have to close the
datareader prior to accessing the parameters.)

Greg
 
J

Jim Bayers

But even so, you are rebinding the datagrid, which
clears it to start with (I thought)

I thought so to. Weird thing is, when it duplicates the records, they
are added to the database! I'm going to stop this by removing 'insert'
permissions on the table. I didn't think a datareader could insert
records.

Could it be that I've set something on that I shouldn't have? I can't
imagine what that would be.

NextResult is working for me.
 
E

ESPN Lover

This has happened to me numerous times. Each time what it took to solve it
was to set the DataGrid's EnableViewState property to false. Otherwise the
data is stored in the Viewstate field (which can be very expensive in terms
of HTML response size) and then is repopulated upon postback. It then
sounds like you're rebinding your data to the DG
 
G

Greg Burns

A datareader can't, but your sproc can...

Greg

Jim Bayers said:
I thought so to. Weird thing is, when it duplicates the records, they
are added to the database! I'm going to stop this by removing 'insert'
permissions on the table. I didn't think a datareader could insert
records.

Could it be that I've set something on that I shouldn't have? I can't
imagine what that would be.

NextResult is working for me.
 
G

Greg Burns

Turning off viewstate on a datagrid can cause trouble if you're not careful.
A lot of the event processing (if your using them) will no longer work.

My .02
Greg
 

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,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top