Reputation: 1
Currently I have 3 servers with different locations. Each of the databases is having the different database name but same Table name. How do I connect to the database dynamically based on the option that I select from a dropdown list?
I tried to use setLogonInfo function but this only applicable to 1 database only without any problem. As I provided the Database name, Server name, username and password.
logOnInfo.ConnectionInfo.ServerName = "ServerName";
logOnInfo.ConnectionInfo.DatabaseName = "DatabaseName";
logOnInfo.ConnectionInfo.UserID = "user";
logOnInfo.ConnectionInfo.Password = "pass";
TableLogOnInfos infos = new TableLogOnInfos();
infos.Add(logOnInfo);
CrystalReportViewer.LogOnInfo = infos;
I would like a code that able to change the ServerName and DatabaseName dynamically without I need to type/choose again.
Upvotes: 0
Views: 361
Reputation: 1047
Basically you need to follow the steps below:
a) Load the Report document
ReportDocument crReport = new ReportDocument();
crReport.Load("path and filename");
b) Create a Report connection information object
ConnectionInfo crConn = new ConnectionInfo();
crConn.ServerName = "my db server name";
crConn.DatabaseName = "my database name";
crConn.UserID = "db user name";
crConn.Password = "db password";
c) Apply the Logon information to each report table
Tables tblsReport = crReport.Database.Tables;
for(int i=0; i<tblsreport.count;i++)>
{
Table tblReport = tblsReport[i];
TableLogOnInfo tliTable = tblReport.LogOnInfo;
tliTable.ConnectionInfo = crConn;
tblReport.ApplyLogOnInfo(tliTable);
}
The solution was extracted from this article.
Upvotes: 1