locked
Casting isssue C# RRS feed

  • Question

  • hello guruz i have following function which load report.

     public void loadreport(string reportname, DataSet Recordset)
            {
                try {


                    CrystalReport1 a = new CrystalReport1();
                    a.SetDataSource(Recordset.Tables[0]);
                  
                    crystalReportViewer1.ReportSource = a;
                    crystalReportViewer1.Refresh();
                }
                catch (System.Exception s)
                { }
            }


    I want to make it dynmic by casting reportname with  CrystalReport1 a = new CrystalReport1();
    rather than doing like this
     if(reportname=="test")
      {
       CrystalReport1 a = new CrystalReport1();
      }
     if(reportname=="test2")
      {
       CrystalReport2 a = new CrystalReport2();
      }


    i want to cast string to crystal report object
    how can i do that help me
    regards


    Ali Muhammad

    • Moved by CoolDadTx Monday, August 20, 2012 2:04 PM Language related (From:Visual C# General)
    • Moved by Mike Dos Zhang Wednesday, August 22, 2012 3:23 AM (From:Visual C# Language)
    Tuesday, August 7, 2012 6:58 AM

All replies

  • Hi,

    I don't think this is a C# problem in general but one of the CrystalReports Software (if this is what you are using?). You should look that problem up in the documentation or search for help in their forums.

    If this isn't CrystalReports but your own class, then no one can really help you because it's your decision what happens if you cast a string to CrystalReports.

    Regards,

    Steve.

    Tuesday, August 7, 2012 8:48 AM
  • i did not get complete picture, you have one report viewer where you want to bind a report object which is bound to a datatable. Here, you can do eveything  with single viewer and single report object. You just need separate datatable to bind the report object to that. It should work, just need to pass dataset only.

    regards

    joon

    Tuesday, August 7, 2012 9:16 AM
  • see joon

     CrystalReport1 a = new CrystalReport1();
    here  CrystalReport1 a = new CrystalReport1(); is hard coded i need to use "reportname"  by casting just like this

    reportname a = new reportname();

    so that i can  dynamically pick the required report.

    how can i cast crystal report object from give string


    Ali Muhammad

    Tuesday, August 7, 2012 9:25 AM
  • I am not sure if I get your question correctly, but if you do not want to create and initialize crystal report object, create and initialize at class level, so that you only set the data source based on your report name.

    CrystalReport1 a = new CrystalReport1();
    
    public void loadreport(string reportname, DataSet Recordset)
    {
                try 
    			{
                    if (reportname == "test1")
    					a.SetDataSource(Recordset.Tables[0]);
                    if (reportname == "test2")
    					a.SetDataSource(Recordset.Tables[1]);               
                    crystalReportViewer1.ReportSource = a;
                    crystalReportViewer1.Refresh();
                }
                catch (System.Exception s)
                { }
    }



    Balaji Baskar
    http://codesupport.wordpress.com
    Click on "Vote As Helpful" and "Mark As Answer" if this has helped you.

    • Proposed as answer by Balaji Baskar Wednesday, March 8, 2017 5:26 PM
    Tuesday, August 7, 2012 9:33 AM
  • Hi Ali ,

    For Crystal report question, please refer to this forum: http://forums.sdn.sap.com/forum.jspa?forumID=313

    Thanks  for your understanding and support.

    Regards , 

    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, August 20, 2012 10:58 AM