0

I have a requirement where I need to be able to insert into a database the total amount of pages there is on a SSRS report once it is generated, which is dynamic. The report is also ran automatically by a service, which creates and stores it as a pdf, so there is no user interaction with the report.

I feel like this would be possible in some way, because SSRS is capable of inserting data when a report is ran. This situation is a bit different though. The reason being you cannot get the amount of pages of the report before it is generated to give it to your procedure which would do your insert within your dataset.

So I have attempted a few things :

Attempt #1

I have created a report that contains 2 rectangles and a sub report. Both rectangles do a page break after. The sub report contains a parameter called

PageAmount

I then added this code to the code properties of the first report

Function TotalPages() As String   
    Return Me.Report.Globals!TotalPages    
End Function

Which returns the total amount of pages from the report so it can be used in the report body.

I then passed it as a parameter to the sub report like so

Code.TotalPages()

The sub report did the insert inside the database, but the result was always 1. No matter how many pages there would be it would always be 1.

Attempt #2

Since you can import custom assemblies to SSRS to use it in your code, I thought I'd give it a try to do the insert with c# code. So I coded a program that does an insert, tested it and worked fine. I then built and imported the custom assembly to a report file. The code of the method that would do the insert was along these lines (I am aware the code could be improved in many ways, but this was just for testing) :

namespace MyNamespace
{
    public class myClass
    {
        public static string InsertPageNumberDB(int id, string returnValue)
        {
            try
            {        
                string connection = "Data Source = Server; Initial Catalog = Database; User ID = Username; Password = Password";

                using(SqlConnection conn = new SqlConnection(connection))
                {
                    conn.open();

                    SqlCommand cmd = new SqlCommand("INSERT INTO TABLE VALUES(" + id.ToString() + ", " + returnValue + ")", conn);
                    cmd.ExecuteNonQuery();
                }
            }
            catch(Exception ex)
            {
                return ex.ToString();
            }

            return returnValue;
        }
    }
}

Once imported, I was able to use it in an expression in the footer of the report like so :

Code.MyClass.InsertPageNumberDB(1, Globals!TotalPages.ToString())

So this would show the page number as well as insert it inside the table. It did not work and returned an exception instead :

[SecurityException: Request for the permission of type 
'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=################' failed.]

It seems from the exception that the user trying to connect to the database cannot be authenticated when ran from SSRS.

EDIT: After further investigation, the connect request does not reach the database. It fails before it tries to connect. Could it be from the System.Data.SqlClient dll?

So my question is : Is there any way to insert to a table the amount of pages a report has after it's been generated? If not, would there be a way to count programmatically the amount of pages there is in the pdf file?

Appreciate any help.

Cheers,

Zaehos
  • 175
  • 2
  • 13
  • https://stackoverflow.com/questions/320281/determine-number-of-pages-in-a-pdf-file – SQLMason Jul 13 '17 at 13:23
  • Thank you. Will take into consideration, but I would like to know first if this is doable with SSRS. – Zaehos Jul 13 '17 at 13:27
  • I understand and I don't know (or think) you can. But someone may know more than me. If not, there is the answer to your second question. – SQLMason Jul 13 '17 at 13:28
  • Why not simply fix the permission issue if you already proofed your approach works in general? – Filburt Jul 13 '17 at 13:31
  • Cheers to you, friend. – Zaehos Jul 13 '17 at 13:31
  • I am not aware which user it is using to connect to the database. I gave the correct permissions on the table for the user inside the connection string, but it doesn't seem to use that one. Also, removing the security from the database is not an option in my case. – Zaehos Jul 13 '17 at 13:33
  • Check the Report Server management console - it should give you the possibility to find out the user account. Is your target table located in the same database with your report data? If not, you could create a view there and handle INSERT privileges in the view definition. – Filburt Jul 14 '17 at 07:22
  • After further review, the connect request does not reach the database. I believe it stops in the code and returns an exception. Thank you for your help. – Zaehos Jul 14 '17 at 17:52

0 Answers0