Quantcast
Channel: Crystal Reports
Viewing all articles
Browse latest Browse all 1507

Crystal Reports using Dataset in C#

$
0
0

 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;
using CrystalDecisions.Shared;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection sqlConn = new SqlConnection("Server=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password;");


        SqlCommand comd = new SqlCommand();
                
        comd.CommandText = "SELECT * From TableName";
        comd.CommandType = CommandType.Text;
        comd.Connection = sqlConn;
        comd.Connection.Open();

        SqlDataAdapter sqlAdapter = new SqlDataAdapter();
        sqlAdapter.SelectCommand = comd;

        DataSet ds = new DataSet();
        sqlAdapter.Fill(ds, "TableName");
       
        ReportDocument myReportDocument;
        myReportDocument = new ReportDocument();
        
        myReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
        myReportDocument.SetDataSource(ds);
        comd.Connection.Close();
        
        CrystalReportViewer1.ReportSource = myReportDocument;
      
        CrystalReportViewer1.DataBind();

      

    
     
    }
}

 

I thought i would post this for the reason i spent a couple of days myself looking for something i felt was solid enough to work for me.  This is done by creating a DataSet and a crystal report supplied by the dataset.

 

You can easily use this to work with parameters carried over from web page to web page as well as parameters and events such as button click events within the existing page. 

 Modify this line of code : comd.CommandText = "SELECT * From TableName";   and change it to something like this:

 

 MyEmployeeName = Textbox1.text      // then add a button click event to do your work

  comd.CommandText = "SELECT * From TableName Where EmployeeName =" + MyEmployeeName;

or many other parameters that would work for you.

 

Some might think this is trivial but it does and has worked perfectly for me.  Just felt worth while to post.

 

JohnnyBS


Viewing all articles
Browse latest Browse all 1507

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>