Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I getting a throw error for creating a crystal report and dont know how to resolve it. Kindly please assist, what could i be missing?

What I have tried:

public ActionResult Download_XMLReport()
      {
          eNtsaRegistration context = new eNtsaRegistration();

          ReportDocument rpt = new ReportDocument();
          rpt.Load(Path.Combine(Server.MapPath("~/Reports"), "AcademyReports.rpt"));
          rpt.SetDataSource(context.TrainingRegs.Select(c => new
          {

             id= c.Id
          }).ToList());
          Response.Buffer = false;
          Response.ClearContent();
          Response.ClearHeaders();

          rpt.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
          rpt.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(5, 5, 5, 5));
          rpt.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA5;

          Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
          stream.Seek(0, SeekOrigin.Begin);




          return File(stream, "application/xml", "eNtsaReportTrainingForm.xml");
      }


// Model
public class eNtsaRegistration:DbContext
    {

        public eNtsaRegistration() : base("eNtsaRegistration")
        {
        }  public DbSet<TrainingRegForm> TrainingRegs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<eNtsaRegistration>(null);
            base.OnModelCreating(modelBuilder);
        }
    }


public class TrainingRegForm
   {
       [Key]
       public Guid? Id { get; set; }
       public string Title { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string Position { get; set; }
       public string Company { get; set; }

       public string StreetAddress { get; set; }

       public string StreetAddressLine { get; set; }

       public string City { get; set; }
       public string StateProvince { get; set; }

       public int ZipCode { get; set; }

       public string Email { get; set; }

       [Required(ErrorMessage = "This field is required")]
       [DataType(DataType.PhoneNumber)]
       public string CellNumber { get; set; }
       public string DietaryRequirement { get; set; }
   }
Posted
Updated 20-Jul-20 0:44am

1 solution

Your Id property is a nullable Guid, which is not supported by Crystal's SetDataSource method.

If the IDs always have a value, then select it:
C#
rpt.SetDataSource(context.TrainingRegs.Select(c => new
{
    id = c.Id.Value
}).ToList());
If you want to ignore the IDs which don't have a value, then filter then out:
C#
rpt.SetDataSource(context.TrainingRegs.Where(c => c.Id != null).Select(c => new
{
    id = c.Id.Value
}).ToList());
If you need to preserve nulls, then you'll need to convert the value to a SqlGuid[^]:
C#
rpt.SetDataSource(context.TrainingRegs.AsEnumerable().Select(c => new
{
    id = c.Id == null ? SqlGuid.Null : new SqlGuid(c.Id.Value)
}).ToList());
 
Share this answer
 
Comments
gcogco10 20-Jul-20 6:53am    
Richard i am not getting Invalid Index, what could i be missing. Yes on the table ID has some values its not empty.
Richard Deeming 20-Jul-20 6:55am    
You have defined your Id property to be a Nullable<Guid>:
Nullable value types - C# reference | Microsoft Docs[^]

That feature is not supported by the DataSet, which is what Crystal Reports is using behind the scenes.

Use one of the options from my solution to remove the error.
gcogco10 20-Jul-20 7:08am    
Richard i have defined it under my model as [Key]
public Guid? Id { get; set; }. This Id holds value within a table from the database.
Richard Deeming 20-Jul-20 7:10am    
Yes - as I keep saying, you have defined it as a nullable Guid.

If you read the link from my previous comment, you will see that adding ? after Guid makes it nullable. It is a shortcut for Nullable<Guid>.

That feature was added in .NET 2.0, and is not supported by the DataSet.

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