Wednesday, March 28, 2012

Saving Report (Access Denied when rendering)

Dear All,

It's a beginner of reporting service. I would like to render a report and then save it automatically. However, I face the problem as below:

When I render a report, I got an error : "Exception Details:System.Net.WebException: The request failed with HTTP status 401: Access Denied.". The code is as follows:

ReportingService rs = new ReportingService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
byte[] ResultStream;
string[] StreamIdentifiers;
string OptionalParam = null, filename="NorthwindCustomers.pdf";
ParameterValue[] optionalParams = null;
Warning[] optionalWarnings = null;
ResultStream = rs.Render("/Northwind Customers", "PDF", null,
"<DeviceInfo<Toolbar>False</Toolbar></DeviceInfo>",null,null,
null, out OptionalParam, out OptionalParam, out optionalParams,
out optionalWarnings, out StreamIdentifiers);
// Creating a verbatim string.
FileStream stream = File.OpenWrite(@."C:\Articles\SQL Server Reporting
Services\SourceCode\RSWebServiceXS\NorthwindCustomers\" + filename);
stream.Write(ResultStream, 0, ResultStream.Length);
stream.Close();

Error occurs when the method rs.Render() is called. Any suggestion to solve the problem??

Wow...so happy that the problem has been solved as follows:

Replace:

rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

To:

CredentialCache cache = new CredentialCache();
cache.Add( new Uri(rs.Url), // Web service URL
"Negotiate", // Kerberos or NTLM
new NetworkCredential("username", "password", "domainname") );
rs.Credentials = cache;

No comments:

Post a Comment