473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Response.BinaryWrite question

Hi!

My ASP.NET page gets a pdf-file from a SQLServer database and writes it in a
browserwindow, using Response.BinaryWrite. It basically looks like this:

public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Byte[] myPdf = GetPdf();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(myPdf);
Response.Flush();
Response.End();
}

private Byte[] GetPdf()
{
//Code to get pdf from DB
}

//etc.
}

I noticed everytime I call the page, Page_Load is executed twice... Could
someone explain this behaviour? How can I get rid of this?

Thanks!
Nov 16 '05 #1
3 7554
spmm# wrote:
Hi!

My ASP.NET page gets a pdf-file from a SQLServer database and writes
it in a browserwindow, using Response.BinaryWrite. It basically looks
like this:
[snip]
I noticed everytime I call the page, Page_Load is executed twice...
Could someone explain this behaviour? How can I get rid of this?

Thanks!


AutoEventWireup set to true? Check the "Web Form Designer Generated code"
region to see if Page_Load is attached twice to the Load event..

Hans Kesting
Nov 16 '05 #2
> AutoEventWireup set to true? Check the "Web Form Designer Generated code"
region to see if Page_Load is attached twice to the Load event..

Hans Kesting


Where do I set AutoEventWireup? I didn't attach Page_Load twice, so that
can't be it. Below my full code... if I step through Page_Load, after
Response.End() it jumps back to byte[] myPdf = GetPdf(); instead of showing
the pdf in my browser window. Any other ideas?? Thanks.

using System;
using System.IO;

namespace WebForm1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
byte[] myPdf = GetPdf();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(myPdf);
Response.Flush();
Response.End();
}

private byte[] GetPdf()
{
//Code to get pdf from disk
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream(@"C:\somefile.pdf", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
return Buffer;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Nov 16 '05 #3
Sounds like this might be your problem:

http://www.eggheadcafe.com/forums/fo...=14821&INTID=5

I'm running into the same issue and haven't seen a lot on the web about
this. I'm thinking about opening a problem report with MS if I don't get an
answer soon.

Steve Fletcher

"spmm#" wrote:
AutoEventWireup set to true? Check the "Web Form Designer Generated code"
region to see if Page_Load is attached twice to the Load event..

Hans Kesting


Where do I set AutoEventWireup? I didn't attach Page_Load twice, so that
can't be it. Below my full code... if I step through Page_Load, after
Response.End() it jumps back to byte[] myPdf = GetPdf(); instead of showing
the pdf in my browser window. Any other ideas?? Thanks.

using System;
using System.IO;

namespace WebForm1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
byte[] myPdf = GetPdf();
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(myPdf);
Response.Flush();
Response.End();
}

private byte[] GetPdf()
{
//Code to get pdf from disk
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream(@"C:\somefile.pdf", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
return Buffer;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zam | last post by:
Hello World, Under Windows 2003 Server. IIS6. The following code working fine for small files, and for files with size about few megabytes. If I am trying to send HUGE file -- about 700...
5
by: katrinaVictim | last post by:
Question: I get the eror listed at the bottom of the post. What can I do to make the response of the x1.send a "binary" type? Or, in general, how can I just "make this work"? <%@...
11
by: Russ | last post by:
My web app writes some binary data to a file at the client site via Response.Write and Response.BinaryWrite. This action is accomplished in response to a button click, with C# code behind as...
3
by: spmm# | last post by:
Hi! My ASP.NET page gets a pdf-file from a SQLServer database and writes it in a browserwindow, using Response.BinaryWrite. It basically looks like this: public class WebForm1 :...
1
by: Alex Nitulescu | last post by:
Hi. I have a WebService which provides a webmethod which returns a byte array representing a picture. The method's signature is: <WebMethod(Description:="Shows something nice every time !")>...
5
by: twiggy182 | last post by:
Hi, I really need you help because I'm not very familliar with ASP and I could not find any solution to my problem. To put you in situation, I have a CGI to which I send a file name, and that...
1
by: mattridings | last post by:
Hi gang, Have a script that works fine. However, it's really cpu intensive and I'm looking for suggestions on a) whether or not that's normal and if so b)a better way of doing it. Script is...
0
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, first question: shouldn't response.binarywrite be able to take my word doc that's stored in a binary field in my database and open it in word within the my browser? second question:...
1
by: .nLL | last post by:
hi everyone. i use asp to send files to users to prevent hotlinking and i always used following code Response.ContentType="video/3gpp" response.addheader "Content-Disposition",...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.