Sunday 21 April 2013

Crystal Reports Display Images From Database in Asp.Net



Description:-

                 In this Example we define that how to Display Image in crystal Report Retrive From database.for example we have one record with name,address and third is it's Image. so that's type of data how we can Display in CrystalReport when user click on each row of Gridview The same record are display in Report and you can also navigate the record printing record in report.

In Simple you Bind the data in CrystalReport then it will Display Name of the Image not Image so avoid this Sitution we Have to Convert this Image in ByteArray If DataType of the Photo or Image Coloumn in database is String if DataType is Image then you did not Require to Covert into ByteArray Because It also Return ByteArray.

If Your Coloumn(photo,iamge etc..) Defines with String DataType then First You Have to Convert it into ByteArray. To Convert this Code is Here

public static byte[] ReadImageFile(string PostedFileName, string[] filetype)
    {
        bool isAllowedFileType = false;
        try
        {
            FileInfo file = new FileInfo(PostedFileName);

            strFile_Name = file.Name;

            foreach (string strExtensionType in filetype)
            {
                if (strExtensionType == file.Extension)
                {
                    isAllowedFileType = true;
                    break;
                }
            }
            if (isAllowedFileType)
            {
                FileStream fs = new FileStream(PostedFileName, FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(fs);

                byte[] image = br.ReadBytes((int)fs.Length);

                br.Close();

                fs.Close();

                return image;
            }
            return null;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }


To Call this Function Pass Parameter Like this

Byte[]  bt = ReadImageFile(s, new string[] { ".GIF", ".gif", ".jpg", ".bmp", ".jpeg" });

To Show How To Convert ByteArray to Image or Image to ByteArray Click this Link Convert ByteArray to Image And Image to ByteArray

Upload File and Bind to Gridview Upload and Bind Image to Gridview

AutoComplete TextBox Search Example Autocomplete Search in Ajax


report.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="report.aspx.cs" Inherits="report" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /><br />
        <br />
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
            AutoDataBind="True" EnableDatabaseLogonPrompt="False" Height="1039px"
            Width="901px" />
        <%--<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
            <Report FileName="CrystalReport2.rpt">
            </Report>
        </CR:CrystalReportSource>--%>
    </div>
    </form>
</body>
</html>



report.aspx.cs:-


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
using System.Text;

public partial class report : System.Web.UI.Page
{
    string conn = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
    protected void Page_Load(object sender, EventArgs e)
    {
        /* ReportDocument reportdocument = new ReportDocument();
         reportdocument.Load(Server.MapPath("CrystalReport2.rpt"));
         reportdocument.SetDatabaseLogon("Demod", "Demo1@", "SQLDB", "Demo");
         CrystalReportViewer1.ReportSource = reportdocument;*/

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conn);
        ReportDocument rd = new ReportDocument();
        rd.Load(Server.MapPath("CrystalReport.rpt"));
        DataSet1 d1 = new DataSet1();
        con.Open();
        SqlDataAdapter adp = new SqlDataAdapter("select * from v_emp", con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = d1.v_emp.NewRow();

            dr["empname"] = dt.Rows[i]["empname"].ToString();
            dr["salary"] = dt.Rows[i]["salary"].ToString();
            dr["desig"] = dt.Rows[i]["desig"].ToString();
            // string text = Encoding.UTF8.GetString(dt.Rows[i]["photo"]);
            dr["photo"] = dt.Rows[i]["photo"];
            d1.v_emp.Rows.Add(dr);

        }
        rd.SetDataSource(d1);
        CrystalReportViewer1.ReportSource = rd;
        CrystalReportViewer1.RefreshReport();



    }
}













0 comments:

Post a Comment