Below figure show you the process how to create xsd file
For adding an xsd file Click on Solution Explorer- Right Click on Project- click on Add new Item and then it will show you below screen.
Figure 4
Click on the ok button so it will ask for confirmation to put that file in App_Code folder just click ok and that file will open in screen as blank screen.
Now we will add one blank datatable to that xsd file. Just right click on the file and select Add -> Datatable. It will add one DataTable1 to the screen. Figure 5 is showing how to add datatable to xsd file.
Figure 5
Now datatable1 is added to xsd file. Now we will add data column to the datatable1 as per figure 6. Remember what ever fields (columns) we add here it will be available to show on report. So add column which you want to display in your reports one by one here.
Figure 6
Remember give exact same name for data column as in database and also select data type same as database otherwise you will get an error for field and data type Mitch match.
Once we added all the required columns in datatable then set property for the datacolumn as it has in database. Below figure will show you how to set property for data columns. Default datatype for all the column is string here so it datatype is other than string then only change it manually.
Just right click on the datacolumn in datatable and select property and from property window select appropriate datatype from DataType Dropdown for that datacolumn.
Figure 7
That’s it. Xsd file creation has been done. Now we will move to create crystal report design.
Just click on the Solution Explorer - > Right click on the project name and select crystal reports. Name it as per your choice and hit the add button
Figure 8 will show you the creation process of crystal reports.
Figure 8
Click on the add button and one .rpt file will be added to the solution. And also it will ask for the report creation type that how you want to create report. Figure 9 will show you screenshot.
Figure 9
Just click ok button to proceed. It will lead you to the figure 10
Figure 10
Under project data expand ADO.NET Datasets and select DataTable1 and add to the selected table portion located at the right side of the windows using > button.
Now click on the Finish button and it will show the next screen (Figure 11).
Figure 11
Once report file is added you can see Field Explorer on the left side near server explorer.
Expand Database Fields under that you will be able to find Debatable what we have created earlier. Just expand it and drag one by one filed from Field Explorer to the rpt file under detail section.
Now the report design part over. Now we have to fetch the data from database and bind it to dataset and then bind that dataset to the report viewer.
Let go step by step process.
First Drag a CrystalReportViewer control on aspx page from tool box as per below screen
Figure 12
Now we will fetch the data, pass data to the dataset and then add that dataset to the Crystal Report.
Below is the C# Code which will do the job
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Below is the final code for reports.
protected 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 replcae 'databaseservername' with your db server name
string sqlCon = "User ID=sa;PWD=sa; server=databaseservername;INITIAL CATALOG=SampleDB;PERSIST SECURITY 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];
}
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Below is the final code for reports.
protected 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 replcae 'databaseservername' with your db server name
string sqlCon = "User ID=sa;PWD=sa; server=databaseservername;INITIAL CATALOG=SampleDB;PERSIST SECURITY 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];
}
Now just save everything and run report it will look like below figure
Figure 13










No comments:
Post a Comment