Asp.Net button event updated again ???? GUID?

The obvious problem here is that a button event is raised during the update, and duplicate messages are created in the database. I read other questions on this site on the same issue.

Apparently, the response to each post creates a GUID and checks if the unique GUID is unique? I am not sure how to implement this.

Does this mean that when updating, he will try to duplicate the message with the same GUID?

How do you implement a GUID in your database? Or, if that is not the answer, what is it?

Thanks!

+1
source share
2 answers

The idea is that you create a unique number for the form, and when you submit the form, you save this unique number in the database in the record that you are editing / creating. Before saving, you check whether this number has already been used, in this case it is a form that was resubmitted during the update.

If you are updating a record, you only need to check if this record has been saved with the same unique number, but if you are adding a new record, you need to check if any other record has this number.

A Guid is a good amount to use, as it is unlikely that you will get a duplicate. The 31-bit random number that the Random class can create is also unlikely to produce duplicates, but 128 bits of Guid make it much more unlikely.

You do not need to create a Guid value in the database, just use Guid.NewGuid() in the code that initializes the form. You can put Guid in a hidden field on the form. In the database, you only need a field in which you can store the Guid value, or a Guid data type, if available, or just a text field large enough to store the Guid text view.

You can use the ToString method to get a string representation of the Guid value (so you can put it on the form). Using id.ToString("N") gives the most compact format, i.e. 32 hexadecimal digits without separators. Using id.ToString("B") gives the more recognizable format "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}". To return a Guid from a string (of any format), you simply use new Guid(str) .

+1
source

The RefreshValidator control is used here. Just drop it on your page and mark Page.IsValid before saving to the database. You can add an error message, for example, other validators, or catch the Refreshed event if you want to do something special. Since it is a validator, GridViews, etc. Already pay attention to this - with the exception of the actions "Delete" or "Cancel" (yes, I have a custom GridView that also solves ...)

The code is pretty simple - save the GUID in ControlState and a copy in the session. When downloading, compare 2. If they do not match, then this is an update. Rinse, repeat and create a new GUID and get started.

 ''' <summary> ''' A validator control that detects if the page has been refreshed ''' </summary> ''' <remarks>If <see cref="SessionState.HttpSessionState" /> is not available or is reset, validator will return Valid</remarks> Public Class RefreshValidator Inherits BaseValidator Private isRefreshed As Boolean Protected Overrides Sub OnInit(ByVal e As System.EventArgs) MyBase.OnInit(e) Page.RegisterRequiresControlState(Me) End Sub Protected Overrides Function SaveControlState() As Object Dim obj As Object = MyBase.SaveControlState() Return New System.Web.UI.Pair(_pageHashValue, obj) End Function Protected Overrides Sub LoadControlState(ByVal savedState As Object) Dim pair As System.Web.UI.Pair = TryCast(savedState, System.Web.UI.Pair) If pair IsNot Nothing Then _pageHashValue = TryCast(pair.First, String) MyBase.LoadControlState(pair.Second) Else MyBase.LoadControlState(savedState) End If End Sub Private _pageHashValue As String Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) MyBase.OnLoad(e) If HttpContext.Current Is Nothing OrElse HttpContext.Current.Session Is Nothing Then isRefreshed = False Return End If ' Get hash value from session Dim currHashValue As String = CType(HttpContext.Current.Session(Me.UniqueID & ":pageHashValue"), String) If _pageHashValue Is Nothing OrElse currHashValue Is Nothing Then ' No page hash value - must be first render ' No current hash value. Session reset? isRefreshed = False ElseIf currHashValue = _pageHashValue Then ' Everything OK isRefreshed = False Else ' Was refreshed isRefreshed = True End If ' Build new values for form hash Dim newHashValue As String = Guid.NewGuid().ToString() _pageHashValue = newHashValue HttpContext.Current.Session(Me.UniqueID & ":pageHashValue") = newHashValue End Sub Protected Overrides Function ControlPropertiesValid() As Boolean Return True End Function Protected Overrides Function EvaluateIsValid() As Boolean If isRefreshed Then OnRefreshed(EventArgs.Empty) Return Not isRefreshed End Function Protected Overridable Sub OnRefreshed(ByVal e As EventArgs) RaiseEvent Refreshed(Me, e) End Sub ''' <summary> ''' Fires when page is detected as a refresh ''' </summary> Public Event Refreshed As EventHandler End Class 
+1
source

All Articles