【问题标题】:c# passing database and parameter to crystal reportc#将数据库和参数传递给水晶报表
【发布时间】:2016-01-19 03:04:25
【问题描述】:

我收到这个错误

参数字段的类型和参数字段的当前值不兼容。

将数据库和参数都传递给 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();
}

SetParameterValue() 导致错误。

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

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

【问题讨论】:

    标签: c# crystal-reports


    【解决方案1】:

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

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

    【讨论】:

    • 我在水晶报表的数据库中没有@UserPrinted的唯一参数。
    • 哦,我把你的问题弄错了。道歉。您可以尝试在设置数据源之后执行 SetParameterValue 行吗?我已经更新了上面的代码
    • 报表中UserPrinted的数据类型是什么?
    • 我从登录传递到此报告的用户名。
    • 在C#代码中,用户被定义为一个字符串(代码:public string user;)。报告中的 UserPrinted 怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多