ASP.NET: Very simple event handling not working - asp.net

I have an object Order with a simple event,
Public Event ErrorOccurred(ByVal msg As String)
that I raise in the constructor like so when an order cannot be found (along w/setting a boolean error flag:
RaiseEvent ErrorOccurred("This order does not exist in the database.")
[Error] = True
I have a webform subscribed to the order's ErrorOccurred event:
Public WithEvents o As New Order
and I have an error handler method on the form:
Private Sub OnErrorOccurred(ByVal msg As String) Handles o.ErrorOccurred
litMsg.Text = "<p class=""error-confirm"">" & msg & "</p>"
End Sub
When a textbox is changed, it autoposts back to the page and employs the following logic:
Private Sub txtOrderID_TextChanged(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles txtOrderID.TextChanged
If IsNumeric(txtOrderID.Text) Then
If o.OrderID = 0 Then o = New Order(txtOrderID.Text)
If Not o.Error Then
'do stuff'
Else
'error, run error handling'
End If
....
When there is an error (when the Else logic is run), everything performs as expected except the event does not fire. However, since the Error flag is set to true, it means the event MUST have fired, since that line executes AFTER the RaiseEvent line.
I've tried everything I can think of, but I cannot figure out what could be wrong. I have events strewn everywhere in my project and they all work well using virtually the same structure. What could I be doing wrong here?

I would say that since you are raising the event in the constructor, before you even have a reference to the object in your parent class, that you are unable to handle the event. In this case, especially with errors in the constructor, you would probably be much better off throwing an exception than to raise an event. I would be better to throw an exception, because some other code calling your class might not even handle the event, and you would most likely want to know that an error occured. Throwing exceptions is the standard way of letting the calling code know that an error occured. Events are more for optional things that the calling class may want to handle, but that it may also want to ignore.

Related

ASP.NET Confirm Message without OnClientClick/Button

is it possible to raise a confirmation message with yes/no from code behind, without any use of client click?
My problem setup:
A row is going to be deleted from a datagrid
delete function checks if the selected row has dependencies
if the row has children (dependencies) raise a message if the user wants to keep going with the delete process, if not cancel the function.
I can't check the dependencies beforehand to set the javascript confirm on the delete button (performance would die [there are other database issues...]).
I need to raise the message from the function with an return value which the code behind can use. Is there any native way to do this?
Small sample:
Private Sub Delete(ByVal sender As Object, Optional ByVal e As System.EventArgs = Nothing)
//Do check for dependencies...
If intDependencyCounter > 0 Then
//This is where i need my message to be shown
Dim blUserInput As Boolean = True/False //depends on user input
If blUserInput = False Then
//Cancel function...
Else
//Keep going with function...
EndIf
EndIf
End Sub
Hope someone can give me an idea how to solve this.
Regards,
Megajin

ASP.NET sqldatasource.select(arg) gives stackoverflow.exception error

I have a code to read the total number of rows in a SQLDatasource:
Protected Sub DSArticles_Selected(sender As Object, e As SqlDataSourceStatusEventArgs) Handles DSArticles.Selected
Dim args As DataSourceSelectArguments = New DataSourceSelectArguments
Dim dv As DataView = DSArticles.Select(args)
dv.RowFilter = DSArticles.FilterExpression
LblCikkekSzama.Text = dv.Count & " cikk"
End Sub
The browser says the page cannot be viewed. When in debugging mode I get the error:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll.
Any ideas?
Thanks
Sudi
I have a feeling you're causing an infinite loop because in the selected event you're doing Select() which triggers the selected event again, which calls Select again, etc... So this is probably not a good place to do this. Maybe move it to page_load or somewhere else that wouldn't trigger the selected event continuously. Somewhere that makes sense for what you're trying to accomplish.

Is assigning an object to itself a good idea?

I have two classes, RecordSet and Record. RecordSet has a Generic List(Of Record).
I can add objects to the list by calling my RecordSet.AddRecord(ObjRecord) function, which returns RecordSet. When the list has a count of 200, some processing occurs and a new RecordSet object is returned, otherwise itself is returned and the application can carry on adding Record objects to the list.
My concern is that there will be 200 objects of RecordSet until garbage collection does it's sweep. Is this a good idea?
Public Class RecordSet
Private lstRecords As New List(Of Record)
Public Function AddRecord(SomeVariable) AS RecordSet
lstRecords.Add(New Record())
If lstRecords.Count = 200 Then
Me.ProcessTheRecords()
Return New RecordSet()
Else
Return Me
End If
End Function
Private Sub ProcessTheRecords()
'Do stuff in here
End Sub
Private Class Record
Public Sub New()
End Sub
End Class
End Class
Then in my application I call:
Dim objRecordSet AS New RecordSet
For Each VariableName In SomeList
objRecordSet = objRecordSet.AddRecord(VariableName)
Next
'Process the remaining objects in objRecordSet here.
First of all, this is really bad pratice, it's hard to follow the code for someone new and is a potential bug source. Instead of returning urself every time, change your design.
Change your function to this:
Public Sub AddRecord(SomeVariable)
lstRecords.Add(New Record()) <--- should't you be doing something with SomeVariable?!
If lstRecords.Count = 200 Then
Me.ProcessTheRecords()
end if
End Function
Private Sub ProcessTheRecords()
'Do stuff in here
Me.lstRecords.clear()
End Sub
Now AddRecord does exactly what it says it does - it adds a new record and modifies the recordSet. ProcessTheRecords does the processing, as its supposed to do, and if u need to clear the list container - oh well, just clear it.
I strongly recommed to read this wiki article about
Cohesion.
Just as a proposiontion, the AddRecord could be a function of return type Boolean, which indicates the success of the operation (maybe an error or exception can be raised by the processing function?).
It's much cleaner now, isn't it?

Custom Exception with Entity Framework and Repository Pattern

I am following this Asp .Net tutorial as a guide:
http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control-part-2-adding-a-business-logic-layer-and-unit-tests
I have created a custom error to prevent duplicate records on insert and update
Public Class DuplicateAgencyException
Inherits Exception
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
I have created a validation method that checks for duplication on insert or update:
Public Sub ValidateAgencyName(ByVal agency As agency_temp)
If Not IsNothing(agency) Then
Dim duplicateAgency As agency_temp = AgencyRepository.GetAgencyByName(agency.agency_name).FirstOrDefault()
If Not IsNothing(duplicateAgency) AndAlso duplicateAgency.agency_id <> agency.agency_id Then
Throw New DuplicateAgencyException(String.Format("Agency: {0} already exists.", duplicateAgency.agency_name))
End If
End If
End Sub
I run a unit test to insert a record, and purposefully insert a duplicate, and it throws the correct error. Now I need to handle this error with the ObjectDataSource that displays this data, such as OnInserted and OnUpdated, but I can't even get to that point. When I insert a record, I get the error assistant telling me that DuplicateAgencyException was unhandled by user code. Do I need a try...Catch there? (I did try that to no avail).
I believe you need to wrap your exception within the methods referenced by the events you mention in a try catch block as you suggest. You should wrap the existing code like so
MethodName(params)
《
try 《 /// existing code
》
catch (ExceptionType ex) 《
/// error handling code
》
》
If you post your event handling code and the aspx i can be more specific. Please excuse any errots wrote this on my phone.

How do I Prevent FormView from clearing user's entered Values after Insert Method has fired?

I have been struggling with getting FormViews to work the way Microsoft expects me to for about a day and have figure a bunch of great stuff out.
I can catch e.Exception and e.ReturnValue in the ObjectDataSource.Inserting Event Handler and I can even cheat and check other properties of the Object in the ObjectDataSource.ObjectDisposing by checking the e.ObjectInstance ... and I even learned that FormView's Inserting Handler Runs AFTER the ObjectDisposing Handler so If there is a problem found I still have time to react to it and st the e.KeepInInsertMode to true on the FormView.
My problem is, it seems that the values entered by the user into the Insert form are cleared regardless.
So, How do I Prevent a FormView from clearing after it's Insert Method has fired?
(Using ASP.NET + VB)
I don't think posting my code here will really do much good and i would have to modify it to trim out confidential business logic stuff... so I'll skip it for now.
edit:
I have found a temporary and admittedly terribly cludgy solution (in case no one ever finds a REAL solution to the problem).
I have a page variable defined as:
Dim eInsertArgs As FormViewInsertedEventArgs
And then I do the following in my ItemInserted handler
If boolInsertErrorOccurred = False Then
e.KeepInInsertMode = True
eInsertArgs = e
Else
eInsertArgs = Nothing
End If
Then on each of the controls I have something like this in that controls databinding event:
If IsNothing(eInsertArgs) = False Then
Dim _sender As TextBox = sender
_sender.Text = eInsertArgs.Values("_FieldName")
End If
The effect of this is that I am setting the values BACK to the submitted values AFTER ASP.NET binds the FormView to the default (blank) Template.
Please help me find a less terrible solution. :)
You need to create your own server control which inherits from the FormView control.
Public Class MyFormView
Inherits FormView
Protected Overrides Sub OnDataSourceViewChanged(ByVal sender As Object,
ByVal e As EventArgs)
If (MyBase.CurrentMode = FormViewMode.Insert) Then
MyBase.RequiresDataBinding = False
Else
MyBase.OnDataSourceViewChanged(sender, e)
End If
End Sub
End Class
Please take a look at this page: http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/76885/FormView-Insert

Resources