Click here to Skip to main content
15,886,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used this code this is working in my visual studio runiing mode but a error come when i published it in localhost

VB
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)

        Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("C:\sample.pdf", FileMode.Create))

        'PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\sample.pdf", FileMode.Create))

        doc.Open()
        Dim table = New PdfPTable(2)

        Dim cell As New PdfPCell

        cell = New PdfPCell(New Phrase("THIS IS CONTACT INFORATION WHICH IS FIELD BY USER"))
        cell.Colspan = 2

        cell.HorizontalAlignment = 1


        table.AddCell(cell)

        table.AddCell("First Name:")

        table.AddCell(txtfname.Text)

        table.AddCell("Last Name: ")
        table.AddCell(txtlname.Text)

        table.AddCell("Address:")
        table.AddCell(txtaddress.Text)

        table.AddCell("Apt#:")
        table.AddCell(txtapt.Text)

        table.AddCell("Postal Code: ")
        table.AddCell(txtpostalcod.Text)

        table.AddCell("City: ")
        table.AddCell(txtcity.Text)

        table.AddCell("*Province:")
        table.AddCell(DropDownList1.Text)

        table.AddCell("Country: ")
        table.AddCell(DropDownList2.Text)

        table.AddCell("Phone: ")
        table.AddCell(txtphone.Text & vbNewLine & s)

        table.AddCell("E-mail Address:")
        table.AddCell(txtemail.Text)

        table.AddCell("Campus of Interest: ")
        table.AddCell(DropDownList3.Text)

        table.AddCell("Program of Interest:")
        table.AddCell(DropDownList4.Text)


        table.AddCell("How did you hear about us?: ")
        table.AddCell(DropDownList5.Text)

        table.AddCell("CONTACT FORM FIELD DATE AND TIME")
        table.AddCell(Date.Now)

        doc.Add(table)

Server Error in '/' Application.
--------------------------------------------------------------------------------
Access to the path 'C:\sample.pdf' is denied. 
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.UnauthorizedAccessException: Access to the path 'C:\sample.pdf' is denied. 

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true" />, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 

[UnauthorizedAccessException: Access to the path 'C:\sample.pdf' is denied.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +651
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038
   System.IO.FileStream..ctor(String path, FileMode mode) +64
   CONTACT.ImageButton1_Click(Object sender, ImageClickEventArgs e) +81
   System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +86
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +115
   System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
Posted
Updated 3-Sep-11 8:58am
v2

When you host and run the application, you would be running under a different account. Make sure this account has access to access and read the file.
 
Share this answer
 
Comments
Abhijit Jana 3-Sep-11 15:13pm    
It should Write Access. Read is the default access which should have for all the Application Pool Identity.
Abhinav S 4-Sep-11 1:30am    
Yes correct. Valid point.
Yes From Visual Studio it will work fine but from Local host won't work. And here is the reason and solution.
When your application is running from Visual Studio, your application having sufficient privilege to write of your hard drive, hence your application is running fine as it is able to create file.
Now Come to the point when you are hosting it to IIS.
Application pool having 3 types of identity.
1. NetworkService<br />
2. Local Service<br />
3. Local System


"Network Service" is the default Identify. "defaultappPool" is also runs under the "Network Service" Identity
By default your application pool runs on "NetworkService" account which has very minimum access to your drive, only read. So, When your application trying to write a file on drive, its getting access denied.
The solution, is give the Application Pool with "LocalSystem" privilege or Run your application pool with the account who have write access on drive.
So, create a new Application Pool for your application, Change the Identity to "LocalSystem" or Run on High Privilege account.

To know more about "Application Pool and the Identity" Read this section from one of my article
Beginner's Guide : Exploring IIS 6.0 With ASP.NET [^]

That will resolve your issue.

Cheers !!
 
Share this answer
 
v3
Comments
irfanansari 3-Sep-11 15:14pm    
So Please tell me how i can do this on my Domain As i Had purchased
i want to Also do this on Write on Domain
Abhijit Jana 3-Sep-11 15:18pm    
Well, you should have mentioned it in your question that you are getting problem with you host provider. Check out your IIS Configuration from Web Hosting site Dashboard. There you should have some option. Else, contact with your admin of hosting site.
Secondly, You can't write HardCoded Path as "C:\" . Because, your Hosting site might not give you write access on their physical drive. So first use Relative path of your Virtual Drive, something using Server.MapPath() . Hope you got my point.
irfanansari 3-Sep-11 15:31pm    
i will use also like this but there is also Same Error
Server.MapPath()
Abhijit Jana 3-Sep-11 15:32pm    
Are you trying it in your local IIS ? What you did ? Just keep Server.MapPath() bit side for a moment.
irfanansari 3-Sep-11 15:36pm    
i will use also like this but there is also Same Error
Server.MapPath()
i mean how i can change in my domain host

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