首页 文章

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

提问于
浏览
0

我收到这个错误

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

将数据库和参数传递给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 .

enter image description here

1 回答

  • 0

    您必须在 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();
        }
    

相关问题