MyBase initialize() in Membership Provider - vb.net

In my code I use this line to Initialize my Base:
MyBase.Initialize(name, config)
Everything goes fine in the first time pass... but when it passes second time then throws me an error The Base Is already Initialized and that is something I don't want to happen.
Is there any way to catch this event?

Finally the Base initialized only in the Default.aspx page, putting these lines in the Page_Load:
Dim myNewAsp As New AspNetSqlProvider
If MyAspNetSqlMembershipProvider.SQLconnectionString = Nothing Then
myNewAsp.InitializeSite(sender, e)
Return
End If
Doing this the system always knows when if the Base is initialized or not.

Related

Making a `FlxSubState` that can be re-used without it crashing on the second `open`?

Typically, when I want to open a FlxSubState that I have defined in my code, I will use:
openSubState(new MySubState());
I have all of my create/add logic inside MySubState.create()
Which works fine, except, if there is a lot of stuff on that SubState it can cause a huge lag spike making the game 'freeze' before the substate is displayed.
If I try set a variable to my substate and re-use it, it works once, but then crashes the game when I try to open it a second time, because the substate is being auto-destoyed on close.
By default, when a FlxSubState closes, it is destroy()ed as well - removing all added objects, etc.
Also, since new and create are only called the very first time the substate is opened, anything you add there doesn't get re-added (and you don't want to have new/create called every time you open the substate, since that would not stop the lag-spike)
The solution is simple: in the FlxState that is opening your substate, there is a flag destroySubStates set this to false and the substates will not be destroyed on close.
...and how do you make changes to the substate between opens? You can use the openCallback in FlxSubState which gets triggered when you open the substate, after create gets called (if it does) and before the substate is displayed.
So:
If you want to have an updating FlxSubState that can be re-used to cut down on 'lag' when it is opened, here's what I did:
in my PlayState:
override public function create():Void
{
...
mySubState = new MySubState();
destroySubStates = false;
...
}
public function openMySubState():Void
{
openSubState(mySubState);
}
in MySubState:
public function new():Void
{
super(0);
openCallback = refresh;
}
override public function create():Void
{
// create and add all of my elements
}
private function refresh():Void
{
// anything that changed between last time it was open and now, update .text/etc
}
AND this cuts out the lag spike everytime you open the substate!
...just remember to call mySubState.destroy(); inside your PlayState.destroy(); to clean up properly!

UWP "Invalid attribute value Unknown for property BorderThickness." while navigating to new Frame

While trying to navigate from one Page to another, I'm getting this "Invalid attribute value Unknown for property BorderThickness." error.
If I step through the code in the debugger everything works fine. If I let the navigation happen on it's own, the code crashes.
Outside of setting BorderThickness to specific (integer) values or using the built-in ThemeResources, these values are not ever tied to bound values that might be null or have an unexpected value.
This code was working fine at one point, but that seems to have come to an end this morning.
I'm still not certain why this is an issue, but I can identify specifically where the error is occurring.
public async void OnLevelUp(object sender, EventArgs e)
{
IsBusy = true;
LevelUpVm.CharacterId = IoC.Game.GetCharacter(SelectedCharacter.Id).Id;
**await IoC.SaveConfigFile();** <<< OFFENDING LINE OF CODE
var rootFrame = Window.Current.Content as Frame;
rootFrame?.Navigate(typeof(LevelUpView), null);
IsBusy = false;
}
If I move the OFFENDING LINE OF CODE after the rootFrame?Navigate line, it works fine.
So, after 5 hours of messing with the code, I've come to a solution - but I'm still not certain why the await call causes the problem.

Proper way to send Web API response

