Saving to XML file; something locks the file after first use; Any ideas?

C

chad.a.morris

I'm new to Crystal Reports, so this may be a dumb problem, but I just
can't figure it out.

Basically, I want to control the selected data in the C# code as
opposed to Crystal Reports (since SQL is truly for querying, and
Crystal Reports is more a reporting tool). The only way I've found to
do this so far is to execute my select in C#, then store to an XML
file, and then have the Crystal Report read the XSD and XML files.

However, I have a HUGE problem. I can only do this once (which works,
by the way), but then, something is locking the file, and I can no
longer access it. I can't delete it, and I can't write to it in my
code again. I close the document, but I'm not sure what else to do.
Even if I close out all applications (Visual Studio, Crystal Reports
XI, etc.), the file remains locked. The only way around it that I've
found is logging out and logging back in. Obviously, this is
unacceptable.

Ok, so here is my code:

Code:
CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new
CrystalDecisions.CrystalReports.Engine.ReportDocument() ;

doc.Load( @"C:\Development\CrystalReports\App1\CustomerProfile.rpt" ) ;

doc.SetDataSource( ds ) ;

string xsdFile =
@"C:\Development\CrystalReports\App1\CustomerProfile.xsd" ;
string xmlFile =
@"C:\Development\CrystalReports\App1\CustomerProfile.xml" ;

ds.WriteXmlSchema( xsdFile ) ;
ds.WriteXml( xmlFile ) ;

crystalReportViewer1.ReportSource = doc ;
crystalReportViewer1.RefreshReport() ;
crystalReportViewer1.Visible = true ;

doc.Close() ;

Does anyone have any idea what would be locking the XML file and why??
The XSD file can be written and rewritten, and it never becomes locked.
But the XML file does after running just once.

Any help is GREATLY APPRECIATED!!!
 
L

Leon Friesema

I'm new to Crystal Reports, so this may be a dumb problem, but I just
can't figure it out.

Basically, I want to control the selected data in the C# code as
opposed to Crystal Reports (since SQL is truly for querying, and
Crystal Reports is more a reporting tool). The only way I've found to
do this so far is to execute my select in C#, then store to an XML
file, and then have the Crystal Report read the XSD and XML files.

However, I have a HUGE problem. I can only do this once (which works,
by the way), but then, something is locking the file, and I can no
longer access it. I can't delete it, and I can't write to it in my
code again. I close the document, but I'm not sure what else to do.
Even if I close out all applications (Visual Studio, Crystal Reports
XI, etc.), the file remains locked. The only way around it that I've
found is logging out and logging back in. Obviously, this is
unacceptable.

Ok, so here is my code:
[CODE SNIPPET]

Does anyone have any idea what would be locking the XML file and why??
The XSD file can be written and rewritten, and it never becomes locked.
But the XML file does after running just once.

Any help is GREATLY APPRECIATED!!!

Is the data you retrieve always in the same format?
If so, you could create an XSD within your project, create a class
instance of this XSD with the class which belongs to it, let the
adapter fill that class with data and only set the datasource for the
report:
Example:

DataSet oSet1 = null;
oSet1 = new RptTotalsQ(); // RptTotalsQ == XSD class
oAdap.Fill(oSet1,"RptTotalsQ"); // oAdap = SqlAdapter
oDoc = new MyReport(); // Report class
axCrystalActiveXReportViewer1.ReportSource = oDoc;

Hope it helps --> the Crystal KB contains some of these samples in
full BTW

greetz, Leon
 
T

thephatp

I don't know how to create a class instance of an XSD (I've never
really used XSDs before). I'm assuming you mean to add the XSD file to
the VS solution, and then create a class instance, correct? How do I
create the class instance?

And thanks for the info. As soon as I figure out how to create the
class, I'll give this a try. It sounds like exactly what I want to do.

Thanks!

- thephatp
 
L

Leon Friesema

I don't know how to create a class instance of an XSD (I've never
really used XSDs before). I'm assuming you mean to add the XSD file to
the VS solution, and then create a class instance, correct? How do I
create the class instance?

And thanks for the info. As soon as I figure out how to create the
class, I'll give this a try. It sounds like exactly what I want to do.

Thanks!

- thephatp


Explanation:
http://msdn.microsoft.com/library/d...mn/html/crtskreportingoffanado.netdataset.asp

greetz.
Leon
 
B

Bruce Wood

I'm not sure why the file remains locked like that, but you are doing
one step more than you need to.

If you design your report using the data source "More Data Sources" ->
ADO.NET (XML), and then point it to the XSD that you already have, then
there's no need to dump the data into an XML file. You can feed the
data to Crystal directly from your ADO.NET DataSet, using the
SetDataSource exactly as you did.

Of course, you have to get the XSD from somewhere, so that
WriteXmlSchema() line is useful the first time you run the program.
However, after that you don't need it any more.

The only hitch with doing it this way (with no XML file) is that in the
report designer you see only the field names, not sample data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top