This is a small tutorial on getting/reading submitted data from ASP.NET pages.

In PHP, you write a form, put in your inputs (i.e. test_name), then some buttons. Usually you would catch this by doing a:

echo $_POST['test_name'];

In ASP.NET C# you get your post variables by using Request.Form:

Request.Form["test_name"]

To send the data, with your submit button, simply have the METHOD=”?action=update” or if you’re using an ASP Button control, the PostBackURL should be “?action=update”

Now you need to catch the post back, and do something with the information you’ve sent to the server.

To see what button has been clicked, use QueryString:

                Request.QueryString["action"];

Put all this together:

                string action = Request.QueryString["action"];
                if (action == "update")
                {
                    name = Request.Form["test_name"];
                }

Easy! :)

Tags: , , , ,

· · · ◊ ◊ ◊ · · ·

At work the other day, we had an issue regarding reporting services. We have two servers, one is the test and one is the live server.

The requirement was:

  1. When debugging the web application, point to the test Reporthost
  2. When running in ‘Release’, point to the live Reporthost

We figured that we could use web.config transformations – that is, two different web.config files for the two solution configurations: Web.Config, Web.Debug.Config, and Web.Release.config.

Unfortunately, we found that this wouldn’t work after a lot of research and trial and error – it only works when the project is published (deployed).

Refusing to be beaten by Visual Studio, we decided to use the Build Event feature, and for whatever solution configuration the project was currently set to, we would search the specified files and replace the text (i.e. From TestReporthost to LiveReporthost). We shortly realized that we would need a third party tool to help with this, and that was the FART tool (http://fart-it.sourceforge.net/).

The prebuild event:

if $(ConfigurationName) == Debug (
$(SolutionDir)debug.bat
) ELSE (
$(SolutionDir)release.bat
)

The bat file(s):

X:\App\fart.exe -i <pathtofile> <find> <replace>
EXIT 0

Everything worked beautifully, and the requirements were met.

Tags: , , , , , , , , ,

· · · ◊ ◊ ◊ · · ·

When you’re working with ASP.NET (C#) and the ReportViewer control, you will probably want to export that report as CSV or Excel (programmatically).

Here’s a snippet of what I’ve used. Simply call the method and voila, you’ll be asked to download the file.

Export(ReportViewer1, “CSV”);
Export(ReportViewer1, “XLS”);

      public bool Export(ReportViewer viewer, string exportType)
        {
            Warning[] warnings = null;
            string[] streamIds = null;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            string deviceInfo = string.Empty;
            string filetype = string.Empty;
            string reportsTitle = GetReportTitle(viewer); // just gets the Report title... make your own method

            //ReportViewer needs a specific type (not always the same as the extension)
            if (exportType == "XLS")
            {
                filetype = "Excel";
            }
            else
            {
                filetype = exportType;
            }

            byte[] bytes = viewer.ServerReport.Render(filetype, null, // deviceinfo not needed for csv
                out mimeType, out encoding, out extension, out streamIds, out warnings);

            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Clear();

            System.Web.HttpContext.Current.Response.ContentType = mimeType;
            System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + reportsTitle + "." + exportType);

            System.Web.HttpContext.Current.Response.BinaryWrite(bytes);

            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();

            return true;
        }

It would be incredibly easy to turn this into a class, that can do a whole bunch of different formats. It’s very easy to turn this code into exporting a PDF document — but it’s a wee bit different.

To select which format I wanted a report to be exported as, I used the following:

Markup:

                        <asp:DropDownList id="DropDownListExport" runat="server" >
                             <asp:ListItem Value="XLS">Excel</asp:ListItem>
                             <asp:ListItem Value="CSV">CSV</asp:ListItem>
                        </asp:DropDownList>
                        <asp:Button id="Button1"  Text="Export" OnClick="Export_Click" runat="server"/>

Code-behind:

        public void Export_Click(object sender, EventArgs e)
        {
            string type = DropDownListExport.SelectedItem.Value;

            Export(type, ReportViewer1);
        }

If you’d like any help regarding this, leave a comment.

Tags: , , , , , , , ,

· · · ◊ ◊ ◊ · · ·