Display related records from database using CrystalReportViewer by giving values in textbox.

In this blog we will know how to display related records from database using CrystalReportViewer by giving values in textbox.

WebForm1.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ReportViewer.WebForm1" %>

 

<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:Button ID="btn_display" runat="server" Text="Display" onclick="btn_display_Click"

             />

    </div>

    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"

        AutoDataBind="true" />

    </form>

</body>

</html>

 

WebForm1.aspx.cs code


using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

using CrystalDecisions.CrystalReports.Engine;

namespace ReportViewer

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlDataAdapter sqlda = new SqlDataAdapter();

        SqlCommand com = new SqlCommand();

        DataTable dt;

        DataSet1 ds = new DataSet1();

        ReportDocument rptDoc = new ReportDocument();

        protected void btn_display_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection(connStr);

            dt = new DataTable();

            dt.TableName = "Crystal Report Example";

            com.Connection = conn;

            com.CommandText = "Select * from product where ProductName='" + TextBox1.Text + "'";

            sqlda = new SqlDataAdapter(com);

            sqlda.Fill(dt);

            ds.Tables[0].Merge(dt);

            rptDoc.Load(Server.MapPath("CrystalReport1.rpt"));

            rptDoc.SetDataSource(ds);

            CrystalReportViewer1.ReportSource = rptDoc;

        }

    }

}

 

Ebook Download
View all
Learn
View all