Can't use "apply" or "call" functions more than once - c#

I'm using the SHDocVw DLL to open and manipulate an Internet Explorer instance from C#. I need to activate the onkeyup event of several textboxes. This is the code I use:
dynamic userNameTextBox=doc.getElementById(5749).getElementsByTagName("input")[0];
userNameTextBox.value=userName;
userNameTextBox.onkeyup.apply(userNameTextBox);
dynamic passwordTextBox=doc.getElementById(5750).getElementsByTagName("input")[0];
passwordTextBox.value=password;
passwordTextBox.onkeyup.apply(passwordTextBox);
This works for the first call to "apply", but the second call generates an error: "System.MissingMemberException: Error while invoking apply.". This problem also occurs if I use "call" instead of "apply". However, if I change one of the event invoking functions from "apply" to "call", than it works - but if I try to call this code again in the same instance of my program it throws the same exception.
In short - I can't use "apply" or "call" more then once, unless I restart my program.
This problem only occurs with Windows XP+IE8. If I try it in Windows7+IE9 I can use "apply"/"call" as many times as I want.
Is there something I could do that will allow me to use "apply"/"call" more than once, or to invoke events in any other way?

OK, I managed a workaround by invoking the event via JavaScript from the command line(AKA "Navigate"), but I still think there should be a COM(Interop?) based solution to this...

Related

How to handle error ": 'Control's handle must be created first.'"?

I ran into an error Control's handle must be created first. The code returns the error
model1.SetView(viewType.Trimetric, true, false);
I already tried model1.CreateControl(); before the code above executed but still same error.
You need to move the SetView() method call in the Form.OnLoad() (WinForms) or Window.OnContentRendered() (WPF) when the Model control is ready to be used.

Subscribing to an event provided by a WCF service

I have a third party application that I'm interacting with via a WCF service that they're exposing. When my application is loaded, the WCF service is supposed to hook into the callbacks I've defined and give me data back. This is straight-forward in VB but I think my issue is syntax in accomplishing this in C#.
Their documentation states the following: Your user control will need to define an internal event with a signature that matches the event that our service is listening for.
Here are my declarations for the handler, as provided by this services' documentation:
public delegate void MyDel(ref string Param1, ref string Param2);
public event MyDel GetInfo;
The documentation then says that I should be able to call this event in the following fashion, and the data I need from the service will be inside of the output variable I pass in by reference:
string output = "";
string methodName = "SpecificAction"
// this is null and throws an exception
// i've tried wrapping it in an if != null block to no avail
GetInfo(ref methodName, ref output);
Console.WriteLine(output.ToString());
In VB the following works without issue:
Public Event GetInfo (ByRef EventName As String, ByRef XmlString As String)
Dim Output As String = ""
RaiseEvent GetInfo("SpecificAction", Output)
The reason that I think I'm doing something wrong with the C# syntax is, as I said, it seems to work in VB just fine. Any suggestions would be appreciated.
Update
So I'm calling this method from my controls' public constructor, which is failing. If I move this exact code inside of a button click handler, for instance, its returning the data that I expect. I'm guessing my program is initializing and hitting that call before their service has hooked in? Is there a way to wait for that to happen?
After seeing that the above code worked when I removed it from my constructor and placed it in a button click handler, I realized that the issue was probably that I was trying to access the data before the service had hooked into it. By instead placing the code inside of my controls Load event, I was able to get back the data I needed.

WebMatrix WebData InitializeDatabaseConnection

I have placed the call to WebSecurity.InitializeDatabase in the _appstart.cshtml as noted in various google searches but periodically I keep receiving the below error when making subsequent calls to WebSecurity.IsAuthenticated for example, I would assume the initialisedatabase is called once for every user entering the website, is there a delay in this initialization before the _pagestart.cshtml is called?
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
at WebMatrix.WebData.SimpleMembershipProvider.VerifyInitialized()
at WebMatrix.WebData.WebSecurity.VerifyProvider()
at WebMatrix.WebData.WebSecurity.Logout()
at ASP._Page__PageStart_cshtml.Execute()
No, there should not be a delay.
To use WebSecurity.InitializeDatabaseConnection, you call once ever with the parameter autoCreateTables= true. After your tables are setup in your database, then from then on in _appstart, you'll call WebSecurity.InitializeDatabaseConnection with autoCreateTables = False.

Why c# winform is not shown when called from vbscript

