How to Prevent Datagrid Repopulating on Refresh? | | |
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 | | | | re: How to Prevent Datagrid Repopulating on Refresh?
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
"Jim Bayers" <spam@spamity.spam> wrote in message
news:Xns95835073BB486spamspamityspam@207.46.248.16 ...[color=blue]
>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[/color] | | | | re: How to Prevent Datagrid Repopulating on Refresh?
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in
news:eFuI4vssEHA.3200@TK2MSFTNGP14.phx.gbl:
[color=blue]
>
>[/color]
[color=blue]
>But even so, you are rebinding the datagrid, which
>clears it to start with (I thought)[/color]
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. | | | | re: How to Prevent Datagrid Repopulating on Refresh?
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
"Jim Bayers" <spam@spamity.spam> wrote in message
news:Xns95835073BB486spamspamityspam@207.46.248.16 ...[color=blue]
> 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[/color] | | | | re: How to Prevent Datagrid Repopulating on Refresh?
A datareader can't, but your sproc can...
Greg
"Jim Bayers" <spam@spamity.spam> wrote in message
news:Xns9583581661A26spamspamityspam@207.46.248.16 ...[color=blue]
> "Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in
> news:eFuI4vssEHA.3200@TK2MSFTNGP14.phx.gbl:
>[color=green]
>>
>>[/color]
>[color=green]
>>But even so, you are rebinding the datagrid, which
>>clears it to start with (I thought)[/color]
>
> 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.[/color] | | | | re: How to Prevent Datagrid Repopulating on Refresh?
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
"ESPN Lover" <espn@lover.com> wrote in message
news:eLlMu6ssEHA.2196@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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
>
>
> "Jim Bayers" <spam@spamity.spam> wrote in message
> news:Xns95835073BB486spamspamityspam@207.46.248.16 ...[color=green]
>> 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[/color]
>
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|