Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
In my INVOICE app there are 2 forms first to add products to my invoice database with bill ID and second with crystal report

In my first form i have one print button which take us to report form what i want is when i click that print button it will take that bill id from form1 and display all the product of that bill id in my crystal report

plz help me

What I have tried:

C#
SqlConnection cnn;
           string connectionString = null;
           string sql = null;

           connectionString = "Data Source=SQLEXPRESS;Initial Catalog=MyBill;Integrated Security=True;Pooling=False";
           cnn = new SqlConnection(connectionString);
           cnn.Open();
           sql = "SELECT * FROM sale where billid = '"+nn+"'";
           SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
           DataSet ds = new DataSet();
           dscmd.Fill(ds);
         //  MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
           cnn.Close();

           CrystalReport2 objRpt = new CrystalReport2();

           objRpt.SetDataSource(ds);
           crystalReportViewer1.ReportSource = objRpt;
           crystalReportViewer1.Refresh();
Posted
Updated 8-Aug-17 1:36am
Comments

Without pointing out the number of examples and tutorials that are available..
I typically create a Report against the database I don't use datasets. I also typically use Stored Procedures for Reports - they are quicker and have distinct advantages when you have sub-reports. the following link will show you how to use datasets though;
Code Project - Creating Crystal Reports using C# with Datasets[^]

You need to add the following references - assuming Windows Forms;
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.ReportSource
CrystalDecisions.Shared
CrystalDecisions.Windows.Forms

I am using VS 2015 with Crystal Reports 13.2 - refer to below link for runtime download;
Crystal Reports, Developer for Visual Studio Downloads - Business Intelligence (BusinessObjects) - SCN Wiki[^]

You will also need to add the following "usings"
C#
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;



You have not specified which report - this is done as follows;
C#
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load("Put the full path and file name here");

Then loop through the Report Parameters and set them - I typically use the following;
C#
if(rptDoc.DataDefinition.ParameterFields.Count > 0)
{
    foreach(ParameterFieldDefinition crDef in rptDoc.DataDefinition.ParameterFields)
    {
    // Check for empty report name
    // Sub Reports will have a value, Main Report does not
    // Sub Report Parameters are passed by the Main Report
        if(crDef.ReportName == string.Empty)
        {
            object objValue = "What ever value I want to use";
            rptDoc.SetParameterValue(crDef.ParameterFieldName, objValue);
        }
    }
}

Next thing to do is create a Crystal reports ConnectionInfo, this is then passed to the Report
C#
ConnectionInfo crConn = new ConnectionInfo();
crConn.ServerName = "My DB Server Name";
crConn.DatabaseName = "My Database Name";
crConn.UserID = "DB User Name";
crConn.Password = "DB Password";
// get the Report Tables
Tables crTables = rptDoc.Database.Tables;
for(int i = 0; i < crTables.Count; i++)
{
    Table crTable = crTables[i];
    TableLogOnInfo tblInfo = crTable.LogOnInfo;
    tblInfo.ConnectionInfo = crConn;
    crTable.ApplyLogOnInfo(tblInfo);
}
// then display the Report - here I am using a Crystal Reports Viewer in a Windows Form
this.crystalReportsViewerFrm.ReportSource = rptDoc;


Good Luck
 
Share this answer
 
It's running example, I'm sharing with you:

Database method:

public DataSet SelectAll_()
       {
           var ds = new DataSet();
           var conn = DACUtil.GetConnection();
           try
           {
               SqlCommand cmd = new SqlCommand(SELECT_ALL, conn);
               var da = new SqlDataAdapter { SelectCommand = cmd };

               da.Fill(ds);

               ds.Tables[0].TableName = "Employee";
               ds.AcceptChanges();

           }
           finally
           {
               conn?.Dispose();
           }
           return ds;
       }


In ReportViewer.cs File:

public partial class ReportViewer : System.Web.UI.Page
    {
        ReportDocument _rpt = new ReportDocument();
        EmployeeDAC employee = new EmployeeDAC();
        protected void Page_Load(object sender, EventArgs e)
        {
            _rpt.Load(Server.MapPath("EmployeeReport.rpt"));
            DataSet ds = employee.SelectAll_();
            _rpt.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = _rpt;

        }
    }


For more detail, see the following article.
Crystal Report in Asp.net 2015 using C#[^]
 
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