Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Selection Formula in the code behind the form that has the crystalreportviewer on it. I am trying to display a different report for each user when they login. When a user logs in the session starts. The session goes by their INST_ID. Right now when a user is logged on and clicks the print button a print preview pops up and shows the report. The report that is displayed is not the report for that user. It is showing the report of another user. Is there a way to filter the reports by the users INST_ID. So when they log on and click print it will display the preview of the report for that user?

Here is my code behind for the crystalreortviewer:

C#
using System;
using System.Collections.Generic;
using System.Collections;
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)
        {
            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() + "'";
        } 
    }
}


This is the error that I get when I click on the print button:
HTML
A number is required here. Details: errorKind Error in File FormAReport {F1AAC878-E2B7-4104-BE51-B9CC0023DD43}.rpt: Error in formula Record Selection: '{TableCOCINST.INST_ID} = '10960'' A number is required here. Details: errorKind


I have no Selection Formula in CR.
Posted
Updated 21-Feb-14 5:04am
v3

You can put a string in a Selection Formula in Crystal Reports eg:

{COURSE.title} = 'M.tech'

First you open that report : ReportA.rpt in Crystal Reports 10/ 8.5 then you directly give that formula in {TableCOCINST.inst_id} = 10000 in Reports > SelectionFormula >Records
 
Share this answer
 
Comments
Computer Wiz99 20-Feb-14 5:28am    
Thanks. I will put this same formula in my code behind in page_load along with the session to pull the right data?
Computer Wiz99 20-Feb-14 10:10am    
k07, I put the code you gave me in but the error pops up asking for a number. Why is that? This is what I have: {TableCOCINST.INST_ID} = 'inst_id'. I am trying to show the report data depending on the users ID that is in a session.
I told you to check the formula using Record Selection Formula Editor[^].
See this example, Crystal reports - Creating Record Selection Formulas[^]

What's the datatype of INST_ID? If it's string then you should enclose value with single quote like below
CrystalReportViewer1.SelectionFormula = "{TableCOCINST.inst_id} = '10000'";

And why did you paste the code in both events? Remove the code from Page_Load()
 
Share this answer
 
Comments
Computer Wiz99 20-Feb-14 10:12am    
thatreja, The data type is int for INST_ID.
thatraja 21-Feb-14 2:00am    
If int, remove single quotes. Are you getting any error? Seriously it's simple task but tricky, you could solve this. Let me know.
Computer Wiz99 21-Feb-14 8:26am    
Well, this is what I am trying to do. I am trying to build the report by using the users ID that is currently in a session. I put a textbox on the web form to show the session ID where the crystal report viewer is and that works. The thing that gets me is why isn't the report displaying current users data from the database using the ID number? I have to many users to write in all of the ID into code and that means that I will have to make 5000 reports for each user. I am looking to see if there is a formula that will build the report using the ID that is in session? Can this work this way?
thatraja 21-Feb-14 8:46am    
/* The thing that gets me is why isn't the report displaying current users data from the database using the ID number? */
So the report loading all users data?
Computer Wiz99 21-Feb-14 8:54am    
No, it loads the first record of the database. Everything looks and work fine on the report. It shows the data I want to show but doesn't change when another user logs into the web site. This is the Selection Formula that I have and it doesn't work.
CrystalReportViewer1.SelectionFormula = "{COCINST.inst_id} = '" + Session["inst_id"].ToString() + "'";
Is there a way to get this formula to use the current ID in the session to build the report? Right now I can put the ID in the formula and that report loads and word but that will not work because every user will print that report that is not theirs.

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