Use Crystal report with XML Schema
අපේ බොහෝ අයට Crystal report භාවිතය පිළිබද ගැටළු තියෙනව. මේ මගේ යාළුවෙක් ඇසූ ප්ර්ශ්නයකට පිළිතුරක්.
මුලින්ම ඔබට Crystal report වලට අවෂය දත්ත තිබෙන SQL Statement එක ලියා ගන්න. එසේත් නැතිනම් report එකේ පෙන්වන දත්ත වල Query එක ලියා ගන්න.
මම මේ උදාහරණය සඳහා AdventureWorks Database එකේ [HumanResources].[Employee] යන Table එක භාවිතා කරනව. මේ තමයි query එක
SELECT Name, ProductNumber, ReorderPoint, StandardCost, ListPrice, SellStartDate, SellEndDate FROM Production.Product
ඉන්පසුව එය ඔබගේ program එක තුල execute කර DataSet එකකට assign කරන්න. පහත method එක මගින් ඒ assign කරගත් DataSet එක return කරගන්නව.
private DataSet getDataForReport()
{
try
{
string SQL = "SELECT Name, ProductNumber, ReorderPoint, StandardCost, ListPrice, SellStartDate, SellEndDate FROM Production.Product";
string ConnStr = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString;
SqlDataAdapter myDataAdapter = new SqlDataAdapter(SQL, ConnStr);
DataSet ds = new DataSet();
// No need to open or close the connection
// since the SqlDataAdapter will do this automatically.
myDataAdapter.Fill(ds);
ds.Tables[0].TableName = "Product";
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
ඉන් පසුව XML Schema එක ඔබට ලියාගන්න පුළුවන් පහත සදහන් code block එක භාවිතා කර. මම Form_Load event එක භාවිතා කලා. ඔබට අවෂය XML Schema එක ලියාගත්තායින් පසුව ඔබට මෙම code block එක ඉවත් කල හැකියි. ඔබට මෙය අවෂය වන්නේ ඔබගේ report එක තුල පෙන්වන දත්ත වෙනස් වුවහොත් පමණි.
private void Form1_Load(object sender, EventArgs e)
{
//Only need for Crystal Report Design comment this part if you generated the XML Schema
DataSet ds = getDataForReport();
ds.WriteXmlSchema("EmployeeList.xsd");
}
ඔබ අවෂය XML Schema එක ලියා ගත්තේ නම් පහල පින්තූරයේ පෙන්වා ඇති ලෙස අළුත් අයිතමයක් එක්කරන්න.

එහි Reporting යන්න තෝරා Crystal Report යන්නද තෝරන්න. නමක් ලබාදී Add යන බොත්තම ක්ලික් කරන්න.

පසුව එන මෙනුවෙන් Standard Report යන්න තෝරා Next යන බොත්තම එබීමෙන්, පහල ඇති Dialog Box එක දිස්වේ.

රූපයේ පෙන්වා ඇති ආකාරයට Create New Connection යන්න තෝරා එහි ADO.Net යන්නට පෙර ඇති + ලකුණ ක්ලික් කරන්න. එවිට අදාල XML හෝ XML Schema ගොනුව තෝරන්න ලැබේ. අදාල ගොනුව තේරූ විට එහි ඇති tables පෙන්වයි. අදාල table එක දකුණු පැත්තට එකතු කිරීමෙන් එය රිපෝට් එකේ Database Fields තුල පෙන්වයි.

ඉන්පසු Report එක සාමාන්ය ලෙස සාදන්න.
ඉන්පසුව එය පෙන්වීමේදි, web හෝ Windows Project එකකට CrystalReportViewer object එකක් සාදා එයට අදාල Data සම්බන්ඨ කල crystal report එක ලබාදෙන්න. මා පහත සඳහන් ලෙස එය Form_Load event එක භාවිතා කර කලෙමි. පෙර තිබූ code කොටස comment කලේ එය නැවත අවෂය වුවහොත් ලබාගැනීමටයි.
private void Form1_Load(object sender, EventArgs e)
{
//Only need for Crystal Report Design comment this part if you generated the XML Schema
DataSet ds = getDataForReport();
//ds.WriteXmlSchema("Product.xsd");
cryProductList cryReport = new cryProductList();
cryReport.SetDataSource(ds);
crvDemo.ReportSource = cryReport;
}
අවසානයේ දී Project එක run කලේ නම් ඔබට පහත ඇති පින්තූරය සේ පෙනෙන report එකක් දැකගත හැක.

Source Code එක භාගන්න නම් “CrystalReportDemo.zip”යන්න.






3 Comments on “Use Crystal report with XML Schema”
thnks machan
Cool. Wonder if you could do something similar with Reporting Services?
That would be a fun RnD project…
@Gogula – yeah, I’ll do when I got free time, these days bit busy with office works and Uni Project.