Click here to Skip to main content
15,302,455 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi All,

I am trying to generate a report in excel.
I am writing following code.
Dim format As System.IFormatProvider = New System.Globalization.CultureInfo(CType(Application("StrToDateFormat"), String), True)
      Dim oXL As Excel.Application
      Dim oWB As Excel.Workbook
      Dim oSheet As Excel.Worksheet
      Dim oRng As Excel.Range
      Dim strFileName, strExcelFIle As String
      objDate = System.Convert.ToDateTime(tbToDate.Text, format)

      strExcelFIle = "PlantUtilizationReport " + objDate.ToString("yyMMdd") + ".xls"
      objDate = System.Convert.ToDateTime(tbToDate.Text, format)
      strFileName = MapPath(".") & "\" & strExcelFIle
      Dim TheFile As FileInfo = New FileInfo(strFileName)
      If TheFile.Exists Then
      End If
      objDate = System.Convert.ToDateTime(tbToDate.Text, format)
      ' Start Excel and get Application object.
      oXL = New Excel.Application
      oWB = oXL.Workbooks.Open("c:\inetpub\wwwroot\LCAccounting\PantOccupancy.xls")
      oSheet = oWB.ActiveSheet

      oSheet.Cells(TodaysDateRow, TodaysDateCol) = tbToDate.Text

      dt = obj.GetTable("SP_FGPlantUtilizationTrnSelForExcel", objDate.ToString("MM/dd/yyyy"))
      Dim RowNo, colNo As Int16
      For Each dr In dt.Rows
          RowNo = dr("RowNo")
          colNo = dr("ColNo")
          oSheet.Cells(RowNo, colNo).Value = dr("UtilizationHours")
          '       oSheet.Cells(RowNo, DailyProductionCol).Value = dr("Qty")


      dt = obj.GetTable("SP_FGPlantProductionTrnSelForExcel", objDate.ToString("MM/dd/yyyy"))
      For Each dr In dt.Rows
          RowNo = dr("RowNo")
          oSheet.Cells(RowNo, DailyProductionCol).Value = dr("Qty")


But it shows following error :

Access is denied.<br />
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.<br />
<br />
Exception Details: System.UnauthorizedAccessException: Access is denied.<br />
<br />
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.<br />
<br />
To grant ASP.NET write 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.

Stack Trace:

[UnauthorizedAccessException: Access is denied.]<br />
   BDRegions.Report.lbReport_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\LCAccounting\Report.aspx.vb:83<br />
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)<br />
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)<br />
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)<br />
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)<br />
   System.Web.UI.Page.ProcessRequestMain() +1330<br />

Please help me

Thanks & Regards,
Atul sharma
Updated 12-Apr-11 21:28pm
Sunasara Imdadhusen 13-Apr-11 1:55am
Please provide appropriate rights on inetpub folder.
AtulOmSharma 13-Apr-11 2:10am
Sir can you please tell me what's rights i should provide to inetpub folder
Atul Sharma

Error message tells that you don't have permissions to access the file.
ASPNET user or NETWORK SERVICE user don't have permissions. Check the read/write/execute checkboxs in the securities tab properties of the Inetpub folder.
Dalek Dave 13-Apr-11 3:28am
Good advice
Go to the Inetpub folder
Rightclick --> Properties --> security TAB
add user ASPNET User --> give rights of Modify
click on Apply and OK
then try with the same
AtulOmSharma 13-Apr-11 2:38am
I gave rights but it still showing same error.

Atul Sharma
AtulOmSharma 13-Apr-11 6:14am
Hi koolprasad,
After setting permission rights to Inetpub folder and in web.config file set <identity impersonate="true"> it works fine
It will be helpfull for others.
AtulOmSharma 13-Apr-11 6:17am
In web.config set
identity impersonate="true"
Check the permission. I think because of permission it may happen.

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