I read somewhere that TRY CATCH is not recommended in Web API methods.
I'm making the following call into a method and if all goes well, I want to return an Employee object along with Status 200 but if something goes wrong e.g. database call fails, etc. I want to return status 500. What's the right way to handle that code?
[HttpPost]
public async Task<IHttpActionResult> PostNewEmployeeAsync(Employee emp)
{
var newEmployee = await RegisterEmployee(emp);
return Ok(emp);
// What if I had a database error in RegisterEmployee method. How do I detect the error and send InternalServerError()
}
private async Task<Employee> RegisterEmployee(Employee emp)
{
// Call DB to register new employee, then return Employee object
}
Your code should return the error code that matches the case that you have, for example if your code couldn't find the required resource in the database return NotFound,
but if you code raises an exception, avoid wrapping your code by try/catch block and instead the exception should bubble up to the level that you can handle it globally, to do this you have many options like :
1- Implement an ExceptionFilter where you can handle all the unhandled exceptions raised in your controllers (this doesn't include any exception happens before the controllers in the pipeline).
See this for more details about ExceptionFilterAttribute.
2- If you are using Web API 2, you can implement the interface IExceptionHandler where you can handle all the exception happens anywhere in the pipeline and there you can return the errors you want.
See this for more details about Global Exception Handling in Web API 2.
Hope that helps.
You don't want to avoid try/catch entirely, you just need to be really careful about it. Wrap your code in a try block, and catch the exception you're expecting. Inside the catch, return the error response.

how to deal with Element is already the child of another element? [duplicate]

At first, this exception doesn't really make sense to me. Why shouldn't i be able to duplicate this object multiple times? but thats not the point:
i use a List. Whenever i navigate to a site, it should do this:
(App.Current as App).recent.ForEach(x => container.Children.Add(x));
(container = another StackPanel)
the first time, it works. afterwards, i get the exception displayed in the questiontitle. i already tried using a listbox, but i just got a ArgumentException. I think these exceptions have the same source, but i don't know what i'm doing wrong. please help
thanks
The error is quite clear: A WPF/SL Control can only belong to 1 Parent control at a time.
So you'll either have to remove the Controls from their Parent when you are moving away from a Page or you'll have to Create (possibly Clone) new Controls in this ForEach.
When you navigate to a page, the old instance is not removed instantly, so when you add your controls to the new page, they may still be used in the old page if it's not destroyed, causing the exception.
Try overriding the OnNavigatedFrom(NavigationEventArgs e) and OnNavigatingFrom(NavigatingCancelEventArgs e) methods that are invoked when you leave a page so that you empty your StackPanel.
For example, you could do :
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
container.Children.Clear();
}
You have to remove the container's children when you navigate from the page. But not if the navigation is due to a suspend. To avoid the children clear when the navigation is due to a suspend it is better to override OnNavigatingFrom instead of OnNavigatedFrom
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
container.Children.Clear();
base.OnNavigatingFrom(e);
}

Windows Forms with async socket; no text output

Currently I am having a weird problem which I simply do not understand. I have a simple GUI, with one button & one richeditbox. I have an async socket running, I am receiving some data over the network which I want to print to the gui(richeditbox). The async socket is being started when the user hits the button. So when I receive the network data I call a function which prints the data, here how it looks like (in form1 class):
Public Sub AddText(ByVal text As String)
Try
Console.WriteLine(text)
RichTextBox1.AppendText(text)
RichTextBox1.AppendText(vbNewLine)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub
Then I simply do Form1.AddText(..) from my network class or a module (does it matter?). The problem is that nothing appears in the richeditbox, even though the AddText function is being called, no exceptions, no errors, simply nothing. I've looked thru it with the debugger, and "text" contained the data it had to print, but simply nothing appears.. Anyone have an idea?
If the socket is running on another thread (which, of course, it is because it's asynchronous), you may have to use InvokeRequired in order to get the RichTextBox to display the text. I had a similar issue with a listener on an asynchronous serial port listener.
I'm pretty sure David is right. Here's an example.
Delegate Sub AddTextDelegate(ByVal text as String)
Public Sub AddText(ByVal text as String)
If Me.InvokeRequired Then
Me.Invoke(new AddTextDelegate(AddressOf Me.AddText), new object() { text })
Else
Try
Console.WriteLine(text)
RichTextBox1.AppendText(text)
RichTextBox1.AppendText(vbNewLine)
Catch e as Exception
Console.WriteLine(e.ToString())
End Try
End If
End Sub
The deal is that controls have to be updated on the thread they were created on. It sounds like the AddText() routine is being called in the context of your async socket's thread. The AddText() routine will behave like a recursive function. The first time it's called, the InvokeRequired property will be true. This will cause it to be called again via the Invoke() call, which takes care of marshaling the data to the correct thread. The second time it's called, InvokeRequired will be false, and the control will be updated.
Fixed. I couldn't use Form1 to call the functions, because it's a type, its like a new var there with its own memory, since its a diff thread. So when I checked InvokeRequired, it said false because that Form1 belongs to that Thread, and thus no text was being displayed because I didn't even see the form. So just made a global var such as Public myForm As Form1 and assigned myForm to Form1 in Form1_Load.