locked
Populate Crystal Report Fields with Database Values RRS feed

  • Question

  • User2012297617 posted

    I need to know  how to map fields coming from a stored procedure to  output fields on a crystal report.  Any feedback is greatly appreciated.  The output that I'm getting on the report is the name of the fields instead of the values coming from the database.

    The code is based on this sample:

    http://www.codeproject.com/Articles/166291/Generate-a-report-using-Crystal-Reports-in-Visual

    Here's my code:

    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Configuration;
    
    namespace project
    {
        public partial class check : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ReportDocument rptCheck = new ReportDocument();
                dsCheck ds = new dsCheck(); // .xsd file name
                DataTable dt = new DataTable();
    
    
                // Just set the name of data table
                dt.TableName = "Crystal Report Checks";
                dt = getChecks(); //This function is located below this function
                ds.Tables[0].Merge(dt);
                
                // Your .rpt file path will be below
                rptCheck.Load(Server.MapPath("checkprt.rpt"));
    
                //set dataset to the report viewer.
                CrystalReportViewer1.ReportSource = rptCheck;
    
    
            }
    
    
            public DataTable getChecks()
            {
                string sqlCon = WebConfigurationManager.ConnectionStrings["hello"].ConnectionString;
                SqlConnection Con = new SqlConnection(sqlCon);
                SqlCommand cmd = new SqlCommand();
                DataSet ds = null;
                SqlDataAdapter adapter;
                try
                {
                    Con.Open();
                    //Stored procedure calling. It is already in sample db.
                    cmd.CommandText = "spGetData";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Date", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@Amount", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@AccountID", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output; 
    
                    cmd.Connection = Con;
                    ds = new DataSet();
                    adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds);
    
                    //get output param list 
                    string Count1 = cmd.Parameters["@Date"].Value.ToString();
                    string Count2 = cmd.Parameters["@Name"].Value.ToString(); 
                    
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    cmd.Dispose();
                    if (Con.State != ConnectionState.Closed)
                        Con.Close();
                }
                return ds.Tables[0];
            }
    
    
        }
    }

    Friday, August 31, 2012 5:07 PM

Answers

  • User-578610739 posted

    Hi Friend,

    see link,

    http://p2p.wrox.com/book-professional-crystal-reports-vs-net/17318-how-pass-parameter-crystal-report-using-c.html

    http://www.c-sharpcorner.com/uploadfile/shashikantray/passing-parameters-to-crystal-reports-at-runtime-with-C-Sharp/

    http://www.codeproject.com/Articles/22135/How-to-Pass-Parameters-to-Crystal-Reports-at-Runti

    http://www.codeproject.com/Articles/9263/How-to-pass-discrete-parameters-to-Crystal-Reports

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 3, 2012 1:08 AM