2 years ago
#59237
Anik Saha
Generate Crystal Report Dynamically reading Store Procedure
I am developing a web form application. Here a Create a table named ReportConfig which contains ReportId, ReportCode, ReportName and Query column. An User will put there Store Procedure or select statement in the Query column. Then, I made another Page named ReportDownload where an user will download a report based on the input he/she gave on the ReportConfig--> Query
Is it possible to create Crystal Report from every Store Procedure or Select statement from query column ?
Here is my ReportConfig page.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ReportManager
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadRecord();
}
}
SqlConnection con = new SqlConnection(@"Data Source=ANIK-IT\SQLEXPRESS;Initial Catalog=ReportManager;Persist Security Info=True;User ID=sa;Password=oLdViCtOrY2008");
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand comm = new SqlCommand("Insert into ReportConfig values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextArea1.InnerText + "')", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Inserted');", true);
LoadRecord();
}
void LoadRecord()
{
SqlCommand comm = new SqlCommand("Select * from ReportConfig", con);
SqlDataAdapter d = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
d.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand comm = new SqlCommand("Update ReportConfig Set ReportName = '" + TextBox2.Text + "', Query = '" + TextArea1.InnerText + "' Where ReportCode= '" + TextBox1.Text + "' ", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
LoadRecord();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand comm = new SqlCommand("Delete ReportConfig Where ReportCode = '" + TextBox1.Text + "' ", con);
comm.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Deleted');", true);
LoadRecord();
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlCommand comm = new SqlCommand("Select * from ReportConfig Where ReportCode = '" + TextBox1.Text + "'", con);
SqlDataAdapter d = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
d.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button5_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand comm = new SqlCommand("Select * from ReportConfig Where ReportCode = '" + TextBox1.Text + "'", con);
SqlDataReader r = comm.ExecuteReader();
while (r.Read())
{
TextBox2.Text = r.GetValue(2).ToString();
TextArea1.InnerText = r.GetValue(3).ToString();
}
con.Close();
}
}
}
Here is my ReportDownload page
using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ReportManager
{
public partial class ReportDownload : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=ANIK-IT\SQLEXPRESS;Initial Catalog=ReportManager;Persist Security Info=True;User ID=sa;Password=oLdViCtOrY2008");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string com = "Select * from ReportConfig";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "ReportName";
DropDownList1.DataValueField = "ReportId";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string chk = DropDownList1.SelectedItem.Text;
string qur = String.Format("Select Query from ReportConfig Where ReportName ='" + chk + "'");
SqlCommand cmd = new SqlCommand(qur, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
string dsa = ds.Tables[0].Rows[0][0].ToString();
SqlCommand cmd1 = new SqlCommand(dsa, con);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataSet ds1 = new DataSet();
sda1.Fill(ds1);
ReportDocument crp = new ReportDocument();
crp.Load(Server.MapPath("ReportViewer.rpt"));
crp.SetDataSource(ds1.Tables["table"]);
CrystalReportViewer1.ReportSource = crp;
crp.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, false, "Report Config");
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),"err_msg",
"alert('Invalid Store Procedure!)');",true);
}
}
}
}
also you can see what i do by downloading the project here
https://drive.google.com/file/d/1GsZSGFmHoINwyuorx3n-o0sH3U8cL2rS/view?usp=sharing
c#
dynamic
webforms
crystal-reports
0 Answers
Your Answer