What
is Crystal Report:-
Crystal Reports is a
business intelligence application used to design and generate reports from a
wide range of data sources like SqlServer Oracle etc.
Crystal Reports can access data from databases and can integrate this data from
multiple databases within one Report using SqlServer Connectivity.
Description:-
we all know that very well Report is the main thing in each and every language. in many way people use report like Bill Generation etc..In this Example We can generate Report of Each row of Gridview When user is Click on Gridview Row. and also Note that we can not use the any table or database but main point is that we can use XML file on which we can Bind data to gridview and perform CRUD operation on grid and Generate Report using CrystalReport.
we all know that XML is also a one kind of Database and in XML that is very Difficult to perform a CRUD operation than an SQLServer database or other database.
to perform operation in XML database first you have to open the file and you have to open it in write mode.
in this Example we can also store the image in XML database and also Bind,edit,Delete image in gridview fetch from XML file. we also provide when user click on each record of gridview then this record is printed on the Crystal Report.
we also provide to generate Report of the Whole Record which are display in Gridview.
to show Example of insert,update,delete in gridview using WCF Service please click here WCF Service For Insert,update,Delete
Export Gridview Data to PDF File Transfer Gridview row to PDF File
Create/Generate Random or Unique Password Generate Password
imagexm.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imagexm.aspx.cs" Inherits="imagexm" %>
<%@ 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:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="City"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
Choose Photo: <asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Update" Visible="false"
onclick="Button2_Click" />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="False"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label Id="l1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label Id="l2" runat="server" Text='<%# Eval("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="80" Width="80" ImageUrl='<%# Eval("photo") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/report.jpeg" Height="80" Width="80" OnClick="ImageButton2_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label3" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:Button ID="Button3" runat="server" Text="Generate Report"
onclick="Button3_Click" />
</div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="True" EnableDatabaseLogonPrompt="False" Height="1039px"
Width="901px" ReportSourceID="CrystalReportSource2" />
<CR:CrystalReportSource ID="CrystalReportSource2" runat="server">
<Report FileName="reportxm.rpt">
</Report>
</CR:CrystalReportSource>
</form>
</body>
</html>
XML FILE:-
<?xml version="1.0" standalone="yes" ?>
- <users>
- <kkkkk>
<name>jhfy</name>
<city>yhj</city>
<photo>1.jpeg</photo>
</kkkkk>
- <kkkkk>
<name>6yr6y</name>
<city>tytr</city>
<photo>3.jpeg</photo>
</kkkkk>
- <kkkkk>
<name>jhj</name>
<city>hjg</city>
<photo>1.jpeg</photo>
</kkkkk>
</users>
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 System.Xml;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using CrystalDecisions.Shared;
public partial class imagexm : System.Web.UI.Page
{
static string strFile_Name;
DataSet2 dd = new DataSet2();
protected void Page_Load(object sender, EventArgs e)
{
/*ReportDocument
reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("reportxm.rpt"));
reportdocument.SetDatabaseLogon("Demoh", "Demo1@",
"SQLDB", "Demo");*/
/*
CrystalReportViewer1.ReportSource = reportdocument;*/
if (!Page.IsPostBack)
GetXMLData();
}
protected void GetXMLData()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("city", typeof(string));
dt.Columns.Add("photo", typeof(string));
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("imagexm.xml"));
XmlNodeList nodeList =
xmldoc.SelectNodes("/users/kkkkk");
foreach (XmlNode node in nodeList)
{
DataRow dtrow = dt.NewRow();
dtrow["name"] =
node["name"].InnerText;
dtrow["city"] =
node["city"].InnerText;
dtrow["photo"] =
node["photo"].InnerText;
dt.Rows.Add(dtrow);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("imagexm.xml"));
XmlElement parentelement =
doc.CreateElement("kkkkk");
XmlElement name = doc.CreateElement("name");
name.InnerText = TextBox1.Text;
XmlElement city = doc.CreateElement("city");
city.InnerText = TextBox2.Text;
XmlElement photo = doc.CreateElement("photo");
/*byte[] imageByte = new
byte[FileUpload1.PostedFile.InputStream.Length + 1];
FileUpload1.PostedFile.InputStream.Read(imageByte, 0,
imageByte.Length);*/
photo.InnerText = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
parentelement.AppendChild(name);
parentelement.AppendChild(city);
parentelement.AppendChild(photo);
doc.DocumentElement.AppendChild(parentelement);
doc.Save(Server.MapPath("imagexm.xml"));
GetXMLData();
clear();
}
protected void Button2_Click(object sender, EventArgs e)
{
String st = Server.MapPath("imagexm.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
ds.Tables[0].Rows[int.Parse(Label3.Text)][0] =
TextBox1.Text;
ds.Tables[0].Rows[int.Parse(Label3.Text)][1] =
TextBox2.Text;
ds.Tables[0].Rows[int.Parse(Label3.Text)][2] =
FileUpload1.FileName;
GridView1.EditIndex = -1;
ds.WriteXml(st);
GetXMLData();
Button2.Visible = false;
Button1.Visible = true;
clear();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String st = Server.MapPath("imagexm.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
ds.Tables[0].Rows.RemoveAt(e.RowIndex);
ds.WriteXml(st);
GetXMLData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label l1 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("l1");
Label l2 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("l2");
TextBox1.Text = l1.Text;
TextBox2.Text = l2.Text;
Button1.Visible = false;
Button2.Visible = true;
Label3.Text =
e.NewEditIndex.ToString();
}
public void clear()
{
TextBox1.Text = "";
TextBox2.Text = "";
}
static byte[] bt;
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;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//Response.Redirect("reportxml.aspx");
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("reportxm.rpt"));
DataSet2 d1 = new DataSet2();
DataSet d2 = new DataSet();
d2.ReadXml(Server.MapPath("imagexm.xml"));
DataTable dt = d2.Tables["kkkkk"];
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = d1.kkkkk.NewRow();
dr["name"] = dt.Rows[i]["name"].ToString();
dr["city"] = dt.Rows[i]["city"].ToString();
string s = Server.MapPath(dt.Rows[i]["photo"].ToString());
bt = ReadImageFile(s, new string[] { ".GIF", ".gif", ".jpg", ".bmp", ".jpeg" });
string msg = "<script
type='text/javascript'>";
msg += " alert('Conversion is successful!') ";
msg += "</script>";
ClientScript.RegisterClientScriptBlock(typeof(Page), "Test", msg);
//
FileUpload1.PostedFile.InputStream.Read(imageByte, 0, imageByte.Length);
dr["photo"] = bt;
d1.kkkkk.Rows.Add(dr);
}
rd.SetDataSource(d1);
CrystalReportViewer1.ReportSource = rd;
CrystalReportViewer1.RefreshReport();
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
ImageButton btndetails = sender as ImageButton;
GridViewRow row =
(GridViewRow)btndetails.NamingContainer;
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("reportxm.rpt"));
DataSet2 d1 = new DataSet2();
Label l1 = (Label)row.FindControl("l1");
Label l2 = (Label)row.FindControl("l2");
Image l3 = (Image)row.FindControl("Image1");
DataRow dr = d1.kkkkk.NewRow();
dr["name"] = l1.Text;
dr["city"] = l2.Text;
string s = Server.MapPath(l3.ImageUrl);
bt = ReadImageFile(s, new string[] { ".GIF", ".gif", ".jpg", ".bmp", ".jpeg" });
string msg = "<script
type='text/javascript'>";
msg += "
alert('Conversion is successful!') ";
msg += "</script>";
ClientScript.RegisterClientScriptBlock(typeof(Page), "Test", msg);
dr["photo"] = bt;
d1.kkkkk.Rows.Add(dr);
//Response.Redirect("reportxml.aspx?l1="
+l1.Text+"&l2="+l2.Text+"&l3="+l3.ImageUrl);
rd.SetDataSource(d1);
CrystalReportViewer1.ReportSource = rd;
CrystalReportViewer1.RefreshReport();
}
}
0 comments:
Post a Comment