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];
}
}
}