24

I am working on project. In my System when I run the project it is running nicely, but after uploading it to my domain when I check, then it displays the error like:

"Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

What is is the reason for this and how can I resolve it?

Please help me...

Thanks.

Brissles
  • 3,833
  • 23
  • 31
Darsak
  • 345
  • 1
  • 2
  • 4
  • 2
    why do you wont to show modal dialog box on **web server** from asp.net application ?!? – Antonio Bakula Jan 19 '12 at 16:02
  • 1
    It is for Conformation of Delete??? – Darsak Jan 19 '12 at 16:10
  • @Darsak How did solve this later? I'm also stuck with exactly the same problem. But all the answers shows for `Messagebox` rather than showdialog. I am not using Messagebox. But looking to open `folderbrowserdialog` box. Any suggestion on how I can make this possible? – Priya May 06 '22 at 11:35

6 Answers6

14

This error can be resolved by adding MessageBoxOptions.ServiceNotification.

MessageBox.Show(msg, "Print Error", System.Windows.Forms.MessageBoxButtons.YesNo, 
   System.Windows.Forms.MessageBoxIcon.Error,     
   System.Windows.Forms.MessageBoxDefaultButton.Button1, 
   System.Windows.Forms.MessageBoxOptions.ServiceNotification); 

But it is not going to show any dialog box if your web application is installed on IIS or server.Because in IIS or server it is hosted on worker process which dont have any desktop.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
Anukana Saha
  • 165
  • 1
  • 3
14

You can't show dialog box ON SERVER from ASP.NET application, well of course tehnically you can do that but it makes no sense since your user is using browser and it can't see messages raised on server. You have to understand how web sites work, server side code (ASP.NET in your case) produces html, javascript etc on server and then browser loads that content and displays it to the user, so in order to present modal message box to the user you have to use Javascript, for example alert function.

Here is the example for asp.net :

https://www.madskristensen.net/blog/javascript-alertshow(e2809dmessagee2809d)-from-aspnet-code-behind/

JayCOS
  • 57
  • 3
  • 9
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • I have almost the same situation. I am doing an ajax call kfrom javascript and Opening a folderBrowserDialog from conrtroller c# (System.Windows.Forms) and its working in my machine . After packaging and deployment it gives the same error in the question. Any ides on this ? https://stackoverflow.com/questions/47544635/showing-a-folderbrowser-dialog-from-webapp-mvc – P-RAD Nov 29 '17 at 23:56
12

You 100% can do this on the server side...

     Protected Sub Button3_Click(sender As Object, e As System.EventArgs)
    MesgBox("Test")
End Sub

Private Sub MesgBox(ByVal sMessage As String)
    Dim msg As String
    msg = "<script language='javascript'>"
    msg += "alert('" & sMessage & "');"
    msg += "</script>"
    Response.Write(msg)
End Sub

here is actually a whole slew of ways to go about this http://www.sislands.com/coin70/week1/dialogbox.htm

Nefariis
  • 3,451
  • 10
  • 34
  • 52
  • 1
    Downvotes are most likely because one should never build html by concatenating unknown strings. This is a XSS vulnerability and generally a bad style. i.e. when your string contains an apostrophe then your script is (possibly intentionally) broken. https://xkcd.com/327/ – Imre Pühvel Sep 21 '20 at 07:13
  • I just downvoted because this "answer" contradicts itself - this doesn't run on the server. It emits *client* javascript that displays an alert box on the client. Never mind the vulnerabilities. This was already a bad idea back in 2012. The answer even back then was to send a notification to the client, either through SignalR or a response to an AJAX request. ASP.NET's Ajax controls already supported this – Panagiotis Kanavos Oct 12 '22 at 07:44
  • @PanagiotisKanavos - I respectively disagree that it contradicts itself. This code lives on the server, it does not live on the client, and it doesn't have to be the client that drives this alert, it could just be a some random internal function showing a self made alert. Also, I mentioned that I used this method for debugging code, I never proclaimed its imperviousness nor recommended anyone allow the input unknown strings. I more wrote this answer (10 years ago) in response to the above answers that said it was impossible to display messages to the client FROM the server - which this does. – Nefariis Oct 12 '22 at 17:26
1

You also encounter this if you run an Application on a Scheduled Task in Non-Interactive mode.

As soon as you show a Dialog it throws the error:

Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

You can see its a MessageBox causing the problem in the stack trace:

at System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon,  MessageBoxDefaultButton defaultButton, MessageBoxOptions options, Boolean showHelp)  

Solution

If you're running your app on a Scheduled Task send an email instead of showing a Dialog.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
1

This can also be caused if your code returns an additional pop-up or messagebox fail message that the window can't handle properly.

Louise
  • 382
  • 1
  • 6
  • 16
-1

For Vb.Net Framework 4.0, U can use:

Alert("your message here", Boolean)

The Boolean here can be True or False. True If you want to close the window right after, False If you want to keep the window open.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168