Click here to Skip to main content
16,004,887 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Pls help me on below issue

I am trying to hide header and footer which printed default by browser when i print any document .
I am able to hide browser header and footer from print through writing some code.
For hiding header and footer i am deleting printer registery settings via code and after print task completed i am restoring default prtinter registery again .

now problems come here it is only working in visual studio i.e. development environment.
But when i am publishing it on IIS it is not working.


Pls suggest some to work this on IIS


Thanks,
Posted
Comments
Kornfeld Eliyahu Peter 1-Oct-13 8:47am    
It's possible that you have no permissions to change the registry?
Pro86 1-Oct-13 9:21am    
but it is not giving any permission related error
Pro86 1-Oct-13 9:21am    
i am able to do via visual studio
Kornfeld Eliyahu Peter 1-Oct-13 9:36am    
May I see your code that changes the registry?
(Or a full working project to debug...)
Pro86 1-Oct-13 9:44am    
actually no need to debug the code. Code is working fine in visual studio.
Its not working when i published this on IIS and I am not getting any error also from IIS.

1 solution

As I told earlier, it's a permission problem.
I can't tell you from the code you provided why you can't see the error message, but when reproducing the problem I got a clear permission error...

Server Error in '/RegistryTest' Application.
--------------------------------------------------------------------------------

Security Exception 
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. 

Exception Details: System.Security.SecurityException: Requested registry access is not allowed.

Source Error: 


Line 8:  		protected void Page_Load ( object sender, EventArgs e )
Line 9:  		{
Line 10: 			RegistryKey oKey = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Michlol2000", true );
Line 11: 
Line 12: 			DateTime oNow = Convert.ToDateTime( oKey.GetValue( "Now" ) ); 

Source File: c:\m2000_net\Local\Registry\Registry\Default.aspx.cs    Line: 10 

Stack Trace: 


[SecurityException: Requested registry access is not allowed.]
   System.ThrowHelper.ThrowSecurityException(ExceptionResource resource) +52
   Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +10629081
   RegistryTest.Default.Page_Load(Object sender, EventArgs e) in c:\m2000_net\Local\Registry\Registry\Default.aspx.cs:10
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6704
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +245
   System.Web.UI.Page.ProcessRequest() +72
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +58
   ASP.default_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\registrytest\c3463957\356fb4d5\App_Web_irghuleb.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18331 


The reason is that when you run from VS you use your own identity, which probably has all the permissions to read and write registry...
When you run directly against iis you got the pool identity which by default is ApplicationPoolIdentity. This identity has no permissions even to read registry!!!
To resolve it you can change the pool identity to someone has permissions, LocalSystem for instance...

---

Not part of the answer but very very VERY important in my opinion.
Do not use registry if you have no an extremely good reason to do so. And never do it from an web application - web application is for the client!!! your code running on the server, which means that actions of the client will change the registry on the server!!! (that's your case!!!)
If you have configuration values to store use config file.


Regards
Peter
 
Share this answer
 
Comments
Pro86 3-Oct-13 2:51am    
ok,
Thanks for reply.

Is there any other way to hide browser header and footer from print page
Kornfeld Eliyahu Peter 3-Oct-13 3:06am    
You just touched the most painful wound for every WEB developer...
Unlike native applications you have only one half of your app under your total control.
The browser is sitting far from you. It's not your choice. Even for the same browser can be so much different configurations...
If your end user using browsers build in printing, you can't control aspects of the printed page. He may decide to print b-w a coloured page. saving it to pdf or tiff or sending to a plotter...
It his machine, his app, his decision.
You may overcame it by providing some pre formatted file - like pdf and hope the user has some software to handle it properly...
Pro86 4-Oct-13 1:01am    
can you please send me code for PDF stuff ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900