Home > Forum > ASP.NET > Display multiple images from database table to pdf using stored procedures

Display multiple images from database table to pdf using stored procedures

by chaitanya   on Dec 14, 2015   Category: ASP.NET  |  Views: 1598    |  Points: 5   |  Starter 
  Reply
Hi,

I am new to asp.net, I have requirement like I have to display multiple records from database table to pdf.

Database table contains columns like(candidateid,candidatename,picture,logo,etc). Now I need to print multiple records from these columns using stored procedures.

I am getting multiple records and its data also but I am not able to print the images in pdf. Here I am having a problem to display images from database table to pdf using stored procedures.

Please help me anyone.


Thanks in Advance,
Chaitanya.

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Display multiple images from database table to pdf using stored procedures   
by John Prathip
on Dec 15 2015 4:55AM
Points : 10
Silver 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lbl2" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logo">
<ItemTemplate>
<asp:Image ID="image1" runat="server" ImageUrl = '<%# Eval("Logo", GetUrl("{0}")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnExport" runat="server" Text="ExporttoPDF" OnClick="btnExport_Click"/>
</div>
</form>
</body>
</html>


protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
BindGrid();
}

}
public void BindGrid()
{
String strConnString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllEmployeeDetails";
cmd.Connection = con;
try
{
con.Open();
GridView1.EmptyDataText = "No Records Found";
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected string GetUrl(string imagepath)
{
string[] splits = Request.Url.AbsoluteUri.Split('/');
if (splits.Length >= 2)
{
string url = splits[0] + "//";
for (int i = 2; i < splits.Length - 1; i++)
{
url += splits;
url += "/";
}
return url + imagepath;
}
return imagepath;
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindGrid();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}



Create PROCEDURE [dbo].[GetAllEmployeeDetails]
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, Logo
FROM Employees
END


CREATE TABLE [dbo].[Employees](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](128) NULL,
[LastName] [varchar](128) NULL,
[Logo] [varchar](128) NULL
)


Table records example :


Id FirstName LastName Logo

2 john1 prathip1 images/cinqueterre.jpg



Add itextsharp.dll to your project.
  Re :Display multiple images from database table to pdf using stored procedures   
by chaitanya
on Dec 15 2015 10:16PM
Points : 10
Starter 
Hi John,

Thanks for your reply.

I tried your code but I am not getting the image in grid view and when I clicked Export to PDF button it is throwing error(URI Formats are not supported), pdf also not generating.

Actual thing is I am generating pdf without using grid view in my code. Code behind I have written a logic for fetching data directly from database table to pdf. Expected data is printing but I am not getting images in my PDF.
Here is my code for generating table from database table to PDF:

using (SqlConnection con1 = new SqlConnection("Data Source=servername;Initial Catalog=mydb;User ID=sa;Password=sa"))
{

using (SqlCommand com1 = new SqlCommand("candidatedetails", con1))
{


com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.AddWithValue("@orgid", orgid.Text);
con1.Open();
com1.ExecuteNonQuery();
DataTable dta1 = new DataTable();
SqlDataAdapter at1 = new SqlDataAdapter(com1);
at1.Fill(dta1);

sb.Append("<br />");
sb.Append("<table border = '0'>");
sb.Append("<tr>");
foreach (DataColumn column in dta1.Columns)
{

sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dta1.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dta1.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");

//Byte[] bytes1 = (Byte[])dta1.Rows[0]["candidate_pic"];
//Byte[] bytes2 = (Byte[])dta1.Rows[0]["candidate_symbol"];

}
sb.Append("</tr>");
}

sb.Append("</table>");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<table border=0>");
sb.Append("<tr colspan = 3><td align:left><b>");
sb.Append(OrgSeal.Text);
sb.Append("</b></td>");
sb.Append("<td align:center><b>");
sb.Append(ReturnOff.Text);
sb.Append("</b></td>");
sb.Append("<td align:right><b>");
sb.Append(Sign.Text);
sb.Append("</b></td></tr>");
sb.Append("</table>");
StringReader sr = new StringReader(sb.ToString());
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=" + fileName + "ManualOptions.pdf");
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytesd = memoryStream.ToArray();
HttpContext.Current.Response.BinaryWrite(bytesd);
memoryStream.Close();
bytes = bytesd;

con1.Close();

}

}
}


Stored Procedure:
ALTER procedure [dbo].[candidatedetails]
@orgid varchar(50)
as
begin
select a.pos_type, b.candidatefirst_name,b.candidatelast_name,b.candidate_pic,b.candidate_symbol from positions1 a,EV_CandidateDetails1 b where a.org_id=@orgid and a.pos_id=b.pos_id order by b.pos_id asc
end

In my table I have taken Image data type for candidate_pic,b.candidate_symbol columns.

Please help me to complete this.

Thanks in Advance,
Chaitanya.

Recent Post

Latest Posts

1 3D model and 2D View New! Forum