Scott on Writing

Musings on technical writing...

Crystal Reports Tip for the Day: Specifying the Database Name Dynamically

I'm back to doing Crystal Reports for one of my long-term consulting projects... ick.  For the project I work on there's two databases: a test database and the live database, the obvious difference being that the test database has test data and the live database is currently in use with live data.  Both have identical schemas.

Anywho, when creating a Crystal Report through VS.NET you have to specify the datasource that you want to use for the report, which can be done through an ADO DataSet or, as commonly the case in this particular project, through a view, table, or stored procedure on the actual database.  Since I am developing on the test database, I've always connected to the test database when crafting my Crystal Reports.  In my ASP.NET page I programmatically display my Crystal Report by first setting the logon information using the code Eric Landes shares in his article titled Automagically Display Crystal Parameters.  Specifically, in the ApplyInfo() I set the DatabaseName and other related server properties to the current database I am using, which is spelled out in the application's Web.config file.  On the development server the Web.config file indicates to use the test database; on the production server, it says to use the live database.

My assumption was that setting this logon info through this manner would have the correct datasource be called from the Crystal Report when viewed on an ASP.NET page.  That is, if I created the CR referencing the test database, but the CR was on the live site, it would use the live site's underlying data.  Today, when pushing up my latest batch of reports to the live server, I found out that assumption was incorrect.  Meh.  The result - the reports on the live site were showing data from the test database.  All of a sudden end users were seeing information about people named Mr. Test and Mr. Test 2.  Eep.

My workaround was to augment the ApplyInfo() method, adding the following line in the For Each oCRTable In oCRTables loop:

oCRTable.Location = oCRConnectionInfo.DatabaseName & ".dbo." & oCRTable.Location.Substring(oCRTable.Location.LastIndexOf(".") + 1)

This modifies the datasource SQL used from something like testDB.dbo.TableName to realDB.dbo.TableName, where realDB is the database name specified in the Web.config file (so its using the test database for its datasource when working on the development server and the live database for its datasource when viewing a report on the live site).

Hope this saves someone else the headache I experienced!  :-)

posted on Wednesday, March 09, 2005 7:09 PM

Feedback

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/10/2005 9:09 PM Jeff Atwood

Someone needs to take Crystal Reports out behind the woodshed and put it out of its ever-lovin' misery. Man. I remember using that POS in VB3 and hating life. I've specifically avoided it ever since then.

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/10/2005 11:43 PM Armando Andrade

Hello Scott, i have worked a lot with Crystal Reports.NET in a ASP.NET Intranet, i even made a class for working with this reports, would you like me to send you the class so you can review it?

# Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/11/2005 7:06 AM CodeBetter.Com Link Blog

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/11/2005 12:42 PM Scott Mitchell

Armando, why don't you post your library up on a public, stable URL and then share the link here?

Thanks

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/15/2005 11:46 AM Sonu Kapoor

I would like to see the class too Armando. If possible can you please email it to me via sonukapoorREMOVETHIS@gmail.comREMOVETHIS

Thanks

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/15/2005 2:27 PM Ryan Farley

Hi Scott,

You can also just remove the prefixed database and owner names from the table. I usually just go into Database|Set Location and remove the prefix info from each table name. This works in most cases since the database you use to build the report will likely be identical to the one you run it against in production. Removing those qualifying prefixes will assume the database and owner info from your connection in the report.

-Ryan

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/17/2005 11:45 AM johnway

hello,I hvae spent a lot of to deal with it.Any easy read materail can be suggested to me?

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/17/2005 11:51 AM Scott Mitchell

Johnway, you might want to consider picking up a copy of Brian Bischof's book, Crystal Reports .NET Programming - http://www.amazon.com/exec/obidos/ASIN/0974953652/4guysfromrollaco

I use that book quite a bit when working with CR. (And writing a review of Brian's book is on my never-shrinking TODO list!)

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/21/2005 3:27 PM Ben Strackany

What about just using an alias in the connection string ("SERVER=MYSERVER"), and using cliconfg.exe on the server hosting the report to point the MYSERVER alias to the appropriate database server?

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/21/2005 3:29 PM Ben Strackany

Although I guess my example string should have been "Data Source=MYSERVER", heh.

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/27/2005 4:25 AM Lis

Hello Scott,

I have created my reports in Crystal(not in-built). I have 8 reports and a user can view them separately cos they are in separate asp pages. But this is taking so much time. I would like to know how I can call these various reports from a single page. (ASP.NET, VB.NET)

Thanks and Regards,

lis

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 3/27/2005 9:08 AM Scott Mitchell

Lis, it is possible to load and display a CR dynamically, such as passing the report name or id through the querystring. I'm no expert on this, but there are plenty of CR smarties available at the Crystal Reports forum over at:
http://forums.asp.net/76/ShowForum.aspx

hth

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 4/1/2005 3:06 AM Brian Storrar

What a guy! That's probably saved me the best part of a serious stress related cardio-vascular excursion!

Thanks for sharing this.

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 4/3/2005 10:27 PM Tirupathirao

public void LogonToReport(string server, string database,string ID, string password)
{
TableLogOnInfo logonInfo = new TableLogOnInfo();
int i=0;
foreach(CrystalDecisions.CrystalReports.Engine.Table table in rd.Database.Tables)
{
logonInfo = table.LogOnInfo;
logonInfo.ConnectionInfo.AllowCustomConnection = true;
logonInfo.ConnectionInfo.ServerName = server;
logonInfo.ConnectionInfo.DatabaseName = database;
logonInfo.ConnectionInfo.UserID = ID;
logonInfo.ConnectionInfo.Password = password;
table.ApplyLogOnInfo(logonInfo);
rd.Database.Tables[i].Location=table.Name;
rd.DataSourceConnections[server,database].SetConnection(server,database,ID,password);
i++;
}
}

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 5/7/2005 9:20 AM Peter

Thank you very much for this info. This will help so many users who got annoyed with this problem.

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 7/6/2005 2:47 AM Lampan

What if report is using a Stored Proc as DataSource ...?

# re: Crystal Reports Tip for the Day: Specifying the Database Name Dynamically 10/27/2006 1:47 AM gvenu

Hi,

I am working on VB.Net i have problem with setlocation i want to know what is rd in above code

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles