Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a web form that has textboxes on it. A user can fill in the textboxes and submit the data to the database. The user can also click the print button and print out a report. I have Crystal Reports in the project also. The report is on a web form in the Crystal Report Viewer. When a user logs into the system and submits some data then clicks print the report prints out but it prints out all of the reports from that database and the reports are blank (no data). I just want to print the report that has the user's data on it that they just submitted. How can I print a Crystal Report within a session? Is there a way to do this?

Here is the web form code for the form that the user can submit the data and print:

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();
        doc.Load(Server.MapPath("FormAReport.rpt"));
        doc.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
        doc.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;

        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 Table44 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 Table33 where INST_ID = '" + TextBoxINST_ID.Text + "'", con2);
        SqlCommand scmd3 = new SqlCommand("Select INST_ID, accessLevel from Table54 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 Table99 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 Table33 (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 Table99 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;
                TextBoxTNA2.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)
    {
        doc.PrintOptions.PrinterName = GetDefaultPrinter();
        doc.PrintToPrinter(1, false, 0, 0);
    }
    string GetDefaultPrinter()
    {
        PrinterSettings settings = new PrinterSettings();
        foreach (string printer in PrinterSettings.InstalledPrinters)
        {
            settings.PrinterName = printer;
            if (settings.IsDefaultPrinter)
                return printer;
        }
        return string.Empty;
    }

    private string GetDefaultprinterName()
    {
        throw new NotImplementedException();
    }
}
Posted
Comments
thatraja 23-Jan-14 2:51am    
I need screenshot of the Report(Design view), Field explorer(where Table names showing), etc.,

Use imgur.com for screenshot uploads.
Computer Wiz99 23-Jan-14 9:00am    
Do you need the Design View of the report in Crystal Report 2013 or in VS 2010?
thatraja 23-Jan-14 9:39am    
yes, I need to see the Tables, Link-relationships, etc., to spot the issue.
Computer Wiz99 23-Jan-14 9:51am    
Ok. Give me a min. I never put a screen shot on this form before.
thatraja 23-Jan-14 9:55am    
Sample screens here
Tables list
Table links

1 solution

I think you forgot to apply logon for report document. Check this
Using the SetDatabaseLogon() Method of the ReportDocument Class[^]
 
Share this answer
 
Comments
Computer Wiz99 23-Jan-14 13:48pm    
thatraja, that might be the issue but when I print this report it prints out all of the users names and no data where it suppose to be. I know there is no data in the database that I am calling it from but when a user logs into the system and submits their data into the database and clicks print, it should only print the report for that current user that is logged into the system. No print all of the other users information. I do have a session in place for the users when they log in. That is what I control the forms with.
thatraja 23-Jan-14 13:54pm    
Are you getting data in report? But you said you're getting blank report
Computer Wiz99 23-Jan-14 14:15pm    
I meant that the report that I was printing was blank. Meaning that all the data fields had 0s in them. I knew that they would because the database is blank. I wanted to print the report just for the user that is logged in not print a report for all the users. Since the users are in a session it should just print that user that is currently logged in report. How can i fix this.
thatraja 24-Jan-14 10:18am    
The current way won't work that way. You should go with PUSH method.
Computer Wiz99 24-Jan-14 10:30am    
So you are saying that a user can't print a report of their own data? How does the PUSH method work with this? Remember, each user is in a session. The session goes by their user ID. Is there a code to just print a report using the user's session?

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