Tuesday, December 6, 2011

How to Convert PdfBinary to PDF

In Html Page
<table width="100%">
<tr>
<td align="right">
</td>
</tr>
<tr>
<td>
<iframe id="ipdfviewer" width="100%" height="400px" runat="server"></iframe>
</td>
</tr>
</table>

In Cs Page

void ShowPdf()
{
Con.Open();
string Qry = "Select * from TblLabTempReport";
SqlCommand Cmd = new SqlCommand(Qry, Con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(Cmd);
da.Fill(ds);

string FileName;
byte[] byteArray;

if (ds != null && ds.Tables[0].Rows.Count > 0)
{
string LocationPath = "PdfContent/";
string WriteFilePath = Server.MapPath("~/PdfContent/");

byteArray = (byte[])ds.Tables[0].Rows[0]["PdfBinary"];
FileName = "Sample.pdf";
LocationPath = LocationPath + FileName;
WriteFilePath = WriteFilePath + FileName;
if (!File.Exists(WriteFilePath))
{
System.IO.File.WriteAllBytes(WriteFilePath, byteArray);
}
ipdfviewer.Attributes.Add("src", LocationPath);
}
Con.Close();
}

Command Parameters Declaration

Type 1
Con.Open();
string Qry = "Update tblcentreselection set ImgHeaderBinayByte=@ImgBinary,ImgExtension=@ImgExtention where Centreid=@CenterId";
SqlCommand cmd = new SqlCommand(Qry, Con);
cmd.Parameters.Add("@ImgBinary", SqlDbType.VarBinary);
cmd.Parameters["@ImgBinary"].Value = ImgBinary;
cmd.Parameters.Add("@ImgExtention", SqlDbType.VarChar);
cmd.Parameters["@ImgExtention"].Value = StrExtension;
cmd.Parameters.Add("@CenterId", SqlDbType.Int);
cmd.Parameters["@CenterId"].Value = Convert.ToString(Session["_SelCen"]);
cmd.ExecuteNonQuery();
Con.Close();
Type 2
Con.Open();
string Qry = "Update tblcentreselection set ImgHeaderBinayByte=@ImgBinary,ImgExtension=@ImgExtention where Centreid=@CenterId";
SqlCommand cmd = new SqlCommand(Qry, Con);
Cmd.Parameters.AddWithValue("", OrgId);
Cmd.Parameters.AddWithValue("", ExVisitId);
Cmd.Parameters.AddWithValue("", ReportType);
Cmd.Parameters.AddWithValue("", LabPdfBinary);
cmd.ExecuteNonQuery();
Con.Close();