Search code examples
asp.netcrystal-reports

Get the value of textbox in crystal report


I have crystal report, in which I need to get the value of textbox in crystal report. If I put textbox1= 1 then in crystal report it display as 1. How can I do this? This is my crystal report code in default.aspx page.

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

Here I need to display textbox1,2 and 3 value in crystal report


Solution

  • At first you have to create 3 parameters in CrystalReport toolbox under Parameter Fields as shown in the following figure.

    enter image description here

    Then you need to pass the values from your behind code

    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;