How to load rpt file using c# in visual studio 2010? - crystal-reports-xi

Am using visual studio 2010 and trying to load a rpt file.
I have used the following code.
ReportDocument rpt = new ReportDocument();
rpt.Load("E:\\Crystal reports docs\\Crystal Reports samples\\Crosstab report");
Then I used isLoaded() function to check whether it is loaded.
When I compile the program, it keeps on running.
Any suggestions???
Thanks in advance!!!!

Here is the sample code how to load a crystal report (.rpt) file that is saved on a local drive instead of embedded. The advantage to this is the program does not need to be re-compiled each time a report is modified. Also, the .rpt can be upload from the application and stored in a database and then written to file. Do not embed the .rpt file when using this method.
using System;using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace Report
{
public partial class Report : Document
{
public void ReportLoad()
{
ReportDocument reportDocument = new ReportDocument();
string filePath = "C:\Projects\Application\Report\CrystalReport.rpt";
reportDocument.Load(filePath);
crystalReportViewer.ReportSource = reportDocument;
}
}
}
Refer More about
http://scn.sap.com/thread/3312329
How do I load external Crystal Reports (2008) files in c#?

ReportDocument reportDocument = new ReportDocument();
//ADD
string filePath = openFileDialog1.FileName;
reportDocument.Load(filePath);
crystalReportViewer1.ReportSource = reportDocument;

Related

Testing the compilation of cshtml files using RazorEngine

I want to unit test my cshtml files to ensure they compile successfully as recently I've had some issues where at run time there were failures due to the views expecting properties that weren't present. I am trying to use RazorEngine to check this, and here is my attempt at a unit test:
public void LayoutCshtml_AnyCase_ViewCompilesSuccessfully()
{
var model = new Model();
var templateName = File.ReadAllText("PathToView/_Layout.cshtml");
var result = Engine.Razor.RunCompile(templateName, "key", typeof(Model), model);
//Assertion will go here
}
I will add the assertion later but at the moment just want the view to compile. The templateName correctly picks up the right .cshtml but the result fails because of error:
The predefined type
'System.Runtime.CompilerServices.ExtensionAttribute' is defined in
multiple assemblies in the global alias;
Is anyone familiar with using RazorEngine to test please?

How to import csv file to DB with asp.net mvc using entity framework

Sorry for this question as I'm new to asp.net. I need to make Import Csv files into database that is made in sql server and added to web application by entity framework. I managed to export it, but have no clue how to import it other than maybe using StreamReader with regex. Tried using Lumenworks csvreader, but it doesn't cooparate with entity.
You can look at the CsvHelper NuGet package. You can find parts of the code you need to use here.
Relevant code:
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
CsvReader csvReader = new CsvReader(reader);
csvReader.Configuration.WillThrowOnMissingField = false;
var countries = csvReader.GetRecords<Country>().ToArray();
context.Countries.AddOrUpdate(c => c.Code, countries);
}

EF migration Seeding with large dataset

I am using EF Code First Migration with ASP.NET MVC5 project. I want to seed the database with about 5000 records. The examples on the asp.net web site and blogs all have the seed function within the configuration method.
internal sealed class Configuration : DbMigrationsConfiguration<foo.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
MigrationsDirectory = #"Migrations\ApplicationDbContext";
}
protected override void Seed(foo.ApplicationDbContext context)
{
SeedProducts(context);
SeedFoods(context);
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
What are some options to seed large data sets when using EF Migration?
Currently The configuration file is over 5000 lines long and is different to manage.
I would prefer to store them in another database or excel spreadsheet and then import them in using the seed function. I am not sure how to go about importing data from external sources within the Seed method.
I also tried to break up the data set into several files but when I try to call the function
SeedProducts(context);
SeedFoods(context);
outside of the Configuration Class I get a build error: "The name does not exists in the current context". (I am not sure what this means?
You can store the sql file in the base directory and use it. You can create multiple sql files. I used following code to execute all sql files stored on base directory.
protected override void Seed(CRM.Data.DbContexts.CRMContext context)
{
var sqlfiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory+"\\initialdata", "*.sql");
sqlfiles.ToList().ForEach(x=> context.Database.ExecuteSqlCommand(File.ReadAllText(x)));
}
Why we need to have a seed data for 5000 records. If you prefer this way it will take lot of manual work also. where as, its not required here.
Instantly you can Create Scripts and execute that into you db. via Code as well as Manually.
Db Scripts can be taken for entire db as well as each table, Store Procedure wise also. So, that you will get records particularly.
Follow the steps from this link OR MSDN
Note: After Creating the Database Script. You can read the file from Seed Function and Execute the query from function itself. Or Manually you can go and execute when ever you need it.
I ended up using a CSV (comma delimited file) and storing it as a domain resource. Then reading the CSV file and adding database records:
I am able to Seed the database using EF Migration Seed method and a CSV file as defined as follows in the Migration.cs file. Note: the CSV file in the project in Visual Studio are set to the Build Action to Embedded Resource.
public void SeedData(WebApp.Models.ApplicationDbContext Context)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourceName = "WebApp.SeedData.Name.csv";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
CsvReader csvReader = new CsvReader(reader);
csvReader.Configuration.WillThrowOnMissingField = false;
var records = csvReader.GetRecords<Products>().ToArray();
foreach (Product record in records)
{
Context.Products.AddOrUpdate(record);
}
}
}
Context.SaveChanges();
}

