简体   繁体   English

c#将数据库和参数传递给Crystal报表

[英]c# passing database and parameter to crystal report

I get this error 我得到这个错误

The types of the parameter field and parameter field current values are not compatible. 参数字段和参数字段当前值的类型不兼容。

when passing both database and parameter to Crystal Reports. 将数据库和参数都传递给Crystal Reports时。

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();
}

The SetParameterValue() is causing the error. SetParameterValue()导致错误。

Can anyone help me to solve this problem? 谁能帮我解决这个问题? Should I add parameter in stored procedure or in dataset? 我应该在存储过程中还是在数据集中添加参数?

I just want to pass the user's name to the Crystal Report that has database. 我只想将用户名传递给具有数据库的Crystal Report。

在此处输入图片说明

You have to set the parameter and value in SqlCommand object in your SqlAdapter . 您必须在SqlAdapter SqlCommand对象中设置参数和值。 Please see below for the updated code: 请参阅下面的更新代码:

    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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM