c# 将数据库和参数传递给 crystal 报告

c# passing database and parameter to crystal report

我收到这个错误

The types of the parameter field and parameter field current values are not compatible.

将数据库和参数传递给 Crystal 报告时。

ReportDocument rd = new ReportDocument();

public string user;

public frmReport(string User)
{
        InitializeComponent();
        loadReport();
        this.user = User;
}

public void loadReport()
{
        rd.Load(@"C:\Users\Jeff Enad\Desktop\TEST1\Cebu Hallmark Hotel Management System\Cebu Hallmark Hotel Management System\cryReport.rpt");

        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=dbCebuHallmark;Integrated Security=True");

        SqlDataAdapter da = new SqlDataAdapter("GetAllReport", con);
        rd.SetParameterValue("UserPrinted", user); //This is the parameter
        da.SelectCommand.CommandType = CommandType.StoredProcedure;

        DataSet ds = new System.Data.DataSet();
        da.Fill(ds, "REPORT");

        rd.SetDataSource(ds);

        crystalReportViewer1.ReportSource = rd;
        crystalReportViewer1.Refresh();
}

SetParameterValue() 导致错误。

谁能帮我解决这个问题?我应该在存储过程中还是在数据集中添加参数?

我只想将用户名传递给具有数据库的 Crystal 报表。

您必须在 SqlAdapterSqlCommand 对象中设置参数和值。请参阅下面的更新代码:

    public void loadReport()
    {
        rd.Load(@"C:\Users\Jeff Enad\Desktop\TEST1\Cebu Hallmark Hotel Management System\Cebu Hallmark Hotel Management System\cryReport.rpt");
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=dbCebuHallmark;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter("GetAllReport", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet ds = new System.Data.DataSet();
        da.Fill(ds, "REPORT");
        rd.SetDataSource(ds);
        rd.SetParameterValue("UserPrinted", user); //This is the parameter
        crystalReportViewer1.ReportSource = rd;
        crystalReportViewer1.Refresh();
    }