Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public DataTable LINQToDataTable<t>(IEnumerable<t> varlist)
    {
        DataTable dtReturn = new DataTable();

        // column names 
        PropertyInfo[] oProps = null; //using System.Reflection;

        if (varlist == null) return dtReturn;

        foreach (T rec in varlist)
        {
            // Use reflection to get property names, to create table, Only first time, others  
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;

                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                    == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }

                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }

            DataRow dr = dtReturn.NewRow();

            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }

            dtReturn.Rows.Add(dr);
        }
        return dtReturn;
    }

    protected void btnPreview_Click(object sender, EventArgs e)
    {
        ReportDocument rptDoc = new ReportDocument();
        DataTable dtable = new DataTable();

        RLMemberDBEntities db = new RLMemberDBEntities();

        String fromid, toid;
        fromid = Convert.ToString(cboFromCustomer.SelectedValue);
        Session["customer"] = fromid;
        toid = Convert.ToString(cboToCustomer.SelectedValue);
        var customer = from customertable in db.CSR_CU_ACNT
                      where (customertable.CU_ACNT_NUM == fromid )
                      //orderby 
                      select new { customertable.CU_ACNT_NUM, customertable.CUST_FRST_NM, customertable.ENC_CTZ_CORP_NUM, customertable.CUST_TYP_CD, customertable.BIRTH_DT};
        DataTable dt = new DataTable();
        dt = LINQToDataTable(customer);

        var service = from servicetable in db.CUS_SERVICE
                      where (servicetable.CU_ACNT_NUM == fromid)
                      select new {servicetable.CUS_SRV_ACNT,servicetable.SVR_PLAN,servicetable.ATV_DATE };

        DataTable dtsub = new DataTable();
        dtsub = LINQToDataTable(service);

        rptDoc.Load(Server.MapPath("~\\Reports\\CustomerReportNService - Copy.rpt"));//show 

        rptDoc.SetDataSource(dt);



        rptDoc.SetParameterValue("ReportTitle", "Customer Report");
        rptDoc.SetParameterValue("From", Session["customer"]);

        Session["Report"] = rptDoc;
        CrystalReportViewer1.ReportSource = rptDoc;
        //
        

    }
Posted
Updated 17-Aug-14 22:24pm
v2
Comments
Dilan Shaminda 18-Aug-14 4:25am    
And the question is?Do you want to pass parameters to your sub-report?

1 solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using RLMemberDBModel;
using System.Reflection;

public partial class Reports_MemberReport : System.Web.UI.Page
{
#region Help methods
public DataTable LINQToDataTable<t>(IEnumerable<t> varlist)
{
DataTable dtReturn = new DataTable();

// column names
PropertyInfo[] oProps = null; //using System.Reflection;

if (varlist == null) return dtReturn;

foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;

if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}

dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}

DataRow dr = dtReturn.NewRow();

foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}

dtReturn.Rows.Add(dr);
}
return dtReturn;
}
#endregion help methods
protected void Page_Load(object sender, EventArgs e)
{
//if (Session["CurrentUser"] == null)
//{
// Response.Redirect("~/LoginForm.aspx");
//}
}
protected void btnPreview_Click(object sender, EventArgs e)
{
ReportDocument rptDoc = new ReportDocument();
DataTable dtable = new DataTable();

RLMemberDBEntities db = new RLMemberDBEntities();
Int32 fromid, toid;
////fromid = Convert.ToInt32(cboFromMember.SelectedValue);
////toid = Convert.ToInt32(cboToPartner.SelectedValue);
var member = from table in db.vw_MembersReport
//where (table.CUS_CARD_ID >= fromid && table.PartnerID <= toid)
//orderby
select new { table.CUS_CARD_ID, table.CU_ACNT_NUM, table.CUST_FRST_NM, table.ENC_CTZ_CORP_NUM, table.CUST_TYP_CD, table.BIRTH_DT, table.Member_Type, table.ISS_DATE,table.VLD_DATE };
DataTable dt = new DataTable();
dt = LINQToDataTable(member);

var service = from servicetable in db.CUS_SERVICE
//where (servicetable.CU_ACNT_NUM == fromid)
select new { servicetable.CUS_SRV_ACNT, servicetable.CU_ACNT_NUM, servicetable.SVR_PLAN, servicetable.ATV_DATE };
DataTable dtsub = new DataTable();
dtsub = LINQToDataTable(service);

//dtable = departmentcontroller.DepartmentListReport(cboFromDepartment.SelectedValue, cboToDepartment.SelectedValue);

rptDoc.Load(Server.MapPath("~\\Reports\\Member18Sep.rpt"));
rptDoc.SetDataSource(dt);

rptDoc.OpenSubreport("subServicePlan").SetDataSource(dtsub);

rptDoc.SetParameterValue("ReportTitle", "Members List Report");
//rptDoc.SetParameterValue("From", cboFromPartner.SelectedItem.Text);
//rptDoc.SetParameterValue("To", cboToPartner.SelectedItem.Text);

Session["Report"] = rptDoc;
//CrystalReportViewer1.ReportSource = rptDoc;
ClientScript.RegisterStartupScript(this.GetType(), "key", "window.open('LargeSizeViewer.aspx');", true);
}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900