Hyperlinkfield report path passed in from code-behind.

I am displaying an asp:gridview of records with a linked list of results;
The first column is a hyperlinkfield of InvoiceNumber; which when the user clicks on it, launches a Reporting Services Report of details for that Invoice Number parameter;
Right now, I have the report path hard-coded and it works nice;
But I cant seem to pass the report server name/path in the code-behind.
Can this be done or do I have to use a linkbutton?

attached is my current code for the hyperlinkfield which works,
AND I am including the non-working code where I try to pass the report name path in the code-behind for the hyperlinkfield.  I add a RowDataBound event to my gridview and code a function in the code-behind.
Protected Sub gvResults_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResults.RowDataBound
            Dim strURL As String
            Dim strPath As String

            strURL = ConfigurationManager.AppSettings("ReportURL").ToString
            strPath = ConfigurationManager.AppSettings("ReportPath").ToString
            strPath = strURL + strPath

            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim hl As HyperLink = TryCast(e.Row.Cells(0).Controls(0), HyperLink)
                hl.NavigateUrl = strPath
                hl.NavigateUrl = "fspInvoice&rs:Command=Render&InvoiceNum={0}" & gvResults.DataKeyNames(e.Row.RowIndex)(0)
            End If
        End Sub

Maybe I don't have the syntax correct but it seems like I have more control over the report and more properties available for the hyperlinkfield.
For example, with the hyperlinkfield, I have target="_blank" so that I can open SSRS report in a new window which helps with our application.
I dont know if that is available for the linkbutton.

thanks in advance.
working hyperlinkfield code:
        SortExpression="InvoiceNum" >
<ItemStyle HorizontalAlign="Center" Wrap="False" Width="10%"></ItemStyle>

Open in new window

Who is Participating?
Shaun KlineConnect With a Mentor Lead Software EngineerCommented:
Instead of using DataKeyNames, try using DataKeys:

hl.NavigateUrl = strPath & "fspInvoice&rs:Command=Render&InvoiceNum={0}" & gvResults.DataKeys(e.Row.RowIndex)(0)
Shaun KlineLead Software EngineerCommented:
The second line below overwrites the first line:

                hl.NavigateUrl = strPath
                hl.NavigateUrl = "fspInvoice&rs:Command=Render&InvoiceNum={0}" & gvResults.DataKeyNames(e.Row.RowIndex)(0)

Instead, do:

                hl.NavigateUrl = strPath & "fspInvoice&rs:Command=Render&InvoiceNum={0}" & gvResults.DataKeyNames(e.Row.RowIndex)(0)
jtrapat1Author Commented:
shaun kline-
thanks for the response-
I tried what you suggested but Im getting an error:
"Index was outside the bounds of the array. "
when I debug, i can mouse over and the strPath is correct;
and the DataKeyNames has the correct field name but not the value;
since this occurs when the gridview is being rendered, NOT even clicked on at this point.

In my definition for the gridview - i stripped out everything for the asp:hyperlinkfield and just added the ondataBound event to the definition like so:

how can i fix this error?
jtrapat1Author Commented:
forgot to attach the error image-
jtrapat1Author Commented:
You were absolutely right!
that worked great -
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.