Code first migrations: read sql from file

I following a post from ADO.NET team Code First Migrations: Alpha 3 ‘No-Magic’ Walkthrough
I'm trying to read a file with sql for a migration in a asp.net mvc web application. I'm using sql method. The problem is that no matter what I do I can not get a root path to my project. I always get 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\'
Could someone point me a way to open my file?
Thanks.
Edited
public partial class AddMembership : DbMigration
{
public override void Up()
{
//var file = File.OpenText(System.IO.Path.GetDirectoryName() + "Model/Schema/membership-up.sql");
//var file = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Model/Schema/membership-up.sql");
//var path = HttpContext.Current.Request.MapPath("/Models/Schema/membership-up.sql");
var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
Sql(file.ReadToEnd());
}
public override void Down()
{
// ....
}
}
What about using Server.MapPath
http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
To run the migration directly from the site you could add this code
var dbMigrator = new DbMigrator(new Settings());
dbMigrator.Update();
To run migrations from either process (IIS vs Package Manager Console) you could check first if Server object is created before using Server.MapPath, this way you'd recognize if you're under IIS or under Package Manager Console.
If Server object is null you're under Package Manager Console so you could use something more appropriate to get your current path from like
Directory.GetCurrentDirectory();
So I would replace this code
var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
with something like this
String path;
if (HttpContext.Current.Server != null) {
path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
} else {
path = Path.Combine(Directory.GetCurrentDirectory(), "Models/Schema/membership-up.sql");
}

How to generate an HTML report from PartCover results .xml

How to generate an HTML report from PartCover results .xml
There is a tool you can use to generate a HTML report:
https://github.com/danielpalme/ReportGenerator
Here you can find an article how to integrate the tool into MSBuild:
http://www.palmmedia.de/Blog/2009/10/30/msbuild-code-coverage-analysis-with-partcover-and-reportgenerator
To my knowledge, there is no convenient tool like NCoverExplorer that can transform a PartCover results .xml file into a .html report, but there are some .xsl files that can be used to transform PartCover's results to .html in CruiseControl.NET: Using CruiseControl.NET with PartCover.
You could take those .xsl files from CruiseControl.NET and convert your PartCover results.xml using something like Sandcastle's XslTransform.exe.
By the way, if this happens to be related to TeamCity, the upcoming 5.0 release will include support for .NET coverage using PartCover or NCover. See the documentation for more informations. Otherwise ignore this paragraph ;-)
Easiest solution is probably to use msxsl, a simple command line transformer. I use it for exactly this purpose, and it's easy to integrate into your build system.
http://www.microsoft.com/downloads/details.aspx?FamilyID=2FB55371-C94E-4373-B0E9-DB4816552E41&displaylang=en
Maybe a complicated way of doing it, but I did this with the Simian xml report. Created an XSL file for the formatting, then wrote a dumb little console application;
private const string MissingExtension = "Please enter a valid {0} file, this is missing the extension.";
private const string InvalidExtension = "Please enter a valid {0} file, the file provided has an invalid extension.";
public static void Main(string[] args)
{
if (args.Length < 2)
{
System.Console.WriteLine("Please enter a xsl file and xml file full path.");
return;
}
var xslFile = args[0];
var xmlFile = args[1];
if (!CheckFileNameFormat(xslFile, false))
return;
if (!CheckFileNameFormat(xmlFile, true))
return;
var transform = new XslCompiledTransform();
// Load the XSL stylesheet.
transform.Load(xslFile);
// Transform xml file into an html using the xsl file provided.
transform.Transform(xmlFile, xmlFile.Replace("xml", "html"));
}
private static bool CheckFileNameFormat(string fileName, bool isXmlFile)
{
var extension = isXmlFile ? "xml" : "xsl";
// valida that the argument has a period
if (fileName.IndexOf(".") < 1)
{
System.Console.WriteLine(string.Format(MissingExtension, extension));
return false;
}
var filePieces = fileName.Split('.');
// split on the period and make sure the extension is valid
if (filePieces[filePieces.GetUpperBound(0)] != extension)
{
System.Console.WriteLine(string.Format(InvalidExtension, extension));
return false;
}
return true;
}
Then I can call it from a MSBuild file like so;
<Target Name="RunSimian" DependsOnTargets="RebuildSolution">
<Exec IgnoreExitCode="true" Command=""$(MSBuildProjectDirectory)\..\Build\Packages\Simian\simian-2.2.24.exe" -formatter=xml:$(MSBuildProjectDirectory)\..\Build\Artifacts\simian.xml -language=cs -excludes=$(MSBuildProjectDirectory)\..\Product\Production\**\*.Designer.cs $(MSBuildProjectDirectory)\Production\**\*.cs" >
</Exec>
<Exec IgnoreExitCode="true" Command=""$(MSBuildProjectDirectory)\..\Build\Packages\XmlToHtmlConverter.exe" $(MSBuildProjectDirectory)\..\Build\Packages\Simian\simian.xsl $(MSBuildProjectDirectory)\..\Build\Artifacts\simian.xml">
</Exec>

Resources