Click here to Skip to main content
15,914,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a page with CrystalReportViewer on it and a report in the viewer. I am trying to print out the report using a session but no luck so far. I have a code that might your but so far I have errors in the code. This is my first time working with Crystal Reports on this level. I never did the sessions on it before. Here is the code.

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


namespace SACSCOCLogin1._1
{
    public partial class ReportFormA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();

            SqlCommand comm = new SqlCommand("SELECT * FROM Table55 where INST_ID=" + Session["INST_ID"].ToString(), con);
            comm.CommandType = CommandType.Text; SqlDataAdapter adapter = new SqlDataAdapter();

            adapter.SelectCommand = comm;

            CrystalReportViewer1 dataset = new CrystalReportViewer1();
            adapter.Fill(dataset, "DataTable1");

            CrystalReportViewer1.SetDataSource(dataset);

            CrystalReportViewer1.ReportSource = crystal;

        }   
        
    }
}


This is where my error are in the code

C#
CrystalReportViewer1 dataset = new CrystalReportViewer1();
            adapter.Fill(dataset, "DataTable1");

            CrystalReportViewer1.SetDataSource(dataset);

            CrystalReportViewer1.ReportSource = crystal;


What am I doing wrong? Can this be fixed or should I go another route?

New Updated code not yet working but getting there.

C#
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();

            SqlCommand comm = new SqlCommand("SELECT * FROM Table55 where INST_ID=" + Session["INST_ID"].ToString(), con);
            comm.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter();

            adapter.SelectCommand = comm;

            CrystalReportViewer dataset = new CrystalReportViewer();
            DataTable dt = new DataTable();
            adapter.Fill(dt);

            dataset.SetDataSource(dt);

            CrystalReportViewer1.ReportSource = new CrystalReportViewer();
Posted
Updated 10-Feb-14 10:06am
v2
Comments
Richard C Bishop 10-Feb-14 14:56pm    
What are the errors?
Computer Wiz99 10-Feb-14 15:01pm    
CrystalReportViewer1 on both sides has errors: CrystalReportViewer1'is a 'field' but is used like a 'type'.

SetDataSource error:'CrystalDecisions.Web.CrystalReportViewer' does not contain a definition for 'SetDataSource' and no extension method 'SetDataSource' accepting a first argument of type 'CrystalDecisions.Web.CrystalReportViewer' could be found (are you missing a using directive or an assembly reference?)

crystal error: The name 'crystal' does not exist in the current context
Richard C Bishop 10-Feb-14 15:16pm    
You are instantiating CrystalReportViewer1 as "dataset" and then trying to use properties of the CrystalReportViewer class and set them on the class instead of the instantiated object.
Computer Wiz99 10-Feb-14 15:18pm    
Ok. What can I do to change it or am I going in the right direction when trying to call the report by using a session?
Richard C Bishop 10-Feb-14 15:19pm    
You are close but you want something like this:
CrystalReportViewer1 report = new CrystalReportViewer1(); report.SetDataSource(dataset);

See also my comments above:

Here is an example of what you can do.

using System.Web.UI.WebControls;
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;
 
namespace SACSCOCLogin1._1
{
    public partial class ReportFormA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
 
            SqlCommand comm = new SqlCommand("SELECT * FROM Table55 where INST_ID=" + Session["INST_ID"].ToString(), con);
            comm.CommandType = CommandType.Text; 
            SqlDataAdapter adapter = new SqlDataAdapter();
 
            adapter.SelectCommand = comm;
 
            CrystalReportViewer dataset = new CrystalReportViewer();
            DataTable dt = new DataTable();
            adapter.Fill(dt, "DataTable1");
 
            dataset.SetDataSource(dt);
 
            dataset.ReportSource = crystal;
 
        }   
        
    }
}
 
Share this answer
 
v2
Comments
Computer Wiz99 10-Feb-14 15:38pm    
Ok. I still have errors on adapter.Fill(dt, "DataTable1"); and SetDataSource.
Richard C Bishop 10-Feb-14 15:42pm    
Ok, what are these error saying?
Computer Wiz99 10-Feb-14 15:45pm    
The best overloaded method match for 'System.Data.Common.DataAdapter.Fill(System.Data.DataTable, System.Data.IDataReader)' has some invalid argument
Argument 2: cannot convert from 'string' to 'System.Data.IDataReader'

'CrystalDecisions.Web.CrystalReportViewer' does not contain a definition for 'SetDataSource' and no extension method 'SetDataSource' accepting a first argument of type 'CrystalDecisions.Web.CrystalReportViewer' could be found (are you missing a using directive or an assembly reference?)




Richard C Bishop 10-Feb-14 15:48pm    
Remove the string "DataTable1" from the method call when filling your datatable with the sqladapter.
Computer Wiz99 10-Feb-14 15:50pm    
Ok. And put what there?
C#
CrystalReportViewer report = new CrystalReportViewer();
            adapter.Fill(dataset, "DataTable1");

            report.SetDataSource(dataset);

            CrystalReportViewer1.ReportSource = crystal;

The new code so far. Where the underline is is where the errors are still.
 
Share this answer
 
Comments
Richard C Bishop 10-Feb-14 15:27pm    
Copy and paste my code verbatim. You're a bit off on your code. "Crystal" does not exist anywhere, so that will either need to be created or removed.
Richard C Bishop 10-Feb-14 16:10pm    
See this article, maybe it will help you:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/95c2a550-ed28-427e-ab85-f79b19f832f1/datatable-to-crystal-report?forum=csharpgeneral
Computer Wiz99 10-Feb-14 16:12pm    
Ok. I will look into it.

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