Hi arehman,
Refering the below link i have added code for saveing .rpt file in database and retrieve rpt from database and display in page.
For saving and retrieving files in database refer below article.
HTML
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="SaveReportFile" />
<asp:Button ID="btnRetrieve" runat="server" Text="Display" OnClick="DisplayReportFile" />
<br />
<CR:CrystalReportViewer ID="crPersonInformation" runat="server" AutoDataBind="true"
EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" ToolPanelView="None" DisplayGroupTree="False" />
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
Code
private PersonInformation GetPersonInformations(string searchTerm)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand("GetPersonInformation");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
sda.SelectCommand = cmd;
using (PersonInformation dsPersonInformations = new PersonInformation())
{
sda.Fill(dsPersonInformations, "PersonInformation");
return dsPersonInformations;
}
}
}
}
protected void SaveReportFile(object sender, EventArgs e)
{
string reportPath = Server.MapPath("~/PersonInformationReport.rpt");
byte[] bytes = File.ReadAllBytes(reportPath);
string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(reportPath));
cmd.Parameters.AddWithValue("@ContentType", "application/octet-stream");
cmd.Parameters.AddWithValue("@Data", bytes);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
protected void DisplayReportFile(object sender, EventArgs e)
{
byte[] bytes = null;
string fileName = "";
string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "SELECT * FROM tblFiles WHERE id = 7";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
bytes = (byte[])sdr["Data"];
fileName = sdr["Name"].ToString();
}
conn.Close();
}
}
string reportSavePath = Server.MapPath("~/Files/" + fileName);
File.WriteAllBytes(reportSavePath, bytes);
string searchTerm = txtSearch.Text.Trim();
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/PersonInformationReport.rpt"));
PersonInformation dsPersonInformations = GetPersonInformations(searchTerm);
crystalReport.SetDataSource(dsPersonInformations);
crPersonInformation.ReportSource = crystalReport;
}
Screenshot
