Why If statement is repeat for twice? - asp.net

I have a "if" or "case" statement in the a method that I can called. I have "Flag" varriable as public status. Its Crazy its loop/running for twice, so cause have duplicate command, in these case I have duplicate data on the database.
Public Sub compartment1(ByVal exec As Boolean)
Try
If exec = True Then
Select Case FlagMark
Case 1
Insert database execute command
Case 2
another command
End Select
ElseIf exec = False Then
End If
Catch ex As Exception
End Try
End Sub
And this is my button command
Private Sub cmd_confirm_Click(sender As Object, e As EventArgs) Handles cmd_confirm.Click
If pCheck2.Checked = True Then
FlagMark = 1
compartment1(True)
End If
End Sub
The Method "Compartment1" is running twice, look like looping, so I have a duplicate data or duplicate for single command. Can it running only once?

Solved! after I replace the html button that given runat=server attributes with ASP.Net button.
Why is so different, If with ASP.NET button the event is running normaly step by step the if or case statement, While using Html button with runnat=server is look like looping process. Even though has a same properties configuration as bellow :
CausesValidation =True
EnableViewState=True, etc

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

Access - Application Echo not working

I want to stop flickering of my form when VBA code is executed, but Application Echo is not working.
I have this code in Combobox_After_Update event :
Private Sub Combo11_AfterUpdate()
'Stop flickering
Application.Echo False
On Error Resume Next
'If User deletes Combo Item, then delete record
If IsNull(Combo11) Then
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
End If
'Call code for re-positioning controls on form
Call MovingAllControls
'Close and reopen form
Call ReOpen
Application.Echo True
End Sub
Called procedure (which is probably cause of flickering):
Sub MovingAllControls
'Refresh
DoCmd.Requery
Const MaxRecs As Integer = 10
Dim NumRecs As Integer
On Error Resume Next
'find last record in subform and then expand Detail section according to number 'of records
With Forms![MyForm]![MySubform].Form
.Recordset.MoveLast
NumRecs = .RecordsetClone.RecordCount
If NumRecs > MaxRecs Then NumRecs = MaxRecs
.InsideHeight = NumRecs * .Section(0).Height + 350
End With
'Moving all controls under subform - in this example only one, but in reality I 'have plenty controls to move on form
Forms![MyForm]![FieldName].Top = Forms![MyForm]![Myubform].Top + Forms![MyForm]![MySubform].Form.InsideHeight + 1100
End Sub
Another procedure that is called from Combobox_After_Update event:
Sub ReOpen()
'I reopen form, because this is only way my subform controls moves as they 'should - dynamically
DoCmd.Close acForm, "MyForm"
DoCmd.OpenForm "MyForm"
End Sub
I also tried to to see what error is producing in After_Update_event, and I get error "424 - object required", but my code excecutes, only problem is flickering of controls. Any other way to stop flickering, or what is wrong with my code ?
Thanks for help !!
Rather than Application.Echo False, try the Form.Painting method:
' code under form
Me.Painting = False
' do actions that cause flicker
Me.Painting = True
Me.Repaint
Also, looking at your code here:
DoCmd.Close acForm, "MyForm"
DoCmd.OpenForm "MyForm"
I would say this is bad practice:
This can cause performance problems for even moderately large record sets.
If you use events like Form_Open or Form_Close, this can cause bugs by re-running code that was intended to only run once, when the form was first opened.
There is always a way in Access to get the results you want without closing/reopening the form.

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?

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

ASP.NET: Very simple event handling not working

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.

Resources