Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a print button that I am trying to get to print the crystal report that I made. The report should print out by Session. How can I print a crystal report by way of the print button on a aspx page?

This is the aspx page that has the print button:
C#
using System;
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.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Drawing.Printing;
using System.IO;
using System.Web.SessionState;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

protected void Page_Load(object sender, EventArgs e)
    {
       TextBoxINST_ID.Text = Session["inst_id"].ToString();
    }
 protected void ButtonPrint_Click(object sender, EventArgs e)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("ReportFormA.aspx"));
    }


This is the page that has the Crystal Report Viewer.

C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
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;


namespace SACSCOCLogin1._1
{
    public partial class ReportFormA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBoxINST_ID.Text = Session["inst_id"].ToString();
            CrystalReportViewer1.Visible = false;
        }

        
        
    }
}


I also don't want to show the viewer when printing.

Here is the code for the web form the user fills out with the session id.

C#
using System;
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.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Drawing.Printing;
using System.IO;
using System.Web.SessionState;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;




public partial class FinancialProfileFormA : System.Web.UI.Page
{
    ReportDocument doc = new ReportDocument();
    protected void Page_Load(object sender, EventArgs e)
    {

        //this.lblYEAR.Text = DateTime.Today.ToString("yyyy");
        //this.lblYEAR2.Text = DateTime.Today.ToString("yyyy");
        this.lblTime.Text = System.DateTime.Now.ToShortTimeString();
        this.TextBoxDATE.Text = DateTime.Today.ToString("dd-MMM-yy");
        TextBoxTROA.Focus();
        
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();
        SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con2.Open();
        SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con3.Open();

        TextBoxINST_ID.Text = Session["inst_id"].ToString();
        SqlCommand scmd = new SqlCommand("Select INST_ID, LongName, City, State from Table1 where INST_ID = '" + TextBoxINST_ID.Text + "'", con);
        SqlCommand scmd2 = new SqlCommand("Select INST_ID, TOTAL_REVE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT from Table2 where INST_ID = '" + TextBoxINST_ID.Text + "'", con2);
        SqlCommand scmd3 = new SqlCommand("Select INST_ID, accessLevel from Table88 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
        SqlDataReader dr = scmd.ExecuteReader();
        SqlDataReader dr2 = scmd2.ExecuteReader();
        SqlDataReader dr3 = scmd3.ExecuteReader();

        if (dr.Read())
            if (dr2.Read())
                if (dr3.Read())
                {
                    TextBoxaccessLevel.Text = dr3["accessLevel"].ToString();
                    lblCity.Text = dr["City"].ToString();
                    lblLYEAR.Text = dr2["FINYR"].ToString();
                    lblLYEAR2.Text = dr2["FINYR"].ToString();
                    lblState.Text = dr["State"].ToString();
                    lblSchool.Text = dr["LongName"].ToString();
                    lblSchool2.Text = dr["LongName"].ToString();
                    TextBoxLYInstr.Text = dr2["INSTRUCTIO"].ToString();
                    TextBoxLYRes.Text = dr2["RESEARCH"].ToString();
                    TextBoxLYPubS.Text = dr2["PUBLIC_SER"].ToString();
                    TextBoxLYAcad.Text = dr2["ACADEMIC_S"].ToString();
                    TextBoxLYStudS.Text = dr2["STUDENT_SE"].ToString();
                    TextBoxLYInstiS.Text = dr2["INSTITUTIO"].ToString();
                    TextBoxLYOperM.Text = dr2["PHYSICAL_P"].ToString();
                    TextBoxLYSFEDA.Text = dr2["SCHOLARSHI"].ToString();
                    TextBoxLYAuxE.Text = dr2["AUXILIARY_"].ToString();
                    TextBoxLYHosS.Text = dr2["HOSPITALS"].ToString();
                    TextBoxLYIndeO.Text = dr2["INDEPENDEN"].ToString();
                    TextBoxLYOED.Text = dr2["OTHEREXP"].ToString();
                    TextBoxLYTA.Text = dr2["TOTASSETS"].ToString();
                    TextBoxLYTL.Text = dr2["TOTLIABILITY"].ToString();
                    TextBoxLYNPRNA.Text = dr2["NoNEXPPERMRESASSETS"].ToString();
                    TextBoxLYTUNA.Text = dr2["UNRNETASSETS"].ToString();
                    TextBoxLYTR.Text = dr2["TOTALREV"].ToString();
                    TextBoxLYTFN.Text = dr2["TUITFEES"].ToString();
                    TextBoxLYCD.Text = dr2["CURRDEBT"].ToString();
                    TextBoxLYLTD.Text = dr2["LONGTERMDEBT"].ToString();
                    TextBoxLYTROA.Text = dr2["TOTAL_REVE"].ToString();


                }
        dr.Close();
        con.Close();
        dr2.Close();
        con2.Close();
        dr3.Close();
        con3.Close();

        if (TextBoxINST_ID.Text.Trim().Length > 0)
        {
            SqlConnection con4 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con4.Open();

            SqlCommand scmd4 = new SqlCommand("Select TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS from Table23 where INST_ID = '" + TextBoxINST_ID.Text + "'", con4);
            SqlDataReader dr4 = scmd4.ExecuteReader();
            if (dr4.Read())
            {
                TextBoxTROA.Text = dr4["TOTAL_REVE"].ToString();
                TextBoxInstr.Text = dr4["INSTRUCTIO"].ToString();
                TextBoxRes.Text = dr4["RESEARCH"].ToString();
                TextBoxPubS.Text = dr4["PUBLIC_SER"].ToString();
                TextBoxAcad.Text = dr4["ACADEMIC_S"].ToString();
                TextBoxStudS.Text = dr4["STUDENT_SE"].ToString();
                TextBoxInstiS.Text = dr4["INSTITUTIO"].ToString();
                TextBoxOperM.Text = dr4["PHYSICAL_P"].ToString();
                TextBoxSFEDA.Text = dr4["SCHOLARSHI"].ToString();
                TextBoxAuxE.Text = dr4["AUXILIARY_"].ToString();
                TextBoxHosS.Text = dr4["HOSPITALS"].ToString();
                TextBoxIndeO.Text = dr4["INDEPENDEN"].ToString();
                TextBoxOED.Text = dr4["OTHEREXP"].ToString();
                TextBoxTA.Text = dr4["TOTASSETS"].ToString();
                TextBoxTL.Text = dr4["TOTLIABILITY"].ToString();
                TextBoxNPRNA.Text = dr4["NoNEXPPERMRESASSETS"].ToString();
                TextBoxETRNA.Text = dr4["EXPENDABLE"].ToString();
                TextBoxTUNA.Text = dr4["UNRNETASSETS"].ToString();
                TextBoxTR.Text = dr4["TOTALREV"].ToString();
                TextBoxTFN.Text = dr4["TUITFEES"].ToString();
                TextBoxCD.Text = dr4["CURRDEBT"].ToString();
                TextBoxLTD.Text = dr4["LONGTERMDEBT"].ToString();
                TextBoxTNA.Text = dr4["TOTALNETASSETS"].ToString();
            }
            con4.Close();
            dr4.Close();
        }
    }

    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into Table22 (INST_ID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@INST_ID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS)Insert into Table23 (INST_ID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@INST_ID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS)", con);
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTROA.Text);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxRes.Text);
        cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxPubS.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcad.Text);
        cmd.Parameters.AddWithValue("@STUDENT_SE", TextBoxStudS.Text);
        cmd.Parameters.AddWithValue("@INSTITUTIO", TextBoxInstiS.Text);
        cmd.Parameters.AddWithValue("@PHYSICAL_P", TextBoxOperM.Text);
        cmd.Parameters.AddWithValue("@SCHOLARSHI", TextBoxSFEDA.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOED.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
        cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
        cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
        cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
        cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
        cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
        cmd.Parameters.AddWithValue("@LONGTERMDEBT", TextBoxLTD.Text);
        cmd.Parameters.AddWithValue("@FINYR", lblYEAR.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        cmd.Parameters.AddWithValue("@TOTALNETASSETS", TextBoxTNA.Text);


        cmd.ExecuteNonQuery();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Financial Profile');", true);

        if (Page.IsValid)
        {
            SqlCommand level = new SqlCommand("select accessLevel, INST_ID from Table88 where INST_ID = @INST_ID AND accessLevel = @accessLevel", con);
            level.Parameters.Add(new SqlParameter("INST_ID", TextBoxINST_ID.Text));
            level.Parameters.Add(new SqlParameter("accessLevel", TextBoxaccessLevel.Text));

            SqlDataReader reader = level.ExecuteReader();
            DataTable dt1 = new DataTable();
            dt1.Load(reader);

            foreach (DataRow dr1 in dt1.Rows)
            {
                int returnedLevel = Convert.ToInt32(dr1[0].ToString());
                int accessLevel = Convert.ToInt32(dr1[1].ToString());
                Session["accessLevel"] = accessLevel;

                if (returnedLevel == 1)
                {
                    Response.Redirect("FormAPublic.aspx");
                }
                else if (returnedLevel == 2)
                {
                    Response.Redirect("FormCPrivateNon.aspx");
                }
                else if (returnedLevel == 3)
                {
                    Response.Redirect("FormDPrivateFor.aspx");
                }
                else if (returnedLevel == 11)
                {
                    Response.Redirect("FormAPublicL.aspx");
                }
                else if (returnedLevel == 21)
                {
                    Response.Redirect("FormCPrivateNonL.aspx");
                }
                else if (returnedLevel == 31)
                {
                    Response.Redirect("FormDPrivateForL.aspx");
                }
                else if (returnedLevel == 7)
                {
                    Response.Redirect("CEOPage.aspx");
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Financial Profile');", true);
                }
                con.Close();
                TextBoxTROA.Text = string.Empty;
                TextBoxInstr.Text = string.Empty;
                TextBoxRes.Text = string.Empty;
                TextBoxPubS.Text = string.Empty;
                TextBoxAcad.Text = string.Empty;
                TextBoxStudS.Text = string.Empty;
                TextBoxInstiS.Text = string.Empty;
                TextBoxOperM.Text = string.Empty;
                TextBoxSFEDA.Text = string.Empty;
                TextBoxAuxE.Text = string.Empty;
                TextBoxHosS.Text = string.Empty;
                TextBoxIndeO.Text = string.Empty;
                TextBoxOED.Text = string.Empty;
                TextBoxTA.Text = string.Empty;
                TextBoxTL.Text = string.Empty;
                TextBoxNPRNA.Text = string.Empty;
                TextBoxETRNA.Text = string.Empty;
                TextBoxTUNA.Text = string.Empty;
                TextBoxTR.Text = string.Empty;
                TextBoxTFN.Text = string.Empty;
                TextBoxCD.Text = string.Empty;
                TextBoxLTD.Text = string.Empty;
                TextBoxFullN.Text = string.Empty;
                TextBoxTitle.Text = string.Empty;
                TextBoxMA.Text = string.Empty;
                TextBoxTN.Text = string.Empty;
                TextBoxFN.Text = string.Empty;
                TextBoxEA.Text = string.Empty;
                TextBoxTNA.Text = string.Empty;
                
            }
        }
    }

    protected void TextBoxTL_TextChanged(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBoxTA.Text);
        int b = Convert.ToInt32(TextBoxTL.Text);
        TextBoxTNA.Text = Convert.ToString(a - b);
        TextBoxNPRNA.Focus();
    }

    protected void TextBoxTUNA_TextChanged(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBoxNPRNA.Text);
        int b = Convert.ToInt32(TextBoxETRNA.Text);
        int c = Convert.ToInt32(TextBoxTUNA.Text);
        TextBoxTNA2.Text = Convert.ToString(a + b + c);
        TextBoxTR.Focus();
    }

    protected void ButtonPrint_Click(object sender, EventArgs e)
    {
        Session["inst_id"] = TextBoxINST_ID;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('ReportFormA.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
    }
}   
Posted
Updated 10-Feb-14 5:01am
v2

 
Share this answer
 
Comments
Computer Wiz99 10-Feb-14 10:36am    
Ahmed Bensaid, I can print the report but how do I get it to show only the one for the current user that is logged in. I have a session setup but it doesn't work. What did I do wrong?
Ahmed Bensaid 10-Feb-14 10:42am    
I didn't understand. You have several reports ?
Computer Wiz99 10-Feb-14 10:46am    
I have several users in my database. The all have different logins and user IDs. When a user logs in and fills out the web form and submits it to the database they can click print and it will print out the report of that current user by using the user ID. I have the UserID in a session on the web form. How do I get it on the Crystal reports? Right now it prints out the first user data in the report.
Ahmed Bensaid 10-Feb-14 10:55am    
Can I see the code where you retrieve data from database accordingly to current client id ?
Computer Wiz99 10-Feb-14 11:01am    
I added the new code.
 
Share this answer
 

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