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   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles