Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string SQL = "SELECT * FROM DPSpdf WHERE intpdfId='" + txtdanceid.Text + "'";
        SqlCommand myCommand = new SqlCommand(SQL, (SqlConnection)Application.Get("DPS"));
        myCommand.Parameters.AddWithValue("@intpdfId", txtdanceid.Text);

        SqlDataReader myReader = myCommand.ExecuteReader();

        if (myReader.Read())
        {
            Response.ContentType = myReader["ContentType"].ToString();
            Response.BinaryWrite((byte[])myReader["strpdfurl"]);

        }

        myReader.Close();
        Response.End();


Where i did mistake?
Posted
Updated 6-Feb-11 19:32pm
v2

Hi,

You are passing inline parameter and command parameter at a time. You need to choose one of them, better choice is passing command parameter instead of inline value.

Please see the modified sql statement below,

C#
string SQL = "SELECT * FROM DPSpdf WHERE intpdfId=@intpdfId";
SqlCommand myCommand = new SqlCommand(SQL, (SqlConnection)Application.Get("DPS"));
myCommand.Parameters.AddWithValue("@intpdfId", txtdanceid.Text);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
   Response.ContentType = myReader["ContentType"].ToString();
   Response.BinaryWrite((byte[])myReader["strpdfurl"]);
}

myReader.Close();
Response.End();
 
Share this answer
 
Comments
Rupa1 7-Feb-11 1:51am    
sir not coming where i am doing mistake?
Shahriar Iqbal Chowdhury/Galib 7-Feb-11 2:00am    
did u debug the code? is data returned from db?
Rupa1 7-Feb-11 4:36am    
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

An invalid character was found in text content. Error processing resource 'http://localhost:3055/DPS/testpdf.aspx'. Line 2...

%

ya finally coming error this one sir where i am doing mistake.......
Try this code


System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] data = (byte[])myReader["strpdfurl"];
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;

Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = myReader["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + "Test" + ".pdf");
Response.BinaryWrite(data);
Response.Flush();
Response.End();
 
Share this answer
 
Comments
Rupa1 7-Feb-11 2:33am    
string SQL = "SELECT * FROM DPSpdf WHERE intpdfId=@intpdfId";
SqlCommand myCommand = new SqlCommand(SQL, (SqlConnection)Application.Get("DPS"));
myCommand.Parameters.AddWithValue("@intpdfId", txtdanceid.Text);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read())
{

byte[] data = (byte[])myReader["strpdfurl"];
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = myReader["ContentType"].ToString();
Response.BinaryWrite(data);
myReader.Close();
Response.End();


}
i implemented like this but not showing pdf file sir................
Rupa1 7-Feb-11 2:34am    
in binary data coming data but it's not able to write data
soni uma 7-Feb-11 2:41am    
I think there is problem in what you store into database. Because the code i give you is working example. Check your code that you store data into binary format.
Rupa1 7-Feb-11 2:45am    
ya i gave data field strpdfurl as varbinary(MAX) is it wrong?
soni uma 7-Feb-11 2:51am    
yes wrong. If you use Mysql than use LongBlob if Mssql than
use binary or search on that.
VB
Server Error in '/WebSite2' Application.
ExecuteReader: Connection property has not been initialized.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900