robhob
robhob

Reputation: 89

Using SQL Server Authentication to connect to SSRS from a WinForms Application

I'm busy testing SSRS to see if it's a viable alternative to our current reporting solution. I've set up SSRS on my local machine and have developed a working report using SQL Server Report Builder. Now what I'm trying to do is to call the report from within a WinForms application and display it in a ReportViewer control. The problem is that I've set up SQL Server to use SQL Server Authentication and I'm struggling to figure out how to connect to it programmatically.

The code I've pieced together so far looks like this:

Imports Microsoft.Reporting.WinForms

Public Class frmMain
Public v_report_name As String = "TestReport"
Public v_report_server As String = "http://elnah-ict-dt006:80"
Public v_report_path As String = "/reports_SSRS/"

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'create parameter array
    Dim paramlist As New List(Of Microsoft.Reporting.WinForms.ReportParameter)

    'create a specific parameter required by the report
    Dim param1 As New Microsoft.Reporting.WinForms.ReportParameter("ClientID")

    'add values to the parameter here we use a variable that holds the parameter value
    param1.Values.Add("0279")

    'add parameter to array
    paramlist.Add(param1)

    'Set the processing mode for the ReportViewer to Remote  
    ReportViewer1.ProcessingMode = ProcessingMode.Remote

    'use the serverreport property of the report viewer to select a report from a remote SSRS server
    ReportViewer1.ServerReport.ReportServerUrl = New System.Uri(v_report_server)
    ReportViewer1.ServerReport.ReportPath = v_report_path & v_report_name
    'select where the report should be generated with the report viewer control or on the report server using the SSRS service.
    'Me.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote

    'add the parameterlist to the viewer
    ReportViewer1.ServerReport.SetParameters(paramlist)


    Me.ReportViewer1.RefreshReport()
End Sub
End Class

When it hits the SetParameters line towards the bottom, it gets the following error message:

Microsoft.Reporting.WinForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.MissingEndpointException HResult=0x80131500 Message=The attempt to connect to the report server failed. Check your connection information and that the report server is a compatible version. Source=Microsoft.ReportViewer.WinForms

I've tried to find examples of how to set the username and password but from what I can tell, most examples are focused on using Windows Authentication. I've tried the following line but it doesn't work:

ReportViewer1.ServerReport.ReportServerCredentials = New ReportServerCredentials("SA", "mypassword")

I haven't worked in VB.NET for ages so please excuse any obvious errors.

Upvotes: 0

Views: 1572

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54467

Here's some code from a Web Forms project I was part of the team for recently:

private void SetCredentials()
{
    var userName = ConfigurationManager.AppSettings["SSRSUserName"];
    var passwordEncrypted = ConfigurationManager.AppSettings["SSRSUserPasswordEncrypted"];
    var passwordPlainText = SI.Crypto3.Crypto.Decrypt(passwordEncrypted, PASSPHRASE);
    var domain = ConfigurationManager.AppSettings["SSRSUserDomain"];

    if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(passwordPlainText) && !string.IsNullOrEmpty(domain))
    {
        this.EventsHubReportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials(userName, passwordPlainText, domain);
    }
}

That's C# but hopefully you can see that the important part is that last line. I think that the equivalent in your case should be:

ReportViewer1.ServerReport.ReportServerCredentials = New ReportServerCredentials(userName, password, domain)

The domain value can be an empty String if your on the same domain as the server.

EDIT:

I looked more closely and the ReportServerCredentials class that code is using is one of our own. In your case, you can use the Microsoft.ReportViewer.WinForms.ReportServerCredentials class, which I don't think has a constructor like that. Looking at the documentation for the NetworkCredentials property of that type indicates that you need to do this:

Dim credentials As New NetworkCredential(userName, password, domain)

ReportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials = credentials

Upvotes: 0

Related Questions