Use Crystal report with XML Schema

This item was filled under [ .NET, Programming, Timely, Visual Studio ]

අපේ බොහෝ අයට 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 එක ලියා ගත්තේ නම් පහල පින්තූරයේ පෙන්වා ඇති ලෙස අළුත් අයිතමයක් එක්කරන්න.
Crystal Reports Pic 1 - Add New Item
එහි Reporting යන්න තෝරා Crystal Report යන්නද තෝරන්න. නමක් ලබාදී Add යන බොත්තම ක්ලික් කරන්න.
Crystal Reports Pic 2 - Select Crystal Report
පසුව එන මෙනුවෙන් Standard Report යන්න තෝරා Next යන බොත්තම එබීමෙන්, පහල ඇති Dialog Box එක දිස්වේ.
Crystal Reports Pic 3 - Select Data Source
රූපයේ පෙන්වා ඇති ආකාරයට Create New Connection යන්න තෝරා එහි ADO.Net යන්නට පෙර ඇති + ලකුණ ක්ලික් කරන්න. එවිට අදාල XML හෝ XML Schema ගොනුව තෝරන්න ලැබේ. අදාල ගොනුව තේරූ විට එහි ඇති tables පෙන්වයි. අදාල table එක දකුණු පැත්තට එකතු කිරීමෙන් එය රිපෝට් එකේ Database Fields තුල පෙන්වයි.
Crystal Reports  Pic 4 - Select Data Tables
ඉන්පසු 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 එකක් දැකගත හැක.
Crystal Reports Pic 5 - Final Report
Source Code එක භාගන්න නම් “CrystalReportDemo.zip”යන්න.


Popularity: 295 views
Tagged with: [ , ]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Comments on “Use Crystal report with XML Schema”

  • nadeen
    7 July, 2010, 1:27

    thnks machan

  • 7 July, 2010, 11:30

    Cool. Wonder if you could do something similar with Reporting Services?
    That would be a fun RnD project…

  • 7 July, 2010, 11:38

    @Gogula – yeah, I’ll do when I got free time, these days bit busy with office works and Uni Project.