How to add parameters to SSRS URL in report viewer? - asp.net

In reference to this article:
http://msdn.microsoft.com/en-us/library/ms252072(VS.80).aspx
I can access my SSRS report from my Web Application but the problem is it does not accept the parameters in the URL.
here is the format that it accepts
Report Server URL:
http://ABCD.com/reportserver
Report Path:
/Company works/Sales report/ABC
Can anyone help me?
I want to pass parameters in the SSRS url through report viewer.

You can not pass parameters via the URL since the control is expecting on the URL to the actual reportserver and nothing more.
Example:
serverReport.ReportServerUrl = new Uri("http://ABCS.com/reportserver");
serverReport.ReportPath = "/AdventureWorks Sample Reports/My Report";
You can pass these parameters in your code behind though by using the ReportViewer.ServerReport.SetParameters() function. Here is a snippet of code I used for this purpose:
var parametersCollection = new List<ReportParameter>();
//add generic parameters that every report needs
parametersCollection.Add(new ReportParameter("RandomizerString", new Random().Next().ToString(), false));
parametersCollection.Add(new ReportParameter("MachineName", Request.UserHostAddress.ToString(), false));
ERDashboard.ServerReport.SetParameters(parametersCollection);

can't seem to add a comment to Sam's answer so I'll post another:
The ReportViewerControl ReportServerURL property is a lot different than formatting a general URL to access a report. You could use the logic that sam has provided to navigate a web browser control to that URL which would also render the report within your web app. It's another to try if you want.
As a note there are various reasons that you may choose a webbrowser control vs a reportviewer control. The reportviewer control is a lot easier to manage in the code-behind, but there is a memory issue when refreshing a report for extended periods of time (lots of information through google about memory leaks). This is the scenario where I have instead used the webbrowser and manually formatted URLs to pass parameters instead.

Related

PDF creation tool that can handle Client-side view changes

I am trying to find a tool that can be used to create PDFs from websites. These websites all have Bootstrap based client side view settings, such as tabs, toggles, and paging. As such, there is no post-back to the server. I need to be able to create a pdf that is in the same state as the user sees it.
In my research, I have only been able to find tools that can create PDFs if given a URL, or if given a HTML string. Examples of these tools include ActivePDF's Webgrabber, and EVO PDF. However, they are not able to generate the PDF's with the client-specific settings, but instead only see the default selections of a given page. It is not possible for me to do a post-back to the server, so I am looking for a tool that can create PDFs on the fly, with the dynamic settings intact. I am working in ASP.NET, and so I would like a tool that is .NET friendly as well. Lastly, I would prefer the tool to not be open-sourced.
It sounds like a proper solution here would be to take data from your client side, post it via AJAX (not an ASP.NET postback!) and then process it on the server side to generate the PDF. Since you haven't given much detail, I'm going to assume we don't need to send over all the HTML, but rather values that were entered via a form.
<script>
function postDataToServer(){
var itemId = $("#ItemIdTB").val();
var quantityOrdered = $("#QuantityTB").val();
$.ajax({
url : "GeneratePdfFromForm?itemId="+itemId+"&quantityOrdered="+quantityOrdered
})
.done(function(){alert("Success!");})
.fail(function(){alert("Failure!");});
}
</script>
My answer is using jQuery, which is a JavaScript library that simplifies AJAX and DOM manipulation. However, this technique is doable with other JavaScript libraries (or even without a library).
On the server side, let's have an ASP.NET Web API function that can handle that AJAX Post.
[Route("GeneratePdfFromForm")]
public static void GeneratePdfFromForm(string itemId, int quantityOrdered)
{
Debug.WriteLine("Received itemId {0} and quantity {1}", itemId, quantityOrdered);
byte[] pdf = GeneratePDF(itemId, quantityOdered); //you'll need a function called GeneratePDF that can generate your PDF based on the parameters, probably using a library like iTextSharp.
//now do you want with the PDF byte array
}
So that's how you'd generate the PDF. You could return it from the GeneratePdfFromForm() function back to the client. But since that's potentially a long running task, you should probably implement it in the background using something like Hangfire, then when the PDF is ready you'd present it to the client for download (perhaps using SignalR or jQuery AJAX polling to alert the client of when the PDF is ready).
I was passing data from the client side to the server side via query string. You could instead create a class to represent the parameters for your PDF generation, then pass that form the client side to the server side via jQuery AJAX's data parameter.
If you really want to post the entire HTML from the client side to the server, you could so something like this:
var html=$("html").html();
$.ajax({
url: <your url here>,
data: html,
contentType: "text/html"
});
However, I'm willing to bet the actual HTML isn't what you care about for generating the PDF, but rather the selected values from some client side form. The actual HTML would include everything on the client side, including navigation menus, scripts etc. You could post a subset of the HTML using a different selector (ex: $("#OrderDiv").html()), which is a technique some people use to generate PDF's. But I think it's much cleaner to decouple your HTML intended for the browser from the way the PDF is generated, so that changes to your site don't mess up the PDF. You can then use the PDF creating library's capabilities to build the PDF rather than using HTML.

Pick a random url from list and display as URL

