Friday, April 18, 2008

Send a Parameter from a form to show in Report Viewer

Things are do before running this from






using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page

{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=vijay;Integrated Security=True");
private ReportDocument rep=new ReportDocument ();
//protected override void unload(object sender, EventArgs e)
//{
// base.OnUnload(e);
// this.Unload += new EventHandler(Page.Unload);
//}
protected void page_unload()
{
rep.Clone();
rep.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{rep.Load (Server.MapPath ("CrystalReport1.rpt"));
rep.FileName = Server.MapPath("CrystalReport1.rpt");
if (!IsPostBack)

{
binddata();
}

}
public void binddata()
{
SqlDataAdapter dr = new SqlDataAdapter("select * from sample", con);
DataSet ds = new DataSet();
dr.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.Items.Insert(0, "Select");
DropDownList1.DataTextField = "username";
DropDownList1.DataValueField = "username";
DropDownList1.DataBind();

DropDownList2.DataSource = ds;
DropDownList2.Items.Insert(0, "Select");
DropDownList2.DataTextField = "num";
DropDownList2.DataValueField = "num";
DropDownList2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = "Connect";
//string name = DropDownList1.SelectedValue.ToString();
//int num = Convert.ToInt32(DropDownList2.SelectedValue);
if (Button1.Text == "Connect")
{
Button1.Text = "View Report";
string name = TextBox1.Text;
int num = Convert.ToInt32(TextBox2.Text.ToString());
rep.SetParameterValue("name", name);
rep.SetParameterValue("num", num);
CrystalReportViewer1.ReportSource = rep;
}
}
}

No comments: