
I will show you simple report creation process with screenshots. A picture is worth more than a thousand words, so I always believe in an article with screenshots.
Let's start by creating a new website in VS2010. See the following screen:
http://www.codeproject.com/Articles/166291/Generate-a-report-using-Crystal-Reports-in-Visual
http://www.c-sharpcorner.com/UploadFile/mahesh/CrystalReportsIntroduction11082005014959AM/CrystalReportsIntroduction.aspx
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;Below is the final code for reports:
Collapse | Copy Codeprotected void Page_Load(object sender, EventArgs e)
{
ReportDocument rptDoc = new ReportDocument();
dsSample ds = new dsSample(); // .xsd file name
DataTable dt = new DataTable();
// Just set the name of data table
dt.TableName = "Crystal Report Example";
dt = getAllOrders(); //This function is located below this function
ds.Tables[0].Merge(dt);
// Your .rpt file path will be below
rptDoc.Load(Server.MapPath("../Reports/SimpleReports.rpt"));
//set dataset to the report viewer.
rptDoc.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rptDoc;
}
public DataTable getAllOrders()
{
//Connection string replace 'databaseservername' with your db server name
string sqlCon = "User ID=sa;PWD=sa; server=databaseservername;INITIAL CATALOG=SampleDB;" +
"PERSISTSECURITY INFO=FALSE;Connect Timeout=0";
SqlConnection Con = new SqlConnection(sqlCon);
SqlCommand cmd = new SqlCommand();
DataSet ds = null;
SqlDataAdapter adapter;
try
{
Con.Open();
//Stored procedure calling. It is already in sample db.
cmd.CommandText = "getAllOrders";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Con;
ds = new DataSet();
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "Users");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cmd.Dispose();
if (Con.State != ConnectionState.Closed)
Con.Close();
}
return ds.Tables[0];
}
Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.
Shamseena, if this helps please login to Mark As Answer. | Alert Moderator