i've wrote a API in C# which will be used in vb6 & vbscript and assume my API details is as below:
1. there is a form in the API but it is not exposed to COM
2. there is a method called ShowForm() with code "form1.Show()". This method ShowForm() is exposed to COM so that i can call this method from vb6/vbscript.
So, my problem is:
When i call ShowForm() method in vb6 under a button click event, the form created in C# API shows up but if i instantiate the same class and call the same method in vbscript, the form dont show up.
However, if i change the code in ShowForm(),
- from "form1.Show()" to "form1.ShowDialog()", it will show in both vb6/vbscript. But, it is showed as a modal form where what i want is modeless
OR
- from "form1.Show()" to "Application.Run(form1)". It will shows up in vbscript but not in vb6 (it actually shows up in vb6, but it crash after i close the form in C#)
What can i do to make it works fine in vb6 and vbscript? Your advice is very much appreciated
PS: its not script syntax problem or COM problem. it should be something to do with form.show()/ShowDialog()/Application.Run() or message loop thingy which is something i dont really understand
My code would be more or less something like this.
//C# code
public void ShowForm()
{
m_frm.tempWebBrowser.Navigate("http://stackoverflow.com", "_self", Encoding.Default.GetBytes(""), "Content-Type: application/x-www-form-urlencoded");
m_frm.FormBorderStyle = FormBorderStyle.Sizable;
m_frm.ShowInTaskbar = true;
m_frm.WindowState = FormWindowState.Maximized;
m_frm.tempWebBrowser.Visible = true;
m_frm.Show();
}
//VB6 code, vbscript also using exactly the same code in the event
Private Sub Command1_Click()
Dim tempClass
Set tempClass = CreateObject("myClass.API")
tempClass.ShowForm
End Sub
The form is not shown exactly because of what you suggested...there is no message pump running on the thread. If you are trying to fire-and-forget the C# window from VBScript, I suggest you launch it in a seperate process. It would have the same effect as you've described. If you instead were trying to launch it and wait for the user to interact with it (say you are prompting them for info) then showing to form modally is appropriate.

Completed Event not triggering for web service on some systems

This is rather weird issue that I am facing with by WCF/Silverlight application. I am using a WCF to get data from a database for my Silverlight application and the completed event is not triggering for method in WCF on some systems. I have checked the called method executes properly has returns the values. I have checked via Fiddler and it clearly shows that response has the returned values as well. However the completed event is not getting triggered. Moreover in few of the systems, everything is fine and I am able to process the returned value in the completed method.
Any thoughts or suggestions would be greatly appreciated. I have tried searching around the web but without any luck :(
Following is the code.. Calling the method..
void RFCDeploy_Loaded(object sender, RoutedEventArgs e)
{
btnSelectFile.IsEnabled = true;
btnUploadFile.IsEnabled = false;
btnSelectFile.Click += new RoutedEventHandler(btnSelectFile_Click);
btnUploadFile.Click += new RoutedEventHandler(btnUploadFile_Click);
RFCChangeDataGrid.KeyDown += new KeyEventHandler(RFCChangeDataGrid_KeyDown);
btnAddRFCManually.Click += new RoutedEventHandler(btnAddRFCManually_Click);
ServiceReference1.DataService1Client ws = new BEVDashBoard.ServiceReference1.DataService1Client();
ws.GetRFCChangeCompleted += new EventHandler<BEVDashBoard.ServiceReference1.GetRFCChangeCompletedEventArgs>(ws_GetRFCChangeCompleted);
ws.GetRFCChangeAsync();
this.BusyIndicator1.IsBusy = true;
}
Completed Event....
void ws_GetRFCChangeCompleted(object sender, BEVDashBoard.ServiceReference1.GetRFCChangeCompletedEventArgs e)
{
PagedCollectionView view = new PagedCollectionView(e.Result);
view.GroupDescriptions.Add(new PropertyGroupDescription("RFC"));
RFCChangeDataGrid.ItemsSource = view;
foreach (CollectionViewGroup group in view.Groups)
{
RFCChangeDataGrid.CollapseRowGroup(group, true);
}
this.BusyIndicator1.IsBusy = false;
}
Please note that this WCF has lots of other method as well and all of them are working fine.... I have problem with only this method...
Thanks...
As others have noted, a look at some of your code would help. But some things to check:
(1) Turn off "Enable Just My Code" under Debug/Options/Debugging/General, and set some breakpoints in the Reference.cs file, to see whether any of the low-level callback methods there are getting hit.
(2) Confirm that you're setting the completed event handlers, and on the right instance of the proxy client. If you're setting the event handlers on one instance, and making the call on another, that could result in the behavior you're describing.
(3) Poke around with MS Service Trace Viewer, as described here, and see if there are any obvious errors (usually helpfully highlighted in red).
Likely there are other things you could check, but this will keep you busy for a day or so :-).
(Edits made after code posted)
(4) You might want to try defining your ws variable at the class level rather than the function. In theory, having an event-handler defined on it means that it won't get garbage collected, but it's still a little odd, in that once you're out of the function, you don't have a handle to it anymore, and hence can't do important things like, say, closing it.
(5) If you haven't already, try rebuilding your proxy class through the Add Service Reference dialog box in Visual Studio. I've seen the occasional odd problem pop up when the web service has changed subtly and the client wasn't updated to reflect the changes: some methods will get called successfully, others won't.
(6) If you're likely to have multiple instances of a proxy client open at the same time, consider merging them into one instance (and use the optional "object userState" parameter of the method call to pass the callback, so you don't run into the nasty possibility of multiple event handlers getting assigned). I've run into nasty problems in the past when multiple instances were stepping on each other, and my current best practice is to structure my code in such a way that there's only ever one client instance open at a time. I know that's not necessarily what MS says, but it's been my experience.
This issue is because of special characters in one of the fields returned from DB which browser was not able to render. After considerable debug n search over the web, was able to find this out. Used Regular expressions to remove these special characters in WCF, the new returned values from the method was successfully rendered in various browsers on different system. :)
Make sure you have checked 'Generate asynchronous operations' in your service reference. Right-click on the service reference and check the box. This solved it for me.

Categories

Resources