Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a web project that saves data to a database. The user is controlled by a session. When a user logs in a session starts. The session is using the users ID number. How can I build a crystal report based on the session ID and the data the user submitted into the database?

C#
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Web;
using System.Web.UI.WebControls.WebParts;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml.Linq;
using System.Web.SessionState;


namespace SACSCOCLogin1._1
{
    public partial class ReportFormA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string SelectFormula = "{COCINST.inst_id} > 10000 "
                + "AND Mid({COCINST.LongName}, 1, 1) = \"A\"";
            }

            CrystalReportViewer1.Visible = true;
            ReportDocument crystalReport = new ReportDocument();
            crystalReport.Load(Server.MapPath("~/Reports/ReportA.rpt"));
            CrystalReportViewer1.ReportSource = crystalReport;
            CrystalReportViewer1.SelectionFormula = "{COCINST.inst_id} = '" + Session["inst_id"].ToString() + "'";
            //CrystalReportViewer1.SelectionFormula = "{FIN2013.inst_id} = '" + Session["inst_id"].ToString() + "'";
        }

        protected override void OnPreRender(EventArgs e)
        {
            Session["inst_id"] = "inst_id";
            CrystalReportViewer1.Visible = true;
            ReportDocument crystalReport = new ReportDocument();
            crystalReport.Load(Server.MapPath("~/Reports/ReportA.rpt"));
            crystalReport.SetDatabaseLogon
            ("Admin", "master22", @"SQL", "Hot");
            CrystalReportViewer1.ReportSource = crystalReport;
            CrystalReportViewer1.SelectionFormula = "{COCINST.inst_id} = '" + Session["inst_id"].ToString() + "'";
            //CrystalReportViewer1.SelectionFormula = "{FIN2013.inst_id} = '" + Session["inst_id"].ToString() + "'";
        }
        
    }
}
Posted
Updated 20-Feb-14 9:17am
v3
Comments
thatraja 19-Feb-14 2:27am    
Where's code for report document? how you're assigning the report to crystal reports viewer? Update your question.
Computer Wiz99 19-Feb-14 5:00am    
I made the report using CR2013 software. I then added it to the web project. Added a new web form, added crystal report viewer. Added the report to the viewer. No code.
thatraja 19-Feb-14 5:44am    
So in report you have connected database using logins & added tables?
Computer Wiz99 19-Feb-14 5:56am    
Yes. What else do I need to do?
thatraja 19-Feb-14 5:58am    
You should filter the data

1 solution

Use the Selectionformula
Creating a Report with a Selection Formula[^]
Sample formula should be like this(Assign the session value to selection formula's field)
C#
crystalReportViewer.SelectionFormula = "{Table.inst_id} = "'" + Session["inst_id"].ToString() + "'";
 
Share this answer
 
Comments
Computer Wiz99 19-Feb-14 6:56am    
You have Table.inst_id. The user id is not a table. Am I saying that right or am I miss reading your code?
thatraja 19-Feb-14 7:11am    
It's just a sample code. Consider you're showing all inst_ids from report herebefore. Now if you want to show particular inst_id, you have to apply selectionformula like above. Recheck the link & sample formula in answer
Computer Wiz99 19-Feb-14 8:49am    
Will this code be on Page_Load?
thatraja 19-Feb-14 8:52am    
Yes, where're calling the report...
And don't forget add some more things like crystalReportViewer.Refresh(); But first try without adding this if not working then call that refresh.
Computer Wiz99 19-Feb-14 8:57am    
Ok. Do I need to build the report through code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900