Retrieve attached file save on folder on ASP.Net RDLC Report Button Click

Vanessa
 
on Apr 16, 2021 06:48 AM
3526 Views

Hello Guys,

I want to generate my reports on RDCL Reports by selecting parameters.

My problem is I didn't know how to merge View button to retrieve my attached file on specific path folder and view to another windows tab.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 24, 2021 02:31 AM
on Jan 11, 2022 09:10 AM

Hi Vanessa,

Adding button is not possible to report column for view the attached file.

You need to add Hyperlink to view the file from path.

Add column with hyperlink to the report.

Set the url for the Hyperlink with query string parameter.

The in the view page based on the query string value fetch the file path and display in page.

For more details on adding hyperlink refer below link.

Add Hyperlink to RDLC Report in ASP.Net

Then you need to set the ReportViewer HyperlinkTarget property to _blank and ReportViewer1 LocalReport EnableHyperlinks to true.

You can also set the background image for the View link aswell.

HyperLink url

="http://localhost:16247/View.aspx?Id=" & Fields!CustomerId.Value

HTML

<asp:ScriptManager runat="server" />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="100%" Height="300" HyperlinkTarget="_blank"></rsweb:ReportViewer>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms

Code

Default

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.LocalReport.EnableHyperlinks = true;
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Customers dsCustomers = GetData("select top 5 * from customers");
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}

private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ReportViewer1.LocalReport.EnableHyperlinks = True
            ReportViewer1.ProcessingMode = ProcessingMode.Local
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
            Dim dsCustomers As Customers = GetData("select top 5 * from customers")
            Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
            ReportViewer1.LocalReport.DataSources.Clear()
            ReportViewer1.LocalReport.DataSources.Add(datasource)
        End If
    End Sub

    Private Function GetData(ByVal query As String) As Customers
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand(query)
        Using con As SqlConnection = New SqlConnection(conString)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dsCustomers As Customers = New Customers()
                    sda.Fill(dsCustomers, "DataTable1")
                    Return dsCustomers
                End Using
            End Using
        End Using
    End Function

View

C#

protected void Page_Load(object sender, EventArgs e)
{
    string filePath = "";
    string id = Request.QueryString["Id"];
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT FilePath FROM Reports WHERE Id = @Id";
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand(query);
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            con.Open();
            filePath = Convert.ToString(cmd.ExecuteScalar());
            con.Close();
        }
    }

    // Display the file from file path.
}

 VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim filePath As String = ""
    Dim id As String = Request.QueryString("Id")
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT FilePath FROM Reports WHERE Id = @Id"
    Using con As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand = New SqlCommand(query)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            con.Open()
            filePath = Convert.ToString(cmd.ExecuteScalar())
            con.Close()
        End Using
    End Using
    ' Display the file from file path.
End Sub