{"id":1101,"date":"2008-10-16T20:48:33","date_gmt":"2008-10-17T03:48:33","guid":{"rendered":"http:\/\/members.crystalreportsbook.com\/books\/?p=1101"},"modified":"2010-11-17T19:13:13","modified_gmt":"2010-11-18T02:13:13","slug":"17-5-logging-on-with-viewer-control","status":"publish","type":"post","link":"http:\/\/www.crystalreportsonlinetraining.com\/training\/17-5-logging-on-with-viewer-control\/","title":{"rendered":"17.05 Logging On with Viewer Control"},"content":{"rendered":"<h2>Changing the Data Source with the Viewer Control<\/h2>\n<p>Changing the data sourcusing the viewer control uses the same classes as the ReportDocument class, but it does so in a slightly different manner. Rather than having a collection object that stores a reference to every table in the report, the viewer stores a reference to each unique database connection. Thus, if a report uses three tables, but they are from the same database, the collection will only have a single object in it. If the report pulls data from two different databases, the viewer will have two objects in the collection. The TableLogOnInfos collection is the viewer&#8217;s collection class of database connections. You have to get a reference to each TableLogOnInfo object within this collection and assign the proper connection properties. The viewer doesn&#8217;t have an ApplyLogOnInfo() method. You simply overwrite the existing connection info and it takes effect.<\/p>\n<p>         <b>Listing 17-3. Setting the Logon credentials with the viewer control.<\/b><br \/>\n         <b>[VB.NET]<\/b><br \/>\n         <code>Private Sub LogonToTables(ByVal UserId As String, ByVal Password As String, ByVal ServerName As String, ByVal DatabaseName As String, ByVal ReportFilename As String)<\/code><br \/>\n         <code>Dim myConnectionInfo As CrystalDecisions.Shared.ConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo()<\/code><br \/>\n         <code>'Set the database connection info<\/code><br \/>\n         <code>myConnectionInfo.ServerName = ServerName<\/code><br \/>\n         <code>myConnectionInfo.DatabaseName = DatabaseName<\/code><br \/>\n         <code>myConnectionInfo.UserID = UserId<\/code><br \/>\n         <code>myConnectionInfo.Password = Password<\/code><br \/>\n         <code>'Apply the connection info to every table<\/code><br \/>\n         <code>CrystalReportViewer1.ReportSource = ReportFilename<\/code><br \/>\n         <code>Dim myTableLogOnInfos As CrystalDecisions.Shared.TableLogOnInfos<\/code><br \/>\n         <code>myTableLogOnInfos = CrystalReportViewer1.LogOnInfo<\/code><br \/>\n         <code>For Each myTableLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo In myTableLogOnInfos<\/code><br \/>\n         <code>myTableLogOnInfo.ConnectionInfo = myConnectionInfo<\/code><br \/>\n         <code>Next<\/code><br \/>\n         <code>End Sub<\/code><br \/>\n         <b>[C#]<\/b><br \/>\n         <code>private void LogonToTables(string UserId, string Password, string ServerName, string DatabaseName, string ReportFilename)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>CrystalDecisions.Shared.ConnectionInfo myConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();<\/code><br \/>\n         <code>\/\/Set the database connection info<\/code><br \/>\n         <code>myConnectionInfo.ServerName = ServerName;<\/code><br \/>\n         <code>myConnectionInfo.DatabaseName = DatabaseName;<\/code><br \/>\n         <code>myConnectionInfo.UserID = UserId;<\/code><br \/>\n         <code>myConnectionInfo.Password = Password;<\/code><br \/>\n         <code>\/\/Apply the connection info to every table<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = ReportFilename;<\/code><br \/>\n         <code>CrystalDecisions.Shared.TableLogOnInfos myTableLogOnInfos;<\/code><br \/>\n         <code>myTableLogOnInfos = crystalReportViewer1.LogOnInfo;<\/code><br \/>\n         <code>foreach (CrystalDecisions.Shared.TableLogOnInfo myTableLogOnInfo in myTableLogOnInfos)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>myTableLogOnInfo.ConnectionInfo = myConnectionInfo;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><\/p>\n<blockquote><p>The choice of names for the viewer properties can be confusing if you aren&#8217;t careful. The name of the collection class is called TableLogOnInfos(). Notice that it has an &#8220;s&#8221; at the end of it. It stores a collection of TableLogOnInfo() objects. Notice that this object name is singular. Now for the confusing part: the viewer&#8217;s property name that represents the TableLogOnInfos() collection is called LogOnInfo. Notice that this name doesn&#8217;t have the &#8220;s&#8221; at the end. Thus, the viewer&#8217;s collection property doesn&#8217;t have the same name as the collection class. Rather it has a similar name as the objects it manages. Be careful or else you might think that the collection is the object it stores.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Changing the Data Source with the Viewer Control Changing the data sourcusing the viewer control uses the same classes as the ReportDocument class, but it does so in a slightly different manner. Rather than having a collection object that stores a reference to every table in the report, the viewer stores a reference to each [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,40],"tags":[],"class_list":["post-1101","post","type-post","status-publish","format-standard","hentry","category-chapter-17-data-sources","category-crystal-reportsnet-2008","entry"],"_links":{"self":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/1101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/comments?post=1101"}],"version-history":[{"count":1,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/1101\/revisions"}],"predecessor-version":[{"id":1806,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/1101\/revisions\/1806"}],"wp:attachment":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/media?parent=1101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/categories?post=1101"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/tags?post=1101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}