获取 crystal 报告中文本框的值

Get the value of textbox in crystal report

我有 crystal 报告,其中我需要获取 crystal 报告中文本框的值。如果我将 textbox1= 1 然后在 crystal 报告中显示为 1。我该怎么做? 这是我在 default.aspx 页面中的 crystal 报告代码。

protected void Page_Load(object sender, EventArgs e)
{  
    TextBox1.Text = "9";
    TextBox2.Text = "02/02/2015";
    TextBox3.Text = "02/03/2015";
    DataTable dt = new DataTable();
    String str = "select * from tbl1 where br=@search and Date between @search1 and @search2  ";
    SqlCommand xp = new SqlCommand(str, con);
    xp.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox1.Text;
    xp.Parameters.Add("@search1", SqlDbType.VarChar).Value = TextBox2.Text;
    xp.Parameters.Add("@search2", SqlDbType.VarChar).Value = TextBox3.Text;
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = xp;
    da.Fill(dt);
    con.Close();
    if (dt.Rows.Count > 0)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
        crystalReport.SetDatabaseLogon("xx", "xxxx");
        crystalReport.SetDataSource(dt);
        CrystalReportViewer1.ReportSource = crystalReport;
        CrystalReportViewer1.DataBind();
    }

这里我需要在crystal报表中显示textbox1,2和3的值

首先需要在CrystalReport工具箱的Parameter Fields下创建3个参数,如下图所示

然后你需要从你的后台代码传递值

var rpt=new ReportDocument();
rpt.SetParameterValue("Search1","I am search1");
rpt.SetParameterValue("Search2","I am search2");
rpt.SetParameterValue("Search3","I am search3");
CrystalReportViewer1.ReportSource = rpt;
protected void Page_Load(object sender, EventArgs e)
{  
    TextBox1.Text = "9";
    DataTable dt = new DataTable();
    String str = "select * from tbl1 where br=@search and Date between @search1 and @search2  ";
    SqlCommand xp = new SqlCommand(str, con);
    xp.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox1.Text;
    xp.Parameters.Add("@search1", SqlDbType.VarChar).Value = TextBox2.Text;
    xp.Parameters.Add("@search2", SqlDbType.VarChar).Value = TextBox3.Text;
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = xp;
    da.Fill(dt);
    con.Close();
    if (dt.Rows.Count > 0)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.SetParameterValue("search", "I am search");
        crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
        crystalReport.SetDatabaseLogon("xx", "xxxx");
        crystalReport.SetDataSource(dt);
        CrystalReportViewer1.ReportSource = crystalReport;
        CrystalReportViewer1.DataBind();
    }

}

我添加了参数名称作为搜索。

但它显示错误报告路径无效。如果我删除此行 crystalReport.SetParameterValue("search", "I am search");然后它的工作。