protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindReport();
}
else
{
//To solve the error:
//Failed to export using the options you specified. Please check your options and try again.
if (Request.Form["__EVENTTARGET"] == CrystalReportViewer1.UniqueID)
{
BindReport();
}
}
}
private void BindReport()
{
string str = TextBox1.Text;
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/CrystalReport3.rpt"));
Customers dsCustomers = GetData(str);
crystalReport.SetDataSource(dsCustomers.Tables[0]);
CrystalReportViewer1.ReportSource = crystalReport;
CrystalReportViewer1.DataBind();
}
private Customers GetData(string query)
{
string str = TextBox1.Text;
string conString = ConfigurationManager.AppSettings["prink"].ToString();
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spxGetCustomers";
cmd.Parameters.AddWithValue("@Name ", str);
sda.SelectCommand = cmd;
using (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = TextBox1.Text;
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/CrystalReport3.rpt"));
Customers dsCustomers = GetData(str);
crystalReport.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = crystalReport;
CrystalReportViewer1.RefreshReport();
CrystalReportViewer1.DataBind();
}