Im using Visual Studio 2012 and creating a web page using "ASP.NET Web Site (Razor v2)"
Im using Java to generate a random link;
<script>
var random = new Array();
random[0] = "example1.com";
random[1] = "pattern1.com";
random[2] = "specimen1.com";
</script>
<script>
function randomlink() {
window.location = random[Math.floor(Math.random() * random.length)];
}
</script>
A Random URL
When I click the A Random URL link it opens a random page from the list in the script above. I'ts all good, but because its a verry big list I need a way to do the same without having it in HTML because its slowing the loading of the page since its in the _SiteLayout.cshtml. Thanks.
Among your choices are the following options:
Send all the URLs to the client and have the client pick a random choice.
Have the server pre-pick the random URL and send only that one to the client (it can just be put directly into the <a> link. No need for javascript at all.
Make an ajax call to the server to request a random link and when that is returned, go to it.
Make a get request to the server and have the server return a redirect to a randomly chosen URL.
It sounds like you don't want to implement the first option if you have a zillion URLs.
The second option is probably the easiest as it requires only slightly modifying the generation of the page and requires no new server APIs. You just have to figure out how to select a random URL in your server-side environment.
The third and fourth options are the least efficient as they require a call to the server, a response from the server with the new URL and then a client redirect to the actual URL.
I would pass the random url with the page when it renders from the server. You can generate the url on the server using c#'s Random class.
A Random URL
Just pass a model that you reference in your view.

Send query string parameter from a non-web application

Ok, I've been bugging with this for too long.
I need to call my website from a rough VB.net app. Then only thing I need is to attach a query string parameter to the calling url, so I can distinguish the pages to be shown to different VB app users.
So I want to click a button and launch that website, giving this parameter.
First I was bugging with adding the system.web libraries. Now I can't use Request/Response.QueryString as well.
I tried getting some example help from this post. but as I said before - I cannot make use of Request.QueryString as I cannot import it.
I am stuck here:
Process.Start("http://localhost:56093/WebSite1?id=")
I need to attach a query string parameter to the url and then open the website with that url.
Can someone just give me a sample code for my problem.
Query parameters are parsed by the web server/http handler off the URL you use to call the page. They consist of key and value pairs that come at the end of the URL. Your code is nearly there. Say you needed to pass through the parameters:
ID = 1234
Page = 2
Display = Portrait
Then you'd turn them into a URL like this:
http://localhost:56093/WebSite1?ID=1234&Page=2&Display=Portrait
Therefore in your code you'd have:
Process.Start("http://localhost:56093/WebSite1?ID=1234&Page=2&Display=Portrait");

ASP.NET ReportViewer Parameter Prompt doesn't show

I've been looking for this answer for a couple days now and I've been getting little bits of information that make it seem that you can have ReportViewer Control automatically prompt for the report's parameters. Just everything that I've tried and found doesn't seem to work. I've gotten the Parameter Prompts to work on a Windows Form but I just cannot get it to work in ASP.NET
I guess I'm simply asking can you get Report-viewer's Parameter Prompts to work in ASP.NET? if so, How?
I know you can do it manually, it's just, I feel if you can make ReportViewer Prompt automatically why program it yourself?
Edit: this is for local processing btw.
Prompting for parameters is not supported in local processing mode.
In article Report Parameters Dialog Box (Visual Studio Report Designer), which is invoked by clicking the Help button on that dialog, it says in the introductory text that:
The parameters properties that you specify in the Report Parameters
dialog box become part of the report definition. Some properties are
intended for programmatic use only. In contrast with reports that are
processed on a remote report server, a locally processed report does
not have a parameter input area used for selecting or typing parameter
values.
A little testing shows me that the default values specified for the parameters will be used, unless you modify them programmatically. I could not find an explanation on this design decision. If you want to use local processing, and prompt for user input, I would recommend to follow this solution:
I you embed the reports in a ReportViewer Control, you can put it on a page or form and add custom input controls to that page or form to gather report parameters. In the code-behind files, you will then pass the parameter values using code like this:
List<ReportParameter> parameterList = new List<ReportParameter>();
List<string> selectedProductTypes = listboxProductTypes.GetSelectedValues();
ReportParameter productTypes = new ReportParameter("ProductTypes", selectedProductTypes.ToArray(), false);
ReportParameter username = new ReportParameter("Username", "<current user>", false);
parameterList.Add(productTypes);
parameterList.Add(username);
reportViewer.LocalReport.SetParameters(parameterList);
In this example, you can see how to pass a multi-valued parameter, the values of which are taken from a multi-select ListBox.
You can also create a page that has the controls to gather the parameter input, put them in a set session variables and then transfer to the page that has the report viewer.
use object data source and set the parameter source as Session.
The only code you need to write is filling the session variables.

Passing Arguments to an ASP .NET Page

So I've recently started working with ASP .NET but I am very familiar with other .NET frameworks. The application I'm working on needs to take in an string index as a command line-style argument. Right now in my Page class I have a const string that I'm using as a placeholder. So the question I have is a two parter:
1- How do I set up my applications innerts to recieve an argument that is passed in when the page is opened?
2- How do I pass that argument in to the page (especially while I'm working in VS 2010)?
Much Thanks!
You pass arguments to web pages with request variables (ever hear of POST or GET?). The simplest way is to append the variables to the end of the url like this:
http://localhost/Default.aspx?varname=varvalue&othervarname=othervarvalue
In your codebehind, you can access the request variables with Request.QueryString.
For #2: In your project config on VS2010, to to the 'Web' tab and set the option to specific page with the desired URL: http://localhost/Default.aspx?varname=varvalue&othervarname=othervarvalue